NAT and Private Addresses
In questa pagina 4
IPv4 addresses are scarce (Network Layer and IPv4 AddressingThe network layer delivers datagrams host to host through routers (encapsulation, routing, forwarding); an IPv4 address is 32 bits split into a network prefix and a host suffix, and modern classless addressing (CIDR) gives a block of 2^(32-n) addresses from a.b.c.d/n, with network and broadcast addresses computed through the netmask.Network Layer and IPv4 Addressing →). Besides CIDR and subnetting (Subnetting, VLSM and SupernettingSubnetting splits an address block into smaller subnets by lengthening the mask (2^s subnets when s bits are borrowed), supernetting does the opposite by shortening the prefix to merge contiguous blocks; design rules for variable-size subnets are power-of-two sizes M with M at least hosts plus 2, prefix 32 minus log2 M, and start addresses divisible by M (largest subnet first), while 2^j blocks of prefix n aggregate into one block of prefix n minus j only if contiguous and aligned.Subnetting, VLSM and Supernetting →), a short-term remedy is to use private addresses and translate them when needed: NAT.
Private networks
Definition (intranet). A private network using the TCP/IP stack, connected to the Internet or not, never crossed by outside traffic. Internal gateways (IG) connect the subnets and an external gateway (EG) connects it to the Internet. Private addresses are used inside, public addresses outside.
Definition (private addresses). Three blocks reserved and never routed in the public network:
| class | range | addresses | mask | host bits |
|---|---|---|---|---|
| A | - | () | 24 | |
| B | - | () | 20 | |
| C | - | () | 16 |
For the B block, covers second bytes from to : addresses.
Is an address private? Use . : second byte , AND , giving : private. : , AND , giving : public.
Different intranets can reuse the same private addresses (uniqueness is needed only inside one intranet). Advantage: more hosts than the public addresses available to the ISP. Drawback: Internet access still needs a public address. A point-to-point link between internal routers can use a private /30, e.g. (hosts and , broadcast ), saving public addresses: assign private addresses wherever possible.
Packets with private addresses are dropped by border routers. To reach the Internet or offer public services: application proxy, NAT or NAPT.
Application proxy
All requests go to the proxy, which forwards them with its own public IP: two TCP connections (client-proxy and proxy-server). Drawback: one proxy per application.
NAT
A Network Address Translator behaves as a standard router and in addition maps private addresses to public ones, using a pool of public addresses; some requests may be blocked if the pool is empty. Example: the pool includes ; host contacts ; the NAT replaces the source with .
NAT table. Bidirectional communication needs a reversible mapping, recorded in the NAT table: the row lets the reply to be converted back to destination .
- Static mapping: a host always has the same public address; impractical (one public address per host, nothing saved).
- Dynamic mapping: based on sessions: when the first packet of a host is intercepted the NAT creates a session and binds the private address to a public one; at the end the binding is released and the public address returns to the pool.
Properties: translation is transparent to hosts and to routing algorithms; ICMP packets must be translated too (they carry IP addresses in the body: ICMP, Ping and TracerouteICMP adds to IP the error reporting and queries it lacks; its messages travel inside IP datagrams with Protocol 1 and are either errors (destination unreachable type 3, time exceeded 11, redirect 5, parameter problem 12), always sent to the original source with the original IP header plus 8 bytes of data, or queries (echo request 8 and reply 0, timestamp 13 and 14); ICMP only reports and never corrects errors, and it supports ping (RTT), path MTU discovery with the D bit (maximum payload 1500-20-8=1472 bytes) and traceroute (TTL increasing, n+1 probes for n routers), but also attacks such as smurf and fake redirects.ICMP, Ping and Traceroute →).
| traditional NAT | bidirectional NAT |
|---|---|
| Basic NAT | Twice NAT |
| NAPT |
Traditional (outbound) NAT
Supports only connections started from the intranet.
Basic NAT translates only the private IP address, one to one for the whole session; if public addresses are fewer than hosts, some sessions cannot start.
NAPT (Network Address Port Translation) also translates the port: The NAT port is chosen among the free ports of the gateway (Transport Layer - Ports and MultiplexingThe transport layer provides logical end-to-end communication between application processes on different hosts and exists only in end hosts; the network layer delivers to the right computer (IP address) and transport to the right process (16-bit port number, 0 to 65535); an IP address plus a port is a socket and the four-tuple of IPs and ports identifies a connection, with multiplexing and demultiplexing done by UDP on the destination socket and by TCP on the whole four-tuple, and the protocols are UDP (connectionless, unreliable), TCP (connection-oriented, reliable) and SCTP.Transport Layer - Ports and Multiplexing →). One public IP serves many sessions, even to the same external host, if the ports differ.
Example. NAT public address . Two internal hosts open connections to port 80, both from source port 5000:
| private (IP, port) | public (IP, NAT port) | destination |
|---|---|---|
| , | , | , |
| , | , | , |
Step by step for :
- Outbound. The segment leaves ; no row exists, so the NAT creates one with a free port () and rewrites the source to . It must also recompute the IP checksum and the TCP/UDP checksum (which includes the IP addresses and ports).
- Why the port. Without it both replies would reach and the NAT could not tell which host they belong to. The two internal ports coincide (5000), hence the NAT must pick different external ports.
- Inbound. The reply to is matched in the table and the destination is rewritten to .
- How many sessions. Ports are 16-bit: ; excluding the 1024 well-known ones about per public address.
Limit. A connection started from outside towards with no row in the table is dropped; for internal servers static mappings or Twice NAT are needed.
Bidirectional NAT: Twice NAT
Supports sessions started from either side. Problem: how can an outside host start a session with an intranet host without knowing a public address for it? Solution with the DNS (DNS - Domain Name SystemDNS translates names such as www.amazon.com into IP addresses; it is a distributed hierarchical database (an inverted tree of up to 128 levels, with 13 root servers and domains registered through ICANN-accredited registrars), each ISP has a local DNS whose address the host gets from DHCP, the host asks it (usually over UDP) and it queries root, top-level and organisation servers, every server caches what it learns marking answers as non-authoritative until the TTL expires, records are (name, type, value, class, TTL), with NAT the DNS must return the public address, and there are four types of attack including cache poisoning.DNS - Domain Name System →) inside the intranet: the DNS returns the (private) address of the server, the NAT translates it to a public one, and the public address is returned to the client in the DNS reply; the DNS must be inside the intranet and statically mapped to a public address. Later packets use that public address and traditional NAT does the translation.
Example. Client asks for myserver.com, private address . The intranet DNS yields ; the NAT maps it to (row ); the DNS reply gives ; the client sends there and the NAT delivers to .
An addressing plan case
Two routers joined by a point-to-point link: the link needs no public addresses, so assign a private /30: interfaces and , network , broadcast . Public addresses are reserved for what must be reachable from the Internet.
Versione ripasso
Private addresses
- Blocks (never routed publicly, reusable by different intranets): (24 host bits), ( to , 20 host bits), (16 host bits).
- Check: : private; : public. Use private /30 on point-to-point links (Subnetting, VLSM and SupernettingSubnetting splits an address block into smaller subnets by lengthening the mask (2^s subnets when s bits are borrowed), supernetting does the opposite by shortening the prefix to merge contiguous blocks; design rules for variable-size subnets are power-of-two sizes M with M at least hosts plus 2, prefix 32 minus log2 M, and start addresses divisible by M (largest subnet first), while 2^j blocks of prefix n aggregate into one block of prefix n minus j only if contiguous and aligned.Subnetting, VLSM and Supernetting →).
- Border routers drop private-address packets: need proxy (one per application, two TCP connections), NAT or NAPT.
NAT
- Router with a pool of public addresses and a NAT table (reversible mapping). Static mapping impractical; dynamic mapping per session (released at the end). Transparent to hosts and routing; ICMP bodies translated too (ICMP, Ping and TracerouteICMP adds to IP the error reporting and queries it lacks; its messages travel inside IP datagrams with Protocol 1 and are either errors (destination unreachable type 3, time exceeded 11, redirect 5, parameter problem 12), always sent to the original source with the original IP header plus 8 bytes of data, or queries (echo request 8 and reply 0, timestamp 13 and 14); ICMP only reports and never corrects errors, and it supports ping (RTT), path MTU discovery with the D bit (maximum payload 1500-20-8=1472 bytes) and traceroute (TTL increasing, n+1 probes for n routers), but also attacks such as smurf and fake redirects.ICMP, Ping and Traceroute →).
- Traditional (outbound): Basic NAT (IP only, one to one, one public IP per active session) and NAPT: , many sessions on one IP.
- , ; replies matched by NAT port; checksums recomputed; about ports per address (Transport Layer - Ports and MultiplexingThe transport layer provides logical end-to-end communication between application processes on different hosts and exists only in end hosts; the network layer delivers to the right computer (IP address) and transport to the right process (16-bit port number, 0 to 65535); an IP address plus a port is a socket and the four-tuple of IPs and ports identifies a connection, with multiplexing and demultiplexing done by UDP on the destination socket and by TCP on the whole four-tuple, and the protocols are UDP (connectionless, unreliable), TCP (connection-oriented, reliable) and SCTP.Transport Layer - Ports and Multiplexing →).
- Inbound connections without a table row are dropped.
- Twice NAT: bidirectional; the DNS inside the intranet (DNS - Domain Name SystemDNS translates names such as www.amazon.com into IP addresses; it is a distributed hierarchical database (an inverted tree of up to 128 levels, with 13 root servers and domains registered through ICANN-accredited registrars), each ISP has a local DNS whose address the host gets from DHCP, the host asks it (usually over UDP) and it queries root, top-level and organisation servers, every server caches what it learns marking answers as non-authoritative until the TTL expires, records are (name, type, value, class, TTL), with NAT the DNS must return the public address, and there are four types of attack including cache poisoning.DNS - Domain Name System →) returns the public address (e.g. ), static mapping for the DNS.
- Typical errors: forgetting the port in NAPT; thinking an outside host can reach an internal host with NAPT only.