So I am learning about NAT. I understand that it changes IP from public>private and vice versa. From my understanding, when a packet or info gets sent to the public ip towards the private ip, through the router, how does the packet know which private ip to attend to, if each private ip is covered by the same public IP. i know i sound super elementary, but i just want to try to understand this. Im assuming the packet has the info about the private IP? and if so, isnt that a flaw?
1 Answer
when a packet or info gets sent to the public ip towards the private ip, through the router, how does the packet know which private ip to attend to
Source NAT/NAPT
For a connection that has been established from the private LAN to the public Internet (source NAT), the NAT router uses its dynamic translation table to translate the shared public IPv4 address to the required private IP.
Example:
- private client sends a packet with TCP SYN from 172.16.1.100:49153 (ephemeral port) to public 192.0.2.79:80 (http)
- NAT router translates source IP:port 172.16.1.100:49153 to its public 198.51.100.10:22777 (port allocated dynamically from NAT pool)
- NAT router stores that translation in its NAT table and forwards the packet to 192.0.2.79 to public WAN
- server responds to 198.51.100.10:22777 with SYN/ACK
- NAT router translates the destination IP:port 198.51.100.10:22777 back to 172.16.1.100:49153 and forwards the packet to private LAN
- client receives the packet and can proceed to establish the socket connect and to request a web page from the server
Since we're translating the source port in addition to the source IP address it's actually NAPT, the most common form of NAT.
Destination NAT
For a connection that is established from the public Internet to a server inside the private LAN (destination NAT aka reverse NAT aka port forwarding), that translation requires to map a router's public IP:port combination to the server's private IP:port statically.
Example:
- public client with IP:port 203.0.113.0:50001 sends a packet with TCP SYN to 198.51.100.10:80
- NAT router has a static port forwarding setting to translate that IP:port for a server to 172.16.2.5:8080
- it translates the destination address accordingly and forwards to private LAN
- server responds with SYN/ACK
- NAT router translates the source IP:port 172.16.2.5:8080 back to 198.51.100.10:80 and forwards to public WAN
- client receives SYN/ACK etc.
You'll notice that the translation is in reverse to source NAT, hence the name reverse NAT.
Im assuming the packet has the info about the private IP?
No, that's not possible. The source cannot control where a NAT router maps its connection.
-
" that translation requires port forwarding to map a router's public IP:port combination to the server's private IP:port statically." a little over my head, i understand the idea of reverse NAT. not how it works though. Is there any way to put that in Lamens terms? If not, no worries. Commented Aug 24, 2023 at 16:00
-
Just watch this: youtube.com/watch?v=_0AlFIEXkQI There are more complicated videos and you can get into a lot of different specific options with NAT but the basic is that for commonly used 'overload' or Port Address Tranlsation NAT, it works based on a device on the 'inside' network initiating a connection to the 'outside' and the NAT router facilitates that and allows for reply traffic for that specific connection. Any connections not related to that connection are ignored or blocked. Commented Aug 24, 2023 at 18:54