10

I think it's a very classical question, but, thought I've have some background, I don't have enough vocabulary anymore to search and understand correctly in the Web.

Let's say I have a home network with 192.168.0.1 (IP.1) and 192.168.0.2 (IP.2) and we are both playing to Counter Strike hitting exactly the same outside server.

How my home router know wich packet will goes to IP.1 ? I often read stuff with NAT and xxx.xxx/y adresses. **Where is written the /y part in the TCP/IP stack ?

Do we need absolutely NAT for that ? What I have understood from Wikipedia article is that NAT is made to optimize IP addresses, when some computers are turned off. Does it also allow to connect 5 devices with only 1 public IP ?

  • One-to-Many should answer your questions. Also, home networking and entry-level-education questions, aren't the best fit for this site. – Craig Constantine Jun 2 '14 at 15:10
14

(For the following I will ignore any DNS lookups or layer two action, since that isn't the relevant part for the NAT story.)

Any TCP connection is a tuple of four parts:

<source IP> <source port> <destination IP> <destination port>

In short: the destination IP is used to get the packet to the correct machine, the destination port is used to get the packet on that machine to the correct program/session The source IP is used to know where to send any replies. Same goes for source port. When a reply is sent, the source and destination are simply swapped.

Let's start with two computers without any NAT:

  • The computer has IP 1.1.1.1
  • The webserver has IP 3.3.3.3
  • The standard port for HTTP is 80

When a computer asks for a webpage, it wil first select a random unused portnumber from the random range (1024-65535). Let's pick 2345. Then the following sequence will happen: Computer send it's packet with: source IP 1.1.1.1, source port 2345, destination IP 3.3.3.3, destination port 80. The packets arrive at the webserver, it sees it's own IP and port 80, so it knows this is a request for a webpage. The webserver then sends the webpage back in packets with Source IP 3.3.3.3, Source port 80, destination IP 1.1.1.1, destination port 2345. The computer recieves these packets, and knows wich requested webpage it was, because of the portnumber 2345.

These port combinations are often written as such: 1.1.1.1:2345 and 3.3.3.3:80.

Now, the number of computers on the internet far outnumbers the number of IPv4 addresses available. To preserve address space, a set of private address ranges was introduced, that can be freely used for address sharing. These rangese are referred to as RFC1918 and are the following:

  • 192.168.0.0 - 192.168.255.255
  • 172.16.0.0 - 172.31.255.255
  • 10.0.0.0 - 10.255.255.255

These addresses are nowhere in the internet routing tables, so if you would send a packet with a destination in these ranges on the backbone of the internet, they will simply get dropped. This is because millions of people use the same addresses. These addresses need to be translated to something useful for the internet. This is where Network Address Translation comes in:

We have two computers:

  • A: 192.168.0.1 and B: 192.168.0.2
  • Their gateway has a public IP of 1.1.1.1.
  • We keep the same webserver.
  • Both computers want the same webpage from the same server.

First both computers select a random port: let's say: 192.168.0.1:2345 and 192.168.0.2:5432.

Computer A send it's packet with source 192.168.0.1:2345 and destination 3.3.3.3:80. The gateway translates this packet to source 1.1.1.1:2345 destination 3.3.3.3:80 and remembers that any replies to this combination goes to 192.168.0.1. So, when it recieves a reply with source 3.3.3.3:80 and destination 1.1.1.1:2345, it will translate it to source 3.3.3.3:80 and destination 192.168.0.1:2345 and send the packet on.

Computer B send it's packet with source 192.168.0.2:5432 and destination 3.3.3.3:80. The gateway translates this packet to source 1.1.1.1:5432 destination 3.3.3.3:80 and remembers that any replies to this combination goes to 192.168.0.2. So, when it recieves a reply with source 3.3.3.3:80 and destination 1.1.1.1:5432, it will translate it to source 3.3.3.3:80 and destination 192.168.0.2:5432 and send the packet on.

If both computers happen to select the same source port number, the gateway will simply pick another free random source port number, and remember to translate the port number as well. This is sometimes refered to as PAT (Port Address Translation). This is basically a subset of NAT.

There are several implementations to this all. The gateway might simply only remember "Computer X used source port Y" and forward anything with port Y to computer X. It might remember that Computer X used source port Y and destination Z" and only forward anything from port Z to port Y back to computer X. Or there is the option that it remembers the entire tuple and only send traffic to computer X that match the entire source/destination ip and port.

  • Thanks. I thought that any client would send with its port 80 to another port 80. I think that a lot of non-specialist believe the same. – Nicolas Zozol Jun 2 '14 at 20:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.