Nat – Why does NAT translate a source port

layer4nat;

I’m beginning to learn about nat and I was wondering why does NAT translate a source port? Doesn’t a port represent an application that’s requesting a service? So why must it be translated? Also what would happen if NAT hypothetically didn’t translate the source port and two separate machines on the same network sent out a message but had the same source port? Would anything bad happen?

Best Answer

First, I'm assuming you're focused on TCP. UDP has some differences, and I'm not as up-to-speed on that part.

I’m beginning to learn about nat and I was wondering why does NAT translate a source port?

You're right to ask this question, and to be frank, it doesn't always need to. However, sometimes that translation IS required. Given that it is sometimes required, and given that the NAT system therefore needs to track source port for some traffic, and because there are efficiencies in doing something the same way every time, most NAT implementations don't make an effort to re-use the original source-port on the NATted connection.

Many (most?) protocols don't depend on the source port for TCP connections, so that's the simplest approach, and it rarely hurts.

Doesn’t a port represent an application that’s requesting a service?

It does, but generally the source port is not specified by the application. Instead it is assigned (somewhat) randomly. This is actually good, because before that was true it was way too easy to break existing connections by predicting the source port number (the "unreach" attack using ICMP DESTINATION UNREACHABLE packets).

So why must it be translated? Also what would happen if NAT hypothetically didn’t translate the source port and two separate machines on the same network sent out a message but had the same source port?

The answer to your first question is in the answer to the second, so I'll take a stab at the second, and you share if it doesn't answer the first.

First, assume your NAT host only has one IP address (N) to translate to. Second, assume you actually have an extra coincidence where internal hosts A and B both try to communicate with external host X on port 80 with source port 17835.

  • A's packets (when generated, before NAT) look like: src: A:17835, dst: X:80.
  • B's packets (when generated, before NAT) look like: src: B:17835, dst: X:80.

After NAT, assuming no translation of source port:

  • A's packets (after NAT) look like: src: N:17835, dst: X:80.
  • B's packets (after NAT) look like: src: N:17835, dst: X:80.

Oops. They look the same. They particularly look the same to the remote host X. It is most likely to drop the packets associated with the second connection attempt because the sequence numbers are going to be wrong.

You also have a problem on the NAT host, as it can't tell the difference between the two either. It can only maintain one "connection" record with remote host X on port 80 per source port. It has to keep this record so it knows which internal host to translate back to when it receives an inbound packet. If the record corresponds to the first host to connect, then you would have the experience where A (the first host) has no problems, and B cannot connect.

If, more entertainingly, B's record in the NAT overwrites A's after the connection is built for A, then the NAT system will never forward X's responses to A (it only has B's record), and X will never respond to B (wrong sequence numbers / connection state) and nobody wins (communicates).

Let's be honest. NAT is a hack. It is an ugly hack, and we are either lucky it works (because we haven't rolled out IPv6 universally yet) or victims of its success (because it works well enough, people don't insist on IPv6 support).

Related Topic