Iptables + NAT and port forward loop with one network interfaceiptables port forward forwardingiptables port redirection on Ubuntuiptables and NAT/port forwarding not working after server rebootForward http traffic to another ip address with iptablesTrying to make iptables stateless is causing unforeseen filteringHow to configure port-forwarding to enable internal service accessed by another machine?RHEL 6 Having issues forwarding port 80 to port 8080iptables fails to load nf_conntrack_ftpiptables port forwarding to server with different portHow to NAT inbound external traffic from port 80 to a local different port using iptables?

Is there a context where the expression `a.b::c` makes sense?

Why did Jon Snow do this immoral act if he is so honorable?

Are black holes spherical during merger?

What are Antecedent & Consequent Phrases in Music?

How can I tell if I'm being too picky as a referee?

Making a electromagnet

What is the use case for non-breathable waterproof pants?

Can a person survive on blood in place of water?

Beginner looking to learn/master musical theory and instrumental ability. Where should I begin?

Does French have the English "short i" vowel?

Why does Bran want to find Drogon?

How can I make an argument that my time is valuable?

Why are Stein manifolds/spaces the analog of affine varieties/schemes in algebraic geometry?

Is there a simple example that empirical evidence is misleading?

WordPress 5.2.1 deactivated my jQuery

便利な工具 what does な means

Public transport tickets in UK for two weeks

Determine this limit

Time complexity of an algorithm: Is it important to state the base of the logarithm?

Can my floppy disk still work without a shutter spring?

Why do Russians almost not use verbs of possession akin to "have"?

Is it possible to remotely hack the GPS system and disable GPS service worldwide?

Dad jokes are fun

Can I tell a prospective employee that everyone in the team is leaving?



Iptables + NAT and port forward loop with one network interface


iptables port forward forwardingiptables port redirection on Ubuntuiptables and NAT/port forwarding not working after server rebootForward http traffic to another ip address with iptablesTrying to make iptables stateless is causing unforeseen filteringHow to configure port-forwarding to enable internal service accessed by another machine?RHEL 6 Having issues forwarding port 80 to port 8080iptables fails to load nf_conntrack_ftpiptables port forwarding to server with different portHow to NAT inbound external traffic from port 80 to a local different port using iptables?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am setting an OpenVPN Access Server behind my NAT server in AWS. Currently, my NAT server has one network interface and I am trying to forward some ports to OpenVPN Access Server.



Port forwarding works and I can connect to my OpenVPN Access Server either via browser or OpenVPN Client (establish the VPN connection).



Now the problem is that OpenVPN Access Server is using ports 443 and 943 for HTTPS. But when I try to access HTTPS websites while connect to VPN, HTTPS certs get all invalidated as the HTTPS connection gets redirected back to the OpenVPN AS.



Here are my IPTABLES rules:



iptables -t nat -A POSTROUTING -o eth0 -s 172.16.0.0/24 -j MASQUERADE

iptables -A FORWARD -m state -p tcp -d 172.16.0.213 --dport 943 --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 943 -j DNAT --to-destination 172.16.0.213:943
iptables -t nat -A POSTROUTING -p tcp -d 172.16.0.213 --dport 943 -j SNAT --to-source 172.16.0.213

iptables -A FORWARD -m state -p tcp -d 172.16.0.213 --dport 443 --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 172.16.0.213:443
iptables -t nat -A POSTROUTING -p tcp -d 172.16.0.213 --dport 443 -j SNAT --to-source 172.16.0.213

iptables -t nat -A PREROUTING -p udp -i eth0 -d 172.16.0.213 --dport 1194 -j DNAT --to-destination 172.16.0.213:1194


The outpout of iptables -t nat -L:



Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- anywhere anywhere tcp dpt:https to:172.16.0.213:443
DNAT udp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal udp dpt:openvpn to:172.16.0.213:1194
DNAT tcp -- anywhere anywhere tcp dpt:943 to:172.16.0.213:943

Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT tcp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal tcp dpt:https to:172.16.0.213
SNAT tcp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal tcp dpt:943 to:172.16.0.213
MASQUERADE all -- ip-172-16-0-0.eu-west-1.compute.internal/24 anywhere


Is it still possible to do this with one network interface and solve it with iptables?



Basically, I want to tell that "iptables, please do not do any port forwarding from inside to the outside", currently the port forwarding goes into "loop".



Thanks for any help and ideas!










share|improve this question
























  • You need to organize your question in a better way. Show all your iptables rules in a consistent order. Describe your setup and the interfaces you have on your server, etc.

    – Khaled
    Mar 30 '17 at 9:54











  • These are all my rules. I am using debian, which has one network interface. And using iptables, as you can see.

    – Sigmar Muuga
    Mar 30 '17 at 9:59












  • Is it mandatory for you to have the VPN listening to port 443?

    – Tero Kilkanen
    Mar 30 '17 at 12:27











  • I was thinking about using alternative ports as well but I'd rather have a solution that works with standard ports, as users are otherwise going to be needing some extra assistance.

    – Sigmar Muuga
    Mar 30 '17 at 14:44

















0















I am setting an OpenVPN Access Server behind my NAT server in AWS. Currently, my NAT server has one network interface and I am trying to forward some ports to OpenVPN Access Server.



Port forwarding works and I can connect to my OpenVPN Access Server either via browser or OpenVPN Client (establish the VPN connection).



Now the problem is that OpenVPN Access Server is using ports 443 and 943 for HTTPS. But when I try to access HTTPS websites while connect to VPN, HTTPS certs get all invalidated as the HTTPS connection gets redirected back to the OpenVPN AS.



Here are my IPTABLES rules:



iptables -t nat -A POSTROUTING -o eth0 -s 172.16.0.0/24 -j MASQUERADE

iptables -A FORWARD -m state -p tcp -d 172.16.0.213 --dport 943 --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 943 -j DNAT --to-destination 172.16.0.213:943
iptables -t nat -A POSTROUTING -p tcp -d 172.16.0.213 --dport 943 -j SNAT --to-source 172.16.0.213

iptables -A FORWARD -m state -p tcp -d 172.16.0.213 --dport 443 --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 172.16.0.213:443
iptables -t nat -A POSTROUTING -p tcp -d 172.16.0.213 --dport 443 -j SNAT --to-source 172.16.0.213

iptables -t nat -A PREROUTING -p udp -i eth0 -d 172.16.0.213 --dport 1194 -j DNAT --to-destination 172.16.0.213:1194


The outpout of iptables -t nat -L:



Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- anywhere anywhere tcp dpt:https to:172.16.0.213:443
DNAT udp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal udp dpt:openvpn to:172.16.0.213:1194
DNAT tcp -- anywhere anywhere tcp dpt:943 to:172.16.0.213:943

Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT tcp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal tcp dpt:https to:172.16.0.213
SNAT tcp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal tcp dpt:943 to:172.16.0.213
MASQUERADE all -- ip-172-16-0-0.eu-west-1.compute.internal/24 anywhere


Is it still possible to do this with one network interface and solve it with iptables?



Basically, I want to tell that "iptables, please do not do any port forwarding from inside to the outside", currently the port forwarding goes into "loop".



Thanks for any help and ideas!










share|improve this question
























  • You need to organize your question in a better way. Show all your iptables rules in a consistent order. Describe your setup and the interfaces you have on your server, etc.

    – Khaled
    Mar 30 '17 at 9:54











  • These are all my rules. I am using debian, which has one network interface. And using iptables, as you can see.

    – Sigmar Muuga
    Mar 30 '17 at 9:59












  • Is it mandatory for you to have the VPN listening to port 443?

    – Tero Kilkanen
    Mar 30 '17 at 12:27











  • I was thinking about using alternative ports as well but I'd rather have a solution that works with standard ports, as users are otherwise going to be needing some extra assistance.

    – Sigmar Muuga
    Mar 30 '17 at 14:44













0












0








0








I am setting an OpenVPN Access Server behind my NAT server in AWS. Currently, my NAT server has one network interface and I am trying to forward some ports to OpenVPN Access Server.



Port forwarding works and I can connect to my OpenVPN Access Server either via browser or OpenVPN Client (establish the VPN connection).



Now the problem is that OpenVPN Access Server is using ports 443 and 943 for HTTPS. But when I try to access HTTPS websites while connect to VPN, HTTPS certs get all invalidated as the HTTPS connection gets redirected back to the OpenVPN AS.



Here are my IPTABLES rules:



iptables -t nat -A POSTROUTING -o eth0 -s 172.16.0.0/24 -j MASQUERADE

iptables -A FORWARD -m state -p tcp -d 172.16.0.213 --dport 943 --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 943 -j DNAT --to-destination 172.16.0.213:943
iptables -t nat -A POSTROUTING -p tcp -d 172.16.0.213 --dport 943 -j SNAT --to-source 172.16.0.213

iptables -A FORWARD -m state -p tcp -d 172.16.0.213 --dport 443 --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 172.16.0.213:443
iptables -t nat -A POSTROUTING -p tcp -d 172.16.0.213 --dport 443 -j SNAT --to-source 172.16.0.213

iptables -t nat -A PREROUTING -p udp -i eth0 -d 172.16.0.213 --dport 1194 -j DNAT --to-destination 172.16.0.213:1194


The outpout of iptables -t nat -L:



Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- anywhere anywhere tcp dpt:https to:172.16.0.213:443
DNAT udp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal udp dpt:openvpn to:172.16.0.213:1194
DNAT tcp -- anywhere anywhere tcp dpt:943 to:172.16.0.213:943

Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT tcp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal tcp dpt:https to:172.16.0.213
SNAT tcp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal tcp dpt:943 to:172.16.0.213
MASQUERADE all -- ip-172-16-0-0.eu-west-1.compute.internal/24 anywhere


Is it still possible to do this with one network interface and solve it with iptables?



Basically, I want to tell that "iptables, please do not do any port forwarding from inside to the outside", currently the port forwarding goes into "loop".



Thanks for any help and ideas!










share|improve this question
















I am setting an OpenVPN Access Server behind my NAT server in AWS. Currently, my NAT server has one network interface and I am trying to forward some ports to OpenVPN Access Server.



Port forwarding works and I can connect to my OpenVPN Access Server either via browser or OpenVPN Client (establish the VPN connection).



Now the problem is that OpenVPN Access Server is using ports 443 and 943 for HTTPS. But when I try to access HTTPS websites while connect to VPN, HTTPS certs get all invalidated as the HTTPS connection gets redirected back to the OpenVPN AS.



Here are my IPTABLES rules:



iptables -t nat -A POSTROUTING -o eth0 -s 172.16.0.0/24 -j MASQUERADE

iptables -A FORWARD -m state -p tcp -d 172.16.0.213 --dport 943 --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 943 -j DNAT --to-destination 172.16.0.213:943
iptables -t nat -A POSTROUTING -p tcp -d 172.16.0.213 --dport 943 -j SNAT --to-source 172.16.0.213

iptables -A FORWARD -m state -p tcp -d 172.16.0.213 --dport 443 --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 172.16.0.213:443
iptables -t nat -A POSTROUTING -p tcp -d 172.16.0.213 --dport 443 -j SNAT --to-source 172.16.0.213

iptables -t nat -A PREROUTING -p udp -i eth0 -d 172.16.0.213 --dport 1194 -j DNAT --to-destination 172.16.0.213:1194


The outpout of iptables -t nat -L:



Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- anywhere anywhere tcp dpt:https to:172.16.0.213:443
DNAT udp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal udp dpt:openvpn to:172.16.0.213:1194
DNAT tcp -- anywhere anywhere tcp dpt:943 to:172.16.0.213:943

Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT tcp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal tcp dpt:https to:172.16.0.213
SNAT tcp -- anywhere ip-172-16-0-213.eu-west-1.compute.internal tcp dpt:943 to:172.16.0.213
MASQUERADE all -- ip-172-16-0-0.eu-west-1.compute.internal/24 anywhere


Is it still possible to do this with one network interface and solve it with iptables?



Basically, I want to tell that "iptables, please do not do any port forwarding from inside to the outside", currently the port forwarding goes into "loop".



Thanks for any help and ideas!







networking iptables vpn openvpn nat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 30 '17 at 10:05







Sigmar Muuga

















asked Mar 30 '17 at 9:39









Sigmar MuugaSigmar Muuga

33




33












  • You need to organize your question in a better way. Show all your iptables rules in a consistent order. Describe your setup and the interfaces you have on your server, etc.

    – Khaled
    Mar 30 '17 at 9:54











  • These are all my rules. I am using debian, which has one network interface. And using iptables, as you can see.

    – Sigmar Muuga
    Mar 30 '17 at 9:59












  • Is it mandatory for you to have the VPN listening to port 443?

    – Tero Kilkanen
    Mar 30 '17 at 12:27











  • I was thinking about using alternative ports as well but I'd rather have a solution that works with standard ports, as users are otherwise going to be needing some extra assistance.

    – Sigmar Muuga
    Mar 30 '17 at 14:44

















  • You need to organize your question in a better way. Show all your iptables rules in a consistent order. Describe your setup and the interfaces you have on your server, etc.

    – Khaled
    Mar 30 '17 at 9:54











  • These are all my rules. I am using debian, which has one network interface. And using iptables, as you can see.

    – Sigmar Muuga
    Mar 30 '17 at 9:59












  • Is it mandatory for you to have the VPN listening to port 443?

    – Tero Kilkanen
    Mar 30 '17 at 12:27











  • I was thinking about using alternative ports as well but I'd rather have a solution that works with standard ports, as users are otherwise going to be needing some extra assistance.

    – Sigmar Muuga
    Mar 30 '17 at 14:44
















You need to organize your question in a better way. Show all your iptables rules in a consistent order. Describe your setup and the interfaces you have on your server, etc.

– Khaled
Mar 30 '17 at 9:54





You need to organize your question in a better way. Show all your iptables rules in a consistent order. Describe your setup and the interfaces you have on your server, etc.

– Khaled
Mar 30 '17 at 9:54













These are all my rules. I am using debian, which has one network interface. And using iptables, as you can see.

– Sigmar Muuga
Mar 30 '17 at 9:59






These are all my rules. I am using debian, which has one network interface. And using iptables, as you can see.

– Sigmar Muuga
Mar 30 '17 at 9:59














Is it mandatory for you to have the VPN listening to port 443?

– Tero Kilkanen
Mar 30 '17 at 12:27





Is it mandatory for you to have the VPN listening to port 443?

– Tero Kilkanen
Mar 30 '17 at 12:27













I was thinking about using alternative ports as well but I'd rather have a solution that works with standard ports, as users are otherwise going to be needing some extra assistance.

– Sigmar Muuga
Mar 30 '17 at 14:44





I was thinking about using alternative ports as well but I'd rather have a solution that works with standard ports, as users are otherwise going to be needing some extra assistance.

– Sigmar Muuga
Mar 30 '17 at 14:44










2 Answers
2






active

oldest

votes


















0














You should reconsider whether any of the DNAT rules are required. You haven't provided a reason for it. Same with the SNAT rules, as they're forcing all of the connections to appear local for no clear reason.



If the DNATs are required, use connmark to mark and restore packets coming from the openvpn-as tun interface in the PREROUTING chain of the mangle table, and add a -m mark ! --mark <the mark you set> condition on your nat PREROUTING port 443 DNAT rule. This will prevent outgoing connections from being DNAT'd back to your device.




Edited based on your network:



Remove all SNAT rules. You can't audit connections on the openvpn-as server with this in place.



Make sure the openvpn-as server is routing connections back through your gateway. Either set the default route to the gateway (simple), or policy route (complex).



Change all of your DNAT rules to ignore your local source range, ie ! -s 172.16.0.0/24. If openvpn-as isn't also performing MASQUERADE or SNAT, you may need to add the VPN IP ranges as well as additional ! -s base/mask per range.






share|improve this answer

























  • my openvpn-as is running on a separate machine though.

    – Sigmar Muuga
    Mar 30 '17 at 11:32











  • Clarify your network layout in your question. Preferably with a diagram.

    – Andrew Domaszek
    Mar 30 '17 at 11:38












  • I hope it clarifies... ibb.co/dVK5MF

    – Sigmar Muuga
    Mar 30 '17 at 11:51


















0














iptables -t nat -I PREROUTING -i eth1 -p tcp --dport 443 -j DNAT --to 192.168.0.1:943
iptables -t nat -I POSTROUTING -o eth1 -p tcp -d 192.168.0.1 --dport 943 -j MASQUERADE


Substitute 443 with the public port to listen to, 192.168.0.1 with your other server and 943 with the other server's listening port. Replace eth1 with your interface. Be aware that some configs have virtual interfaces, so something like vlan1 or ppp0 could be required.



The first line is the basic rule forwarding the packets. But since the packets would have the source address intact, it would result in asymmetric routing which would break with this stateful netfilter. Hence comes the second line - replace the source address of the packet with one of this server. This gives the connection a little detour forcing it to go twice through this server, but it does get rid of the asymmetry allowing this to work.



This solution relies on the ports used to tell which connections from the first rule are used by the second one. This will be fine with your simple setup, but for anyone trying to implement this in a router also having users behind it trying to access the same resource, it's worth noting that the MARK feature of netfilter should be used to tell the second MASQUERADE line which packets it should handle instead of just the port. Having MASQUERADE applied to everything could introduce unwanted behavior.






share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "2"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f841521%2fiptables-nat-and-port-forward-loop-with-one-network-interface%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You should reconsider whether any of the DNAT rules are required. You haven't provided a reason for it. Same with the SNAT rules, as they're forcing all of the connections to appear local for no clear reason.



    If the DNATs are required, use connmark to mark and restore packets coming from the openvpn-as tun interface in the PREROUTING chain of the mangle table, and add a -m mark ! --mark <the mark you set> condition on your nat PREROUTING port 443 DNAT rule. This will prevent outgoing connections from being DNAT'd back to your device.




    Edited based on your network:



    Remove all SNAT rules. You can't audit connections on the openvpn-as server with this in place.



    Make sure the openvpn-as server is routing connections back through your gateway. Either set the default route to the gateway (simple), or policy route (complex).



    Change all of your DNAT rules to ignore your local source range, ie ! -s 172.16.0.0/24. If openvpn-as isn't also performing MASQUERADE or SNAT, you may need to add the VPN IP ranges as well as additional ! -s base/mask per range.






    share|improve this answer

























    • my openvpn-as is running on a separate machine though.

      – Sigmar Muuga
      Mar 30 '17 at 11:32











    • Clarify your network layout in your question. Preferably with a diagram.

      – Andrew Domaszek
      Mar 30 '17 at 11:38












    • I hope it clarifies... ibb.co/dVK5MF

      – Sigmar Muuga
      Mar 30 '17 at 11:51















    0














    You should reconsider whether any of the DNAT rules are required. You haven't provided a reason for it. Same with the SNAT rules, as they're forcing all of the connections to appear local for no clear reason.



    If the DNATs are required, use connmark to mark and restore packets coming from the openvpn-as tun interface in the PREROUTING chain of the mangle table, and add a -m mark ! --mark <the mark you set> condition on your nat PREROUTING port 443 DNAT rule. This will prevent outgoing connections from being DNAT'd back to your device.




    Edited based on your network:



    Remove all SNAT rules. You can't audit connections on the openvpn-as server with this in place.



    Make sure the openvpn-as server is routing connections back through your gateway. Either set the default route to the gateway (simple), or policy route (complex).



    Change all of your DNAT rules to ignore your local source range, ie ! -s 172.16.0.0/24. If openvpn-as isn't also performing MASQUERADE or SNAT, you may need to add the VPN IP ranges as well as additional ! -s base/mask per range.






    share|improve this answer

























    • my openvpn-as is running on a separate machine though.

      – Sigmar Muuga
      Mar 30 '17 at 11:32











    • Clarify your network layout in your question. Preferably with a diagram.

      – Andrew Domaszek
      Mar 30 '17 at 11:38












    • I hope it clarifies... ibb.co/dVK5MF

      – Sigmar Muuga
      Mar 30 '17 at 11:51













    0












    0








    0







    You should reconsider whether any of the DNAT rules are required. You haven't provided a reason for it. Same with the SNAT rules, as they're forcing all of the connections to appear local for no clear reason.



    If the DNATs are required, use connmark to mark and restore packets coming from the openvpn-as tun interface in the PREROUTING chain of the mangle table, and add a -m mark ! --mark <the mark you set> condition on your nat PREROUTING port 443 DNAT rule. This will prevent outgoing connections from being DNAT'd back to your device.




    Edited based on your network:



    Remove all SNAT rules. You can't audit connections on the openvpn-as server with this in place.



    Make sure the openvpn-as server is routing connections back through your gateway. Either set the default route to the gateway (simple), or policy route (complex).



    Change all of your DNAT rules to ignore your local source range, ie ! -s 172.16.0.0/24. If openvpn-as isn't also performing MASQUERADE or SNAT, you may need to add the VPN IP ranges as well as additional ! -s base/mask per range.






    share|improve this answer















    You should reconsider whether any of the DNAT rules are required. You haven't provided a reason for it. Same with the SNAT rules, as they're forcing all of the connections to appear local for no clear reason.



    If the DNATs are required, use connmark to mark and restore packets coming from the openvpn-as tun interface in the PREROUTING chain of the mangle table, and add a -m mark ! --mark <the mark you set> condition on your nat PREROUTING port 443 DNAT rule. This will prevent outgoing connections from being DNAT'd back to your device.




    Edited based on your network:



    Remove all SNAT rules. You can't audit connections on the openvpn-as server with this in place.



    Make sure the openvpn-as server is routing connections back through your gateway. Either set the default route to the gateway (simple), or policy route (complex).



    Change all of your DNAT rules to ignore your local source range, ie ! -s 172.16.0.0/24. If openvpn-as isn't also performing MASQUERADE or SNAT, you may need to add the VPN IP ranges as well as additional ! -s base/mask per range.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 30 '17 at 12:05

























    answered Mar 30 '17 at 10:20









    Andrew DomaszekAndrew Domaszek

    4,77011025




    4,77011025












    • my openvpn-as is running on a separate machine though.

      – Sigmar Muuga
      Mar 30 '17 at 11:32











    • Clarify your network layout in your question. Preferably with a diagram.

      – Andrew Domaszek
      Mar 30 '17 at 11:38












    • I hope it clarifies... ibb.co/dVK5MF

      – Sigmar Muuga
      Mar 30 '17 at 11:51

















    • my openvpn-as is running on a separate machine though.

      – Sigmar Muuga
      Mar 30 '17 at 11:32











    • Clarify your network layout in your question. Preferably with a diagram.

      – Andrew Domaszek
      Mar 30 '17 at 11:38












    • I hope it clarifies... ibb.co/dVK5MF

      – Sigmar Muuga
      Mar 30 '17 at 11:51
















    my openvpn-as is running on a separate machine though.

    – Sigmar Muuga
    Mar 30 '17 at 11:32





    my openvpn-as is running on a separate machine though.

    – Sigmar Muuga
    Mar 30 '17 at 11:32













    Clarify your network layout in your question. Preferably with a diagram.

    – Andrew Domaszek
    Mar 30 '17 at 11:38






    Clarify your network layout in your question. Preferably with a diagram.

    – Andrew Domaszek
    Mar 30 '17 at 11:38














    I hope it clarifies... ibb.co/dVK5MF

    – Sigmar Muuga
    Mar 30 '17 at 11:51





    I hope it clarifies... ibb.co/dVK5MF

    – Sigmar Muuga
    Mar 30 '17 at 11:51













    0














    iptables -t nat -I PREROUTING -i eth1 -p tcp --dport 443 -j DNAT --to 192.168.0.1:943
    iptables -t nat -I POSTROUTING -o eth1 -p tcp -d 192.168.0.1 --dport 943 -j MASQUERADE


    Substitute 443 with the public port to listen to, 192.168.0.1 with your other server and 943 with the other server's listening port. Replace eth1 with your interface. Be aware that some configs have virtual interfaces, so something like vlan1 or ppp0 could be required.



    The first line is the basic rule forwarding the packets. But since the packets would have the source address intact, it would result in asymmetric routing which would break with this stateful netfilter. Hence comes the second line - replace the source address of the packet with one of this server. This gives the connection a little detour forcing it to go twice through this server, but it does get rid of the asymmetry allowing this to work.



    This solution relies on the ports used to tell which connections from the first rule are used by the second one. This will be fine with your simple setup, but for anyone trying to implement this in a router also having users behind it trying to access the same resource, it's worth noting that the MARK feature of netfilter should be used to tell the second MASQUERADE line which packets it should handle instead of just the port. Having MASQUERADE applied to everything could introduce unwanted behavior.






    share|improve this answer



























      0














      iptables -t nat -I PREROUTING -i eth1 -p tcp --dport 443 -j DNAT --to 192.168.0.1:943
      iptables -t nat -I POSTROUTING -o eth1 -p tcp -d 192.168.0.1 --dport 943 -j MASQUERADE


      Substitute 443 with the public port to listen to, 192.168.0.1 with your other server and 943 with the other server's listening port. Replace eth1 with your interface. Be aware that some configs have virtual interfaces, so something like vlan1 or ppp0 could be required.



      The first line is the basic rule forwarding the packets. But since the packets would have the source address intact, it would result in asymmetric routing which would break with this stateful netfilter. Hence comes the second line - replace the source address of the packet with one of this server. This gives the connection a little detour forcing it to go twice through this server, but it does get rid of the asymmetry allowing this to work.



      This solution relies on the ports used to tell which connections from the first rule are used by the second one. This will be fine with your simple setup, but for anyone trying to implement this in a router also having users behind it trying to access the same resource, it's worth noting that the MARK feature of netfilter should be used to tell the second MASQUERADE line which packets it should handle instead of just the port. Having MASQUERADE applied to everything could introduce unwanted behavior.






      share|improve this answer

























        0












        0








        0







        iptables -t nat -I PREROUTING -i eth1 -p tcp --dport 443 -j DNAT --to 192.168.0.1:943
        iptables -t nat -I POSTROUTING -o eth1 -p tcp -d 192.168.0.1 --dport 943 -j MASQUERADE


        Substitute 443 with the public port to listen to, 192.168.0.1 with your other server and 943 with the other server's listening port. Replace eth1 with your interface. Be aware that some configs have virtual interfaces, so something like vlan1 or ppp0 could be required.



        The first line is the basic rule forwarding the packets. But since the packets would have the source address intact, it would result in asymmetric routing which would break with this stateful netfilter. Hence comes the second line - replace the source address of the packet with one of this server. This gives the connection a little detour forcing it to go twice through this server, but it does get rid of the asymmetry allowing this to work.



        This solution relies on the ports used to tell which connections from the first rule are used by the second one. This will be fine with your simple setup, but for anyone trying to implement this in a router also having users behind it trying to access the same resource, it's worth noting that the MARK feature of netfilter should be used to tell the second MASQUERADE line which packets it should handle instead of just the port. Having MASQUERADE applied to everything could introduce unwanted behavior.






        share|improve this answer













        iptables -t nat -I PREROUTING -i eth1 -p tcp --dport 443 -j DNAT --to 192.168.0.1:943
        iptables -t nat -I POSTROUTING -o eth1 -p tcp -d 192.168.0.1 --dport 943 -j MASQUERADE


        Substitute 443 with the public port to listen to, 192.168.0.1 with your other server and 943 with the other server's listening port. Replace eth1 with your interface. Be aware that some configs have virtual interfaces, so something like vlan1 or ppp0 could be required.



        The first line is the basic rule forwarding the packets. But since the packets would have the source address intact, it would result in asymmetric routing which would break with this stateful netfilter. Hence comes the second line - replace the source address of the packet with one of this server. This gives the connection a little detour forcing it to go twice through this server, but it does get rid of the asymmetry allowing this to work.



        This solution relies on the ports used to tell which connections from the first rule are used by the second one. This will be fine with your simple setup, but for anyone trying to implement this in a router also having users behind it trying to access the same resource, it's worth noting that the MARK feature of netfilter should be used to tell the second MASQUERADE line which packets it should handle instead of just the port. Having MASQUERADE applied to everything could introduce unwanted behavior.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 10 at 19:24









        ZdenekZdenek

        1774




        1774



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Server Fault!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f841521%2fiptables-nat-and-port-forward-loop-with-one-network-interface%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company