Loading W Code...
IPv4, Subnetting Math, CIDR Notation, IPv6 & NAT
IP Addressing: The logical layer-3 addressing system that allows packets to route across global networks.
32-bit Logical Addressing System
An IPv4 address is a 32-bit numerical label divided into 4 octets (8 bits each), separated by dots. It provides a total address space of $2^{32} \approx 4.29 \text{ billion}$ addresses.
| Class | First Octet | Address Range | Default Subnet Mask | Usable Hosts / Subnet |
|---|---|---|---|---|
| Class A | 1 – 126 | 1.0.0.0 – 126.255.255.255 | 255.0.0.0 (/8) | 16,777,214 |
| Class B | 128 – 191 | 128.0.0.0 – 191.255.255.255 | 255.255.0.0 (/16) | 65,534 |
| Class C | 192 – 223 | 192.0.0.0 – 223.255.255.255 | 255.255.255.0 (/24) | 254 |
| Class D | 224 – 239 | 224.0.0.0 – 239.255.255.255 | N/A (Multicast) | Reserved |
| Class E | 240 – 255 | 240.0.0.0 – 255.255.255.255 | N/A (Experimental) | Reserved |
# Linux System IP Configuration & Routing Information
$ ip -4 addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP
inet 192.168.1.105/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86142sec preferred_lft 86142sec
$ ip route show
default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.105 metric 100
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.105 metric 100To prevent global IPv4 address exhaustion, RFC 1918 reserved dedicated address blocks for internal private networks. Private IP addresses are non-routable on the public Internet and rely on NAT for outbound internet access.
| Class | Private Address Range | CIDR Prefix | Total Addresses |
|---|---|---|---|
| Class A | 10.0.0.0 – 10.255.255.255 | 10.0.0.0/8 | 16,777,216 |
| Class B | 172.16.0.0 – 172.31.255.255 | 172.16.0.0/12 | 1,048,576 |
| Class C | 192.168.0.0 – 192.168.255.255 | 192.168.0.0/16 | 65,536 |
# RFC 1918 Private IP Routing Test & Socket Interface
$ ping -c 3 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=1.42 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.98 ms
--- 10.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001msSubnetting divides a single broad network into smaller, isolated subnetworks. CIDR replaces fixed classful boundaries with variable-length prefix masks (/24, /26), optimizing routing table efficiency and security containment.
| Prefix | Subnet Mask | Usable Hosts / Subnet | Wildcard Mask |
|---|---|---|---|
/24 | 255.255.255.0 | 254 Hosts | 0.0.0.255 |
/25 | 255.255.255.128 | 126 Hosts | 0.0.0.127 |
/26 | 255.255.255.192 | 62 Hosts | 0.0.0.63 |
/27 | 255.255.255.224 | 30 Hosts | 0.0.0.31 |
/28 | 255.255.255.240 | 14 Hosts | 0.0.0.15 |
/30 | 255.255.255.252 | 2 Hosts (Point-to-Point) | 0.0.0.3 |
// C Subnet Mask & Host Range Calculation
#include <stdio.h>
#include <arpa/inet.h>
void calculate_subnet(const char* ip_str, int prefix) {
uint32_t ip, mask, net, bcast;
inet_pton(AF_INET, ip_str, &ip);
ip = ntohl(ip);
mask = prefix == 0 ? 0 : (~0U << (32 - prefix));
net = ip & mask;
bcast = net | ~mask;
printf("Network ID : %u.%u.%u.%u\n", (net>>24)&0xFF, (net>>16)&0xFF, (net>>8)&0xFF, net&0xFF);
printf("Broadcast : %u.%u.%u.%u\n", (bcast>>24)&0xFF, (bcast>>16)&0xFF, (bcast>>8)&0xFF, bcast&0xFF);
}# Subnet Partitioning Command (ipcalc)
$ ipcalc 192.168.1.0/26
Address: 192.168.1.0 11000000.10101000.00000001.00 000000
Netmask: 255.255.255.192 = 26 11111111.11111111.11111111.11 000000
Wildcard: 0.0.0.63 00000000.00000000.00000000.00 111111
=>
Network: 192.168.1.0/26 HostMin: 192.168.1.1 HostMax: 192.168.1.62
Broadcast: 192.168.1.63 Hosts/Net: 62IPv6 expands the address space to $2^{128} \approx 3.4 \times 10^{38}$ addresses, featuring built-in IPsec security and auto-configuration (SLAAC).
| Scope Type | Address Prefix | Functionality |
|---|---|---|
| Global Unicast | 2000::/3 | Globally routable public IP address |
| Link-Local | fe80::/10 | Auto-configured local LAN communication |
| Unique Local | fc00::/7 | Private internal network routing |
| Multicast | ff00::/8 | One-to-many packet distribution |
| Loopback | ::1/128 | Localhost interface |
# Linux IPv6 Interface Address Query & Ping6
$ ip -6 addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
inet6 2001:db8:85a3::8a2e:370:7334/64 scope global dynamic
valid_lft 86400sec preferred_lft 14400sec
inet6 fe80::a00:27ff:fe4e:66a1/64 scope link
valid_lft forever preferred_lft foreverNAT translates private IP addresses to a public IP address as packets traverse the router boundary to the Internet.
# Linux iptables Port Address Translation (PAT / Masquerade) Setup
$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$ sudo iptables -t nat -L -n -v
Chain POSTROUTING (policy ACCEPT)
target prot opt in out source destination
MASQUERADE all -- * eth0 192.168.1.0/24 0.0.0.0/0