3

When my PC sends a request to a server, the transmitted IP address is the one of my private network. When the server responds to the request, how does the router know to which PC in the network to send the response?

Ron Maupin
  • 99,565
  • 26
  • 120
  • 195

1 Answers1

4

The NAT process maintains the translations tables. When the client sends to the server, NAT creates a table entry which saves the fact that the client IP address and port sent something to the server IP address address and port. When something comes back from the server address and port to the public address and NAT selected port, NAT looks it up and sees that it should go to the client address and port.

Ron Maupin
  • 99,565
  • 26
  • 120
  • 195
  • Thanks, but what happens if two local devices choose the same ephemeral source port while connecting to the same target IP and port (e.g. google.com:80)? Does router then change the source port number too in order to distinguish the two sockets, and then rewrites it back when forwarding to the right PC? – Lou Jun 26 '17 at 21:29
  • There are several forms of NAT. What you are implying is called NAPT (Network Address Port Translation), which translates both the layer-3 and layer-4 network addresses. In fact all four addresses (source and destination IP and TCP/UDP/ICMP) addresses combine for a NAT table entry. – Ron Maupin Jun 26 '17 at 21:51
  • 1
    But isn't this common for all routers? If I have two computers behind a router and they both visit the same web page, then on the TCP level they are likely connecting to the same IP:port tuple? So I don't understand what you meant by "what you are implying", I thought this was the general, everyday case for any router out there? – Lou Jul 06 '17 at 09:25
  • The default for all routers is no NAT. Unless your two PCs both have the same source address and are using the same source port, both of which are highly unlikely, then, no, they do not have the same four addresses in the connection with a server in common, NAT or no NAT. – Ron Maupin Jul 06 '17 at 15:34
  • They won't have the same source address, but the source port might get equal. Perhaps I am mistaken, but I thought router will rewrite the IP packet and place its external source address in the IP packet. So when the response packet arrives, it will be addressed to the router, not to the internal machine's IP. This means that router needs to forward the packet based on the port number only, unless the response packet somehow contains the local IP information. – Lou Jul 11 '17 at 23:20
  • By default routers simply route packets based on the destination IP address, and they only decrement the TTL and possibly fragment a packet if the MTU for the next interface is too small and the DF bit is not set. You are describing NAT, which is a kludge to try to extend IPv4 addressing until IPv6 becomes ubiquitous, and that is an option on most routers, but it is not a router requirement, and it can be a process on other device types, too. If you involve layer-4 addressing, e.g. ports, then it is NAPT. Port numbers are addresses for some layer-4 protocols, but routing doesn't use that. – Ron Maupin Jul 11 '17 at 23:32
  • The last response to this just became jibberish. You might say, "Learn Networking then.". But this is us learning.

    I'll set some terminology (because I don't know what they're called yet) client = my pc or phone router = the device that has a WAN ip and connects to internet destination = another WAN ip like google.com

    When the destination responds, it will address that to the router's WAN address + client's assigned ephemeral port (CAEP). The NAT knows what client it belongs to because of the CAEP.

    Now, I think what @Lou is asking, who assigns that CAEP?

    – Hunkoys Dec 15 '23 at 23:41
  • If it's the client? Then what's stoping another client from picking (probably low odds) the same CAEP? The NAT table will then look like:
    Client IP CAEP Destination IP + Port
    192.168.0.1 10000 google.com:443
    192.168.0.2 10000 google.com:443

    Given:

    • WAN ip = 123.123.123.123 (example only)

    How does NAT know which client to forward the response to if it got a reponse from google to 123.123.123.123:10000?

    – Hunkoys Dec 15 '23 at 23:55
  • All this because the we're assuming the response only contains the WAN address and CAEP. Unless we misunderstood. – Hunkoys Dec 15 '23 at 23:55
  • this answer is severely lacking. The most obvious 'edge case', that immediately gets beginners distraught as laid out in the comments, is not handled. For a good answer, read this: https://networkengineering.stackexchange.com/questions/17439/how-does-the-router-know-how-to-route-the-packets-to-my-terminal – loonquawl Mar 28 '24 at 10:10