Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Linux IPTables NAT to pfSense NAT

    Scheduled Pinned Locked Moved NAT
    12 Posts 3 Posters 1.2k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      viragomann @thomasyuan
      last edited by viragomann

      @thomasyuan
      Port forwarding = DNAT
      Outbound NAT = SNAT

      Note that port forwarding rules are applied at first, when a packets enter pfSense on the interface the rule is on, while outbound NAT is applied at last, before the packet exits pfSense from on the interface the rule is on,.

      I did'nt understand, what you want to translate here, however, maybe you can explain more clearly.

      T 1 Reply Last reply Reply Quote 0
      • T
        thomasyuan @viragomann
        last edited by thomasyuan

        @viragomann
        Your explanation is same as my understanding.
        here is the topology:

                        pfSense LAN 172.16.0.1 with Virtual IP 192.168.0.1
                                               |
                                             Switch 
                                               |
                                +--------------+------------+
                                |                           |
                     Laptop 192.168.0.100          Device 172.16.0.100
        

        What I want is, when I try to access 1.2.3.4 from my laptop, pfSense can do DNAT and SNAT, which make it looks like this:

        192.168.0.100 -> 1.2.3.4
        Apply DNAT rule => 192.168.0.100 -> 172.16.0.100
        If no DNAT, then the package will route to WAN (default gateway), but now, it should route to LAN interface.
        Apply SNAT rule. => 172.16.0.1 -> 172.16.0.100

        V 1 Reply Last reply Reply Quote 0
        • T
          thomasyuan
          last edited by

          Tried packet capture.
          Seems the issue is the DNAT not working as I expected.
          When I ping 1.2.3.4 from 192.168.0.100, the DNAT rule should change the destination to 172.16.0.100, but it actually goes to WAN.
          I captured the ICMP on WAN interface, and the source ip has been changed to external IP, and the target didn't change.

          S 1 Reply Last reply Reply Quote 0
          • S
            SteveITS Galactic Empire @thomasyuan
            last edited by

            @thomasyuan So you're trying to capture traffic from laptop 192.168.0.100 intended for the external IP 1.2.3.4 and send it to 172.16.0.100?

            This may help, it's describing capturing and redirecting DNS requests on port 53:
            https://docs.netgate.com/pfsense/en/latest/recipes/dns-redirect.html

            "invert LAN Address" would be "any other DNS server" but in your case the destination would be 1.2.3.4.

            Pre-2.7.2/23.09: Only install packages for your version, or risk breaking it. Select your branch in System/Update/Update Settings.
            When upgrading, allow 10-15 minutes to restart, or more depending on packages and device speed.
            Upvote šŸ‘ helpful posts!

            T 1 Reply Last reply Reply Quote 0
            • V
              viragomann @thomasyuan
              last edited by

              @thomasyuan said in Linux IPTables NAT to pfSense NAT:

              192.168.0.100 -> 1.2.3.4
              Apply DNAT rule => 192.168.0.100 -> 172.16.0.100
              If no DNAT, then the package will route to WAN (default gateway), but now, it should route to LAN interface.
              Apply SNAT rule. => 172.16.0.1 -> 172.16.0.100

              So if you call 1.2.3.4 on the laptop it should go to 172.16.0.100, as I understood.

              This would need a simple port forwarding rule on the LAN.
              Select the LAN interface, at source state 192.168.0.100, at destination enter 1.2.3.4 and at redirect target 172.16.0.100.

              An outbound NAT rule (SNAT) is only necessary if the destination device has no default gateway settings or even use another default gateway than pfSense.
              In this case switch the outbound NAT into hybrid mode and add a rule:
              interface: LAN
              source: *
              destination: 172.16.0.100
              translation: interface address (as I got it, this is 172.16.0.1)

              T 1 Reply Last reply Reply Quote 0
              • T
                thomasyuan @viragomann
                last edited by thomasyuan

                @viragomann
                yes, I need the SNAT because I can NOT change anything on the device, the devices only has fixed IP, and no default gateway.

                And I believe your suggestion for the SNAT rule is exactly what I did. (did spend sometime to figure out why the SNAT is grayed out)

                V 1 Reply Last reply Reply Quote 0
                • T
                  thomasyuan @SteveITS
                  last edited by

                  @SteveITS
                  Thanks for the reference link. I wasn’t sure what’s NAT reflection before as I mentioned I an newbie to pfSense. After reviewing some explanations, I feel maybe I don’t need the SNAT, just need to set the NAT reflection to Proxy? Will try and update later.

                  one comment from reddit

                  DutchOfBurdock
                  •
                  4 yr. ago
                  
                  It's a facility of NAT to allow LAN hosts to make connections to forwarded ports on WAN. In most circumstances, LAN hosts will access LAN hosts via LAN. However, in some cases, you need to access the services via the WAN (TLS certificate in a public facing web server, for example) IP. NAT would normally drop these connections.
                  
                  Reflection changes that behaviour and will forward the connections to the forwarded IP. There are two modes for this, Proxy and Pure. Pure is generally better; especially for game servers or services requiring large port ranges and UDP. Proxy is useful for TCP only servers, but has the benefit of making the connection appear to come from the Firewall (LAN) IP.
                  
                  
                  1 Reply Last reply Reply Quote 0
                  • V
                    viragomann @thomasyuan
                    last edited by

                    @thomasyuan
                    And you say, even though you added these two NAT rule correctly, the packets to 1.2.3.4 is directed out to the WAN gateway?

                    When I ping 1.2.3.4 from 192.168.0.100, the DNAT rule should change the destination to 172.16.0.100, but it actually goes to WAN.
                    I captured the ICMP on WAN interface, and the source ip has been changed to external IP, and the target didn't change.

                    So I'd assume, that your LAN interface network mask is wrong.

                    Can you post some screenshot, please, so that we can see your setting rather then stepping in the dark:
                    Status > interfaces
                    NAT Port forward
                    NAT outbound

                    @thomasyuan said in Linux IPTables NAT to pfSense NAT:

                    I feel maybe I don’t need the SNAT, just need to set the NAT reflection to Proxy?

                    NAT reflection mirrors NAT rules from WAN to the internal interfaces.So internal devices are enabled to access other internal destinations with the public IP.
                    I don't expect, that this does anything in your setup.

                    1 Reply Last reply Reply Quote 0
                    • T
                      thomasyuan
                      last edited by thomasyuan

                      @viragomann
                      I didn't use the real ip address when I describe the issue.
                      You can replace 192.168.0.x to 192.168.66.x, 1.2.3.4 to 50.112.4.100, and 172.16.0.x to 172.16.4.x.
                      Here is the screenshot of the NAT.

                      Screenshot 2023-11-11 at 9.55.41 PM.png

                      Screenshot 2023-11-11 at 9.58.59 PM.png

                      Screenshot 2023-11-11 at 10.03.12 PM.png

                      Screenshot 2023-11-11 at 10.00.46 PM.png

                      I think right now, the major issue is, seems the port forwarding is not applied for some reason.

                      1 Reply Last reply Reply Quote 0
                      • T
                        thomasyuan
                        last edited by

                        When I ping 52.112.4.100 from my laptop, I can see below packet from LAN

                        03:19:05.100249 IP 192.168.66.210 > 52.112.4.100: ICMP echo request, id 1, seq 2471, length 40
                        

                        And below packet from WAN (192.168.99.99 is WAN IP address)

                        03:21:08.179184 IP 192.168.99.99 > 52.112.4.100: ICMP echo request, id 36355, seq 2472, length 40
                        

                        I feel the Port Forward is not working, so the destination didn't change to 172.16.4.100, so the packet will route to WAN instead of LAN, which then cause the Outbound rule can't match because it was applied on LAN interface.

                        1 Reply Last reply Reply Quote 0
                        • T
                          thomasyuan
                          last edited by

                          I am stupid.
                          The Port Forward rule was wrong.
                          It should be 192.168.66.0/24 network, but I selected single host and give the ip address as 192.168.66.0. šŸ˜…

                          Thanks @viragomann and @SteveITS !

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.