Nat – How does Pinging from a private IP behind NAT to a public IP work

nat;ping

Recently I realized that ICMP does not contain any ports, so I tried to Ping from my wireless connected laptop to public IPs(of course it worked!).

In a typical configuration, a local network uses one of the designated "private" IP address subnets (RFC 1918). A router on that network has a private address in that address space. The router is also connected to the Internet with a "public" address assigned by an Internet service provider. As traffic passes from the local network to the Internet, the source address in each packet is translated on the fly from a private address to the public address. The router tracks basic data about each active connection (particularly the destination address and port).

Does my router have a special space for the ICMP packets?

ICMP HEADER

It only has 4 fields: "type", "code", "checksum" and "data". Only adding extra data seems a feasible solution. Please provide sources when answering as I couldn't find them. Thank you for your time:)

Best Answer

The missing piece of information here is the Identifier (aka. query ID) which lives in the 8-byte ICMP header - see https://en.wikipedia.org/wiki/Ping_(networking_utility)#Echo_request. When multiple inside hosts ping the same outside host simultaneously they will most likely do so with different query IDs.

This is used in place of a TCP/UDP port number in the NAT table of the router so that replies coming back can be matched to the initial request and sent back to the correct inside host (as well as the usual matching based on the outside global address).

See also https://www.rfc-editor.org/rfc/rfc5508#section-3.1

Related Topic