Use iptables as router

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
3
down vote

favorite












I am having the following setup:



schematic overview of setup



client: 
192.168.103.55

"router"
192.168.103.30 (eth3), 192.168.102.30 (eth2)

server-1 server-2
192.168.102.21 (eth2) 192.168.102.22 (eth2)


The router is actually a small linux machine, running iptables.



The goal is to configure iptables in such a way that I can ping from the servers (192.168.102.21 and .22) to the client (192.168.103.55), via the router (192.168.102.30 at server-side and 192.168.103.30 at client-side)



The servers have a route configured as:



192.168.103.55 via 192.168.102.30 dev eth2 


On the router I have configured the following rules in IP-tables:



*filter
:INPUT ACCEPT [1610193:248234329]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1945999:238163662]
-A FORWARD -i eth3 -j ACCEPT
-A FORWARD -o eth3 -j ACCEPT
-A FORWARD -i eth2 -j ACCEPT
-A FORWARD -o eth2 -j ACCEPT
COMMIT
# Completed on Wed May 2 08:26:55 2018
# Generated by iptables-save v1.4.21 on Wed May 2 08:26:55 2018
*nat
:PREROUTING ACCEPT [5610:715368]
:INPUT ACCEPT [2029:121740]
:OUTPUT ACCEPT [326029:19788110]
:POSTROUTING ACCEPT [326029:19788110]
-A POSTROUTING -d 192.168.103.55/32 -o eth3 -j SNAT --to-source 192.168.103.30
COMMIT


The ping from 192.168.102.21 is reaching the client (192.168.103.55), and the client sends the reply towards 192.168.103.30. But the reply is not forwarded towards to the client (.55). It gets stuck in the router



What am I missing here?



Thanks in advance for your time!



=================== UPDATE ===================



Guntbert suggestion on The forwarding was already done.



The SNAT-rule is required in order for the router to know to which server to route back the ICMP Reply.
In my first explanation I left out the second server. I updated the question now to give you the full overview.



The SNAT seems to work somehow, since I can see the ICMP-reply packet going to the correct server:



[router ~]# tcpdump -ni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes

08:38:26.110245 IP 192.168.102.21 > 192.168.103.55: ICMP echo request, id 4471, seq 1, length 64
08:38:26.112722 IP 192.168.103.55 > 192.168.102.21: ICMP echo reply, id 4471, seq 1, length 64

08:39:53.238281 IP 192.168.102.22 > 192.168.103.55: ICMP echo request, id 8285, seq 1, length 64
08:39:53.239110 IP 192.168.103.55 > 192.168.102.22: ICMP echo reply, id 8285, seq 1, length 64


What actually should happen next, I think, is that the source IP (192.168.103.55) in the reply needs to be replaced by the router's IP (192.168.102.30).
Or am I wrong?










share|improve this question



























    up vote
    3
    down vote

    favorite












    I am having the following setup:



    schematic overview of setup



    client: 
    192.168.103.55

    "router"
    192.168.103.30 (eth3), 192.168.102.30 (eth2)

    server-1 server-2
    192.168.102.21 (eth2) 192.168.102.22 (eth2)


    The router is actually a small linux machine, running iptables.



    The goal is to configure iptables in such a way that I can ping from the servers (192.168.102.21 and .22) to the client (192.168.103.55), via the router (192.168.102.30 at server-side and 192.168.103.30 at client-side)



    The servers have a route configured as:



    192.168.103.55 via 192.168.102.30 dev eth2 


    On the router I have configured the following rules in IP-tables:



    *filter
    :INPUT ACCEPT [1610193:248234329]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [1945999:238163662]
    -A FORWARD -i eth3 -j ACCEPT
    -A FORWARD -o eth3 -j ACCEPT
    -A FORWARD -i eth2 -j ACCEPT
    -A FORWARD -o eth2 -j ACCEPT
    COMMIT
    # Completed on Wed May 2 08:26:55 2018
    # Generated by iptables-save v1.4.21 on Wed May 2 08:26:55 2018
    *nat
    :PREROUTING ACCEPT [5610:715368]
    :INPUT ACCEPT [2029:121740]
    :OUTPUT ACCEPT [326029:19788110]
    :POSTROUTING ACCEPT [326029:19788110]
    -A POSTROUTING -d 192.168.103.55/32 -o eth3 -j SNAT --to-source 192.168.103.30
    COMMIT


    The ping from 192.168.102.21 is reaching the client (192.168.103.55), and the client sends the reply towards 192.168.103.30. But the reply is not forwarded towards to the client (.55). It gets stuck in the router



    What am I missing here?



    Thanks in advance for your time!



    =================== UPDATE ===================



    Guntbert suggestion on The forwarding was already done.



    The SNAT-rule is required in order for the router to know to which server to route back the ICMP Reply.
    In my first explanation I left out the second server. I updated the question now to give you the full overview.



    The SNAT seems to work somehow, since I can see the ICMP-reply packet going to the correct server:



    [router ~]# tcpdump -ni eth2 icmp
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes

    08:38:26.110245 IP 192.168.102.21 > 192.168.103.55: ICMP echo request, id 4471, seq 1, length 64
    08:38:26.112722 IP 192.168.103.55 > 192.168.102.21: ICMP echo reply, id 4471, seq 1, length 64

    08:39:53.238281 IP 192.168.102.22 > 192.168.103.55: ICMP echo request, id 8285, seq 1, length 64
    08:39:53.239110 IP 192.168.103.55 > 192.168.102.22: ICMP echo reply, id 8285, seq 1, length 64


    What actually should happen next, I think, is that the source IP (192.168.103.55) in the reply needs to be replaced by the router's IP (192.168.102.30).
    Or am I wrong?










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I am having the following setup:



      schematic overview of setup



      client: 
      192.168.103.55

      "router"
      192.168.103.30 (eth3), 192.168.102.30 (eth2)

      server-1 server-2
      192.168.102.21 (eth2) 192.168.102.22 (eth2)


      The router is actually a small linux machine, running iptables.



      The goal is to configure iptables in such a way that I can ping from the servers (192.168.102.21 and .22) to the client (192.168.103.55), via the router (192.168.102.30 at server-side and 192.168.103.30 at client-side)



      The servers have a route configured as:



      192.168.103.55 via 192.168.102.30 dev eth2 


      On the router I have configured the following rules in IP-tables:



      *filter
      :INPUT ACCEPT [1610193:248234329]
      :FORWARD ACCEPT [0:0]
      :OUTPUT ACCEPT [1945999:238163662]
      -A FORWARD -i eth3 -j ACCEPT
      -A FORWARD -o eth3 -j ACCEPT
      -A FORWARD -i eth2 -j ACCEPT
      -A FORWARD -o eth2 -j ACCEPT
      COMMIT
      # Completed on Wed May 2 08:26:55 2018
      # Generated by iptables-save v1.4.21 on Wed May 2 08:26:55 2018
      *nat
      :PREROUTING ACCEPT [5610:715368]
      :INPUT ACCEPT [2029:121740]
      :OUTPUT ACCEPT [326029:19788110]
      :POSTROUTING ACCEPT [326029:19788110]
      -A POSTROUTING -d 192.168.103.55/32 -o eth3 -j SNAT --to-source 192.168.103.30
      COMMIT


      The ping from 192.168.102.21 is reaching the client (192.168.103.55), and the client sends the reply towards 192.168.103.30. But the reply is not forwarded towards to the client (.55). It gets stuck in the router



      What am I missing here?



      Thanks in advance for your time!



      =================== UPDATE ===================



      Guntbert suggestion on The forwarding was already done.



      The SNAT-rule is required in order for the router to know to which server to route back the ICMP Reply.
      In my first explanation I left out the second server. I updated the question now to give you the full overview.



      The SNAT seems to work somehow, since I can see the ICMP-reply packet going to the correct server:



      [router ~]# tcpdump -ni eth2 icmp
      tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
      listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes

      08:38:26.110245 IP 192.168.102.21 > 192.168.103.55: ICMP echo request, id 4471, seq 1, length 64
      08:38:26.112722 IP 192.168.103.55 > 192.168.102.21: ICMP echo reply, id 4471, seq 1, length 64

      08:39:53.238281 IP 192.168.102.22 > 192.168.103.55: ICMP echo request, id 8285, seq 1, length 64
      08:39:53.239110 IP 192.168.103.55 > 192.168.102.22: ICMP echo reply, id 8285, seq 1, length 64


      What actually should happen next, I think, is that the source IP (192.168.103.55) in the reply needs to be replaced by the router's IP (192.168.102.30).
      Or am I wrong?










      share|improve this question















      I am having the following setup:



      schematic overview of setup



      client: 
      192.168.103.55

      "router"
      192.168.103.30 (eth3), 192.168.102.30 (eth2)

      server-1 server-2
      192.168.102.21 (eth2) 192.168.102.22 (eth2)


      The router is actually a small linux machine, running iptables.



      The goal is to configure iptables in such a way that I can ping from the servers (192.168.102.21 and .22) to the client (192.168.103.55), via the router (192.168.102.30 at server-side and 192.168.103.30 at client-side)



      The servers have a route configured as:



      192.168.103.55 via 192.168.102.30 dev eth2 


      On the router I have configured the following rules in IP-tables:



      *filter
      :INPUT ACCEPT [1610193:248234329]
      :FORWARD ACCEPT [0:0]
      :OUTPUT ACCEPT [1945999:238163662]
      -A FORWARD -i eth3 -j ACCEPT
      -A FORWARD -o eth3 -j ACCEPT
      -A FORWARD -i eth2 -j ACCEPT
      -A FORWARD -o eth2 -j ACCEPT
      COMMIT
      # Completed on Wed May 2 08:26:55 2018
      # Generated by iptables-save v1.4.21 on Wed May 2 08:26:55 2018
      *nat
      :PREROUTING ACCEPT [5610:715368]
      :INPUT ACCEPT [2029:121740]
      :OUTPUT ACCEPT [326029:19788110]
      :POSTROUTING ACCEPT [326029:19788110]
      -A POSTROUTING -d 192.168.103.55/32 -o eth3 -j SNAT --to-source 192.168.103.30
      COMMIT


      The ping from 192.168.102.21 is reaching the client (192.168.103.55), and the client sends the reply towards 192.168.103.30. But the reply is not forwarded towards to the client (.55). It gets stuck in the router



      What am I missing here?



      Thanks in advance for your time!



      =================== UPDATE ===================



      Guntbert suggestion on The forwarding was already done.



      The SNAT-rule is required in order for the router to know to which server to route back the ICMP Reply.
      In my first explanation I left out the second server. I updated the question now to give you the full overview.



      The SNAT seems to work somehow, since I can see the ICMP-reply packet going to the correct server:



      [router ~]# tcpdump -ni eth2 icmp
      tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
      listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes

      08:38:26.110245 IP 192.168.102.21 > 192.168.103.55: ICMP echo request, id 4471, seq 1, length 64
      08:38:26.112722 IP 192.168.103.55 > 192.168.102.21: ICMP echo reply, id 4471, seq 1, length 64

      08:39:53.238281 IP 192.168.102.22 > 192.168.103.55: ICMP echo request, id 8285, seq 1, length 64
      08:39:53.239110 IP 192.168.103.55 > 192.168.102.22: ICMP echo reply, id 8285, seq 1, length 64


      What actually should happen next, I think, is that the source IP (192.168.103.55) in the reply needs to be replaced by the router's IP (192.168.102.30).
      Or am I wrong?







      iptables router






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 8 at 18:25









      guntbert

      8,750123067




      8,750123067










      asked Apr 13 at 11:42









      Radje

      162




      162




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote














          1. you need to enable packet forwarding in the kernel




            • edit /etc/sysctl.conf and activate the line



              net.ipv4.ip_forward=1


              by removing the # at the beginning.




            • reboot or enable the setting immediately with



              echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward




          2. Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your iptables-rules.



            • remove the NAT rule from the router

            • configure all systems to reach the "other" network via the router

              • on the servers replace your route with
                192.168.103.0/24 via 192.168.102.30 dev eth2

                (this tells them how to reach the whole network, not just one client)

              • on the client add the following route
                192.168.102.0/24 via 192.168.103.30 dev eth0



          Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.






          share|improve this answer






















          • Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
            – Radje
            May 2 at 6:56










          • see above the update questions and additional findings
            – Radje
            May 2 at 7:06










          • @Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
            – guntbert
            May 8 at 18:48










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          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',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1024638%2fuse-iptables-as-router%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote














          1. you need to enable packet forwarding in the kernel




            • edit /etc/sysctl.conf and activate the line



              net.ipv4.ip_forward=1


              by removing the # at the beginning.




            • reboot or enable the setting immediately with



              echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward




          2. Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your iptables-rules.



            • remove the NAT rule from the router

            • configure all systems to reach the "other" network via the router

              • on the servers replace your route with
                192.168.103.0/24 via 192.168.102.30 dev eth2

                (this tells them how to reach the whole network, not just one client)

              • on the client add the following route
                192.168.102.0/24 via 192.168.103.30 dev eth0



          Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.






          share|improve this answer






















          • Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
            – Radje
            May 2 at 6:56










          • see above the update questions and additional findings
            – Radje
            May 2 at 7:06










          • @Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
            – guntbert
            May 8 at 18:48














          up vote
          2
          down vote














          1. you need to enable packet forwarding in the kernel




            • edit /etc/sysctl.conf and activate the line



              net.ipv4.ip_forward=1


              by removing the # at the beginning.




            • reboot or enable the setting immediately with



              echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward




          2. Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your iptables-rules.



            • remove the NAT rule from the router

            • configure all systems to reach the "other" network via the router

              • on the servers replace your route with
                192.168.103.0/24 via 192.168.102.30 dev eth2

                (this tells them how to reach the whole network, not just one client)

              • on the client add the following route
                192.168.102.0/24 via 192.168.103.30 dev eth0



          Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.






          share|improve this answer






















          • Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
            – Radje
            May 2 at 6:56










          • see above the update questions and additional findings
            – Radje
            May 2 at 7:06










          • @Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
            – guntbert
            May 8 at 18:48












          up vote
          2
          down vote










          up vote
          2
          down vote










          1. you need to enable packet forwarding in the kernel




            • edit /etc/sysctl.conf and activate the line



              net.ipv4.ip_forward=1


              by removing the # at the beginning.




            • reboot or enable the setting immediately with



              echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward




          2. Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your iptables-rules.



            • remove the NAT rule from the router

            • configure all systems to reach the "other" network via the router

              • on the servers replace your route with
                192.168.103.0/24 via 192.168.102.30 dev eth2

                (this tells them how to reach the whole network, not just one client)

              • on the client add the following route
                192.168.102.0/24 via 192.168.103.30 dev eth0



          Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.






          share|improve this answer















          1. you need to enable packet forwarding in the kernel




            • edit /etc/sysctl.conf and activate the line



              net.ipv4.ip_forward=1


              by removing the # at the beginning.




            • reboot or enable the setting immediately with



              echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward




          2. Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your iptables-rules.



            • remove the NAT rule from the router

            • configure all systems to reach the "other" network via the router

              • on the servers replace your route with
                192.168.103.0/24 via 192.168.102.30 dev eth2

                (this tells them how to reach the whole network, not just one client)

              • on the client add the following route
                192.168.102.0/24 via 192.168.103.30 dev eth0



          Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 26 at 18:15









          dessert

          19.8k55594




          19.8k55594










          answered Apr 13 at 21:44









          guntbert

          8,750123067




          8,750123067











          • Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
            – Radje
            May 2 at 6:56










          • see above the update questions and additional findings
            – Radje
            May 2 at 7:06










          • @Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
            – guntbert
            May 8 at 18:48
















          • Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
            – Radje
            May 2 at 6:56










          • see above the update questions and additional findings
            – Radje
            May 2 at 7:06










          • @Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
            – guntbert
            May 8 at 18:48















          Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
          – Radje
          May 2 at 6:56




          Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
          – Radje
          May 2 at 6:56












          see above the update questions and additional findings
          – Radje
          May 2 at 7:06




          see above the update questions and additional findings
          – Radje
          May 2 at 7:06












          @Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
          – guntbert
          May 8 at 18:48




          @Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
          – guntbert
          May 8 at 18:48

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1024638%2fuse-iptables-as-router%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          pylint3 and pip3 broken

          Missing snmpget and snmpwalk

          How to enroll fingerprints to Ubuntu 17.10 with VFS491