Network Engineering Stack Exchange is a question and answer site for network engineers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As I know, NAT is the system, that divides IP addresses into two categories - public and private. Public represents the local network from the outside and private represents the device from the inside.

Subnetting divides one IP address into the part that represents the network from outside and the part that represents the device on the network.

Remembering that every IP addresses is 32 bit, having two of them (in NAT) gives an opportunity to use more IP addresses (both on the inside and on the outside), than in the situation with subnetting. Why would we need subnet masks then?

When I ask google for my IP from computer and from my phone, I see the very same public IP, and when I scan the local network with nmap, I see only private IPs.

So why do we need subnet masks, if we have NAT?

P.S. When the server sends out the signal to my PC, does it send it only to my public IP and the router remembers which private IP needs it, or does the server send out both, the public and the private IPs?

share|improve this question
    
IP adresses are 128 bit. Only the deprecated IPv4 used 32 bit. – Martin Schröder Mar 24 at 19:42
up vote 4 down vote accepted

NAT and subnetting solve two different problems.

Problem 1: Previously IP addresses were divided into classes A, B and C. A class C address had a default subnet mask of 255.255.255.0 meaning 24 bits decide which network and the last 8 bits are for the host. With 8 bits for hosts you could have 2^8 - 1 = 254 IP addresses that are part of the same network.

Historically it was not recommended to have more than around 250 IPs on the same network because of broadcasts flooding the network which meant that class B networks, which have a default subnet mask of 255.255.0.0 with 16 bits for hosts, have way too many addresses for a single network. Even more so with a class A network.

Also most of the time we require many smaller networks with just a few hosts which is why we subnet. Subnetting is basically moving the boundary between the host and network part of the address. So by decreasing the number of addresses on each network, you can increase the number of networks.

Problem 2 is that, even with subnetting, we have way too few addresses for every device to get its own, which is why we've got NAT.

NAT or in this case PAT works by letting multiple private adresses share a single public IP address by mapping to a port number.

So, in the IPv4 address space, we have a few reserved blocks of addresses which are meant to be used locally on a private network, e.g. 192.168.0.0 with a subnet mask of 255.255.255.0. This network can be used on multiple sites at the same time because they are private and not allowed to be used on the Internet.

So when a device with a private IP wants to go out on the Internet, the private IP is mapped to a public IP address, together with a port number which means that multiple devices on a local network can share on a single public IP. This extends the number of devices that can access the Internet.

If the server is on the same network as your PC it will communicate with your private IP. If the server is on the Internet, it will communicate with your public IP on a specific port which your router maps to your private IP.

share|improve this answer
1  
A minor point: given modern PCs and network devices, limiting the number of hosts in a broadcast domain to 250 is not necessary. – Ron Trunk Mar 19 at 16:08
    
True, I was a bit uncertain if I should have included that limit or not. I'll note that the limit was necessary historically but not any longer. – Jimmy Mar 19 at 16:13
    
Is subnetting used only for private IPs and has nothing to do with servers? If yes, then why cant we assign any IP for any device on LAN (since it happens on a local network, what would be the difference between 192.168.0.20 and 100.100.100.100?)? – New Thing Mar 19 at 16:28
    
Subnetting is for IPs in general no matter if private or public to adjust the size. Any Ip can be used on a local network but you are only supposed to use private adresses because if you use public adresses on a local private network, you won't be able to access the public hosts with the same ip. – Jimmy Mar 19 at 16:33
    
@Jimmy why wouldnt I be able to access the public host with the same IP? – New Thing Mar 19 at 16:36

Let me see if I can clear up some misunderstanding for you:

NAT is the system that divides IP addresses into two categories - public and private. Public represents the local network from the outside and private represents the device from the inside.

Not quite. NAT simply translates IP addresses from one set to another. As you use the terms, "public" means addresses that are globally unique and routable on the Internet. "Private" addresses are not globally unique and therefore not routable on the Internet. As NAT is normally used, your public address is the globally unique address that is the source address of all the packets you send on the Internet.

Subnetting divides one IP address into the part that represents the network from outside and the part that represents the device on the network.

Take out the phrase "from outside" and your definition is correct. Subnetting has nothing to do with "inside" or "outside." Networks exist both on the "inside" as well as "outside" and therefore you need a way to identify networks and how to reach them.

When I ask google for my IP from computer and from my phone, I see the very same public IP, and when I scan the local network with nmap, I see only private IPs.

You see the same IP because your router or firewall is translating the "inside" IPs of your phone and PC to a single public source address as your packets flow through the router. When the return traffic flows through the router, it translates the destination address to the addresses on your PC or phone.

When the server sends out the signal to my PC, does it send it only to my public IP and the router remembers which private IP needs it, or does the server send out both, the public and the private IPs?

IP packets only have a source and destination address, which on the Internet can be considered "public" IPs. The server has no idea if you are using NAT or not. Yes, your router keeps track of the translation.

Remembering that every IP addresses is 32 bit, having two of them (in NAT) gives an opportunity to use more IP addresses (both on the inside and on the outside), than in the situation with subnetting. Why would we need subnet masks then?

I confess I'm not sure of the point you're trying to make here, or how you believe NAT would allow more addresses. Your private address may be 192.168.1.100, translated into a public address. My private address is also 192.168.1.100 translated into a different public address.

share|improve this answer
    
Your answer would be improved (and I think the questioner would get some more clarity) if you explained how the edge router/firewall does the translation: with port mapping. It takes 192.168.1.100 (say) on the outbound packet, and sends it as from itself (x.x.x.x) but using port 123456. When it receives packets back on port 123456, it knows to map this back to the internal IP address it knows for that port. – ErikE Mar 21 at 0:00
    
Well, I think you just did :). Actually, I think the confusion is more about the necessity of subnetting and routing than NAT. But you make a valid point. Thanks. – Ron Trunk Mar 21 at 0:56
    
I think this explanation may do more for the OP than expected, especially the confusing part about "two IP addresses"--clarifying that only one originating IP address goes out in the outbound packet--the public one. – ErikE Mar 21 at 0:58

The internet was built without NAT: it's a later invention. NAT imposes three very strict limitations on networks. Firstly, you can't address hosts behind NAT directly. Secondly, there must be a single gateway linking the network to the rest of the internet. Thirdly, the gateway must track the TCP source and destination ports of every connection through it.

In the internet core, there are a large number of networks that have more than one gateway. The gateways are not aware of one another's network traffic, and they are stateless: they do not track the TCP sequence numbers of every connection through them. This greatly improves their performance. How is a packet routed through the correct gateway to the correct network? Via routing tables, which match the network prefix.

share|improve this answer

The OP is mixing 2 very different concepts. Subnetting is a routing concept that allows for segmentation and a hierarchical routing structure. Think in terms of a phone number with (areacode) Exchange-terminal. In routing there is an Autonomous System Number (areacode) Network (exchange) and host (-terminal). If you think in those terms, the subnet mask determines the size of the exchange-terminal range. This is what routing protocols use to send packets to and from the correct addresses.

As noted above, a NAT translates between 2 networks that cannot route between them. In most cases this is between the public internet address and the private RFC1918 private space that can be used over and over in different locations. The NAT device keeps track of which flows map to the address and ports on two sides of the network space. While this is in your router, which is the IP layer, it is not a function of routing, but works at the transport layer.

Hope this helps

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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