CN/Lesson 3 of 8/7 min read
Network Layer and IP Addressing
IPv4 header, IP address classes, private addresses, subnetting with worked examples, CIDR and supernetting, NAT, fragmentation, ICMP, DHCP, and IPv6.
On this page
Role of the network layer
The network layer moves packets from source host to destination host across multiple networks.
- Logical addressing (IP addresses).
- Routing: choosing the path (control plane, see routing).
- Forwarding: moving a packet from an input to the right output port of a router (data plane).
- Fragmentation when a link's MTU is smaller than the packet.
IP is connectionless and best effort: no guarantee of delivery, order or no duplication. Reliability is left to TCP.
IP Address
An IP address identifies a network interface at layer 3. IPv4 addresses are 32 bits, written in dotted decimal: 192.168.10.5. An address has a network part and a host part.
IPv4
IPv4 header (20 bytes minimum):
| Field | Purpose |
|---|---|
| Version | 4 |
| IHL | Header length in 32-bit words |
| Type of Service / DSCP | Priority, QoS |
| Total Length | Header + data, max 65535 |
| Identification, Flags, Fragment Offset | Fragmentation and reassembly |
| TTL | Hop limit |
| Protocol | Payload type: 1 ICMP, 6 TCP, 17 UDP |
| Header Checksum | Header only, recomputed each hop |
| Source / Destination Address | 32 bits each |
| Options | Rare |
TTL
Time To Live is decremented by 1 at every router; when it hits 0 the packet is dropped and an ICMP "time exceeded" is sent back. Prevents routing loops from circulating packets forever. traceroute exploits this by sending packets with TTL 1, 2, 3, ...
IP Address Classes
Classful addressing (historic, replaced by CIDR in 1993, but still asked):
| Class | First bits | First octet range | Default mask | Networks / hosts |
|---|---|---|---|---|
| A | 0 | 1 to 126 | /8 (255.0.0.0) | 126 nets, 2^24 - 2 hosts each |
| B | 10 | 128 to 191 | /16 (255.255.0.0) | 16384 nets, 2^16 - 2 hosts |
| C | 110 | 192 to 223 | /24 (255.255.255.0) | ~2 million nets, 254 hosts |
| D | 1110 | 224 to 239 | n/a | Multicast |
| E | 1111 | 240 to 255 | n/a | Reserved |
127.0.0.0/8 is loopback (127.0.0.1 = localhost). 0.0.0.0 means "this host" / any address.
Private IP Address
Reserved ranges not routed on the public internet (RFC 1918), used inside homes and organizations behind NAT:
10.0.0.0/8172.16.0.0/12(172.16.0.0 to 172.31.255.255)192.168.0.0/16
Also 169.254.0.0/16 is link-local (APIPA, self-assigned when DHCP fails) and 100.64.0.0/10 is carrier-grade NAT.
Subnet Mask
A subnet mask marks which bits are the network part (1s) and which are the host part (0s). 255.255.255.0 = /24.
- Network address = IP AND mask (all host bits 0).
- Broadcast address = all host bits 1.
- Usable hosts =
2^(host bits) - 2.
Subnetting
Subnetting borrows host bits to split one network into smaller subnets: less broadcast traffic, better security and address use.
Worked example 1
Split 192.168.1.0/24 into 4 subnets.
Need 2 extra bits (2^2 = 4), so /26 (mask 255.255.255.192), block size 256 - 192 = 64.
| Subnet | Network | Usable hosts | Broadcast |
|---|---|---|---|
| 1 | 192.168.1.0 | .1 to .62 | 192.168.1.63 |
| 2 | 192.168.1.64 | .65 to .126 | 192.168.1.127 |
| 3 | 192.168.1.128 | .129 to .190 | 192.168.1.191 |
| 4 | 192.168.1.192 | .193 to .254 | 192.168.1.255 |
Each has 2^6 - 2 = 62 usable hosts.
Worked example 2
Which subnet is 172.16.45.130/20 in?
/20mask =255.255.240.0; third octet block size256 - 240 = 16.- 45 falls in the block 32 to 47.
- Network
172.16.32.0, broadcast172.16.47.255, hosts172.16.32.1to172.16.47.254,2^12 - 2 = 4094hosts.
Quick prefix table
| Prefix | Mask | Hosts |
|---|---|---|
| /24 | 255.255.255.0 | 254 |
| /25 | 255.255.255.128 | 126 |
| /26 | 255.255.255.192 | 62 |
| /27 | 255.255.255.224 | 30 |
| /28 | 255.255.255.240 | 14 |
| /29 | 255.255.255.248 | 6 |
| /30 | 255.255.255.252 | 2 (point-to-point links) |
VLSM (variable length subnet masking) gives each subnet a different prefix length to fit its size, wasting fewer addresses.
CIDR
Classless Inter-Domain Routing drops classes; any prefix length is allowed: 203.0.113.0/22. This slowed IPv4 exhaustion and shrank routing tables.
Supernetting
Supernetting (route aggregation) combines contiguous networks into one shorter prefix. 192.168.0.0/24 through 192.168.3.0/24 aggregate to 192.168.0.0/22.
Routers forward using longest prefix match: if a packet matches both 10.0.0.0/8 and 10.1.0.0/16, the more specific /16 wins.
NAT
Network Address Translation lets many devices with private addresses share one public address.
- PAT / NAT overload (most common, home routers): the router rewrites the source IP and source port of outgoing packets and keeps a translation table to map replies back.
Inside 192.168.1.10:5000 -> router -> Outside 203.0.113.7:62001 -> server- Static NAT: one private address permanently mapped to one public address (for servers).
- Dynamic NAT: private addresses mapped to a pool of public ones.
Pros: saves IPv4 addresses, hides internal structure. Cons: breaks end-to-end connectivity (inbound connections need port forwarding), complicates peer-to-peer and some protocols (needs STUN/TURN for WebRTC).
Fragmentation
MTU
The Maximum Transmission Unit is the largest packet a link can carry (1500 bytes for Ethernet).
If a packet is larger than the next link's MTU, an IPv4 router fragments it. Fragments share the Identification field; the fragment offset (in 8-byte units) and the More Fragments (MF) flag let the destination reassemble. If the Don't Fragment (DF) flag is set, the router drops the packet and sends ICMP "fragmentation needed": this is how Path MTU Discovery works.
Example: a 4000-byte packet (20 header + 3980 data) over MTU 1500. Each fragment carries at most 1480 data bytes: 1480 + 1480 + 1020, offsets 0, 185, 370.
IPv6 routers never fragment; only the source does.
ICMP
ICMP (Internet Control Message Protocol) carries error and diagnostic messages for IP (protocol number 1).
| Type | Message |
|---|---|
| 0 / 8 | Echo reply / echo request (ping) |
| 3 | Destination unreachable (network, host, port, fragmentation needed) |
| 5 | Redirect |
| 11 | Time exceeded (TTL expired, used by traceroute) |
DHCP
DHCP (Dynamic Host Configuration Protocol) automatically gives a host its IP address, subnet mask, default gateway and DNS servers. Uses UDP ports 67 (server) and 68 (client).
The DORA process:
- Discover: client broadcasts looking for a server (source
0.0.0.0). - Offer: server offers an address.
- Request: client broadcasts that it accepts that offer.
- Acknowledge: server confirms and starts the lease.
Clients renew at 50% of the lease time. A DHCP relay agent forwards broadcasts to a server on another subnet. Rogue DHCP servers are a classic LAN attack (DHCP snooping on switches prevents it).
IPv6
IPv6 uses 128-bit addresses (2^128 addresses), written as 8 groups of hex: 2001:0db8:0000:0000:0000:ff00:0042:8329, shortened to 2001:db8::ff00:42:8329 (drop leading zeros, replace one run of zero groups with ::).
Differences from IPv4:
| IPv4 | IPv6 |
|---|---|
| 32-bit | 128-bit |
| Variable header with options and checksum | Fixed 40-byte header, extension headers, no checksum |
| Routers fragment | Only the source fragments |
| ARP | Neighbor Discovery Protocol (ICMPv6) |
| Broadcast | No broadcast, multicast and anycast instead |
| DHCP or manual | SLAAC (stateless autoconfiguration) or DHCPv6 |
| NAT common | Designed for end-to-end, no NAT needed |
Address types: global unicast (2000::/3), link-local (fe80::/10), unique local (fc00::/7, like private), multicast (ff00::/8), loopback ::1.
Transition: dual stack (run both), tunneling (IPv6 inside IPv4), translation (NAT64).
Quick revision
- Network = IP AND mask; hosts =
2^h - 2. - Private ranges: 10/8, 172.16/12, 192.168/16.
- Longest prefix match decides forwarding.
- NAT maps private IP:port to public IP:port.
- DHCP = DORA over UDP 67/68.
- IPv6: 128-bit, no broadcast, no router fragmentation, NDP replaces ARP.