Lecture 2
IP and Subnets
The Internet Protocol (IP) runs at the network layer, in the middle of the five layers.
| Layer | Role |
|---|---|
| Application | communication |
| Transport | end-host connectivity |
| Network | interconnection of all hosts, across LANs |
| MAC | interconnection within one LAN |
| Physical | point-to-point transmission of bits |
IP forwards packets from router to router by packet switching, based on their destination IP addresses.IP is best effort: it gives no delivery guarantee.Packets wait briefly in router buffers, and packets of one flow can take different paths.So packets can be dropped, delayed or reordered.
The devices of a subnet connect at the data link layer and share one IP-address prefix, such as 128.178.71.The subnet mask marks the prefix bits with 1s.On an EPFL IPv4 LAN, the prefix has 24 bits:
mask, binary 1111 1111 1111 1111 1111 1111 0000 0000
mask, dotted 255. 255. 255. 0
slash notation 128.178.71.34/24Prefixes have 26 bits on ETHZ's IPv4 LANs and 64 bits on EPFL's IPv6 LANs, as in 2001:620:618:1a6:0a00:20ff:fe78:30f9/64.
The Two Rules of IP Unicast
IP rule 1 says that only routers interconnect different subnets.Inside a subnet, traffic crosses only data-link devices, such as switches and WiFi base stations.Two subnets have different prefixes, with the same or different masks.
Example 1. The destination IP address inside a LAN.
Host W sends an IP packet to host P through the switch.
The IP header names P as its destination, with no router on the path.An IP packet always carries its destination IP address, even in a LAN.The switch appears in neither header.
IP rule 2 says that IP addresses have structure.IP aims to interconnect every system in the world by IP address.So each address tells something about delivery, in a form that makes forwarding in hardware easy.
Unicast. A unicast packet goes to exactly one interface.
Host part. The host part of an address is the bits after the prefix.It is also called the suffix.In 128.178.71.202/24, the prefix is 128.178.71 and the host part 202.Interfaces of one subnet share the prefix, and so the subnet mask.They differ in their host parts.
Longest Prefix Match
Next hop. The next hop is the router or host on an attached link to which a system forwards the packet.
Forwarding table. A forwarding table maps each destination prefix to a next hop, an outgoing interface, or both.It is also called the routing table.Every host (end system) and every router (intermediate system) has one.
Longest prefix match (LPM). LPM is the forwarding rule of IP unicast.A system forwards each packet by the closest entry of its table:the longest prefix that the destination address starts with.
Letters stand for prefixes, and a host's name follows its prefix.So A.H1 is host H1 in prefix A, and B.* matches every address in B.
| Letters | Prefix |
|---|---|
A | 2001:620:8::/48 |
B.C | 2001:620:618:1a5::/64 |
B.D | 2001:620:618:1a6::/64 |
Example 2. Two packets from Alice.
Alice sends one packet to Bob, at B.D.H2, and one to Bart, at B.C.H2.
| Router | Entry for Bob, B.D.H2 | Entry for Bart, B.C.H2 |
|---|---|---|
| R1 | B.*, the only match | B.*, the only match |
| R2 | B.D.*, longer than B.* | B.*, the only match |
| R3 | B.C.*, the only match |
Aggregation. Aggregation replaces many long prefixes by one shorter prefix that covers them all.R1 and R4 need a single entry, B.*, for every subnet of B, so their forwarding tables stay small.
Multicast, Broadcast and Anycast
Multicast. A multicast packet goes to every interface that joined its group.Each member listens to its own address and to the group's multicast address.LPM is not enough to forward it: routers also need group membership, distribution trees and packet replication.
Broadcast. A broadcast packet goes to every interface on its subnet.Each interface listens to its own address and to the broadcast address.Routers never forward a broadcast packet.
Anycast. An anycast packet goes to the nearest of several interfaces that hold the same address.An anycast address looks like a unicast one, but several places announce the same address, or the same prefix.Routers forward it by LPM, as a unicast packet.
IPv4 Addresses
An IPv4 address has 32 bits.It is usually written in dotted decimal: four integers of 8 bits each.It can also be written in binary, or in hexadecimal: 8 hex digits of 4 bits each.
binary 1000 0000 1011 1111 1001 0111 0000 0001
dotted decimal 128. 191. 151. 1
hexadecimal 80 bf 97 01
binary 1000 0001 1100 0000 1100 1000 0000 0010
dotted decimal 129. 192. 200. 2
hexadecimal 81 c0 c8 02In base , an integer is a string over an alphabet of symbols, starting from 0.
| Base | Alphabet | 200 in this base |
|---|---|---|
| binary, | 0, 1 | 1100 1000 |
| decimal, | 0 to 9 | 200 |
| hexadecimal, | 0 to 9, then a to f | c8 |
Nibble. A nibble is four bits, written as one hexadecimal digit.So binary and hexadecimal convert digit by digit:c is 1100 and 8 is 1000, so c8 is 1100 1000.
A calculator is the best tool to convert binary or hex to decimal.By hand, add the powers of 2 at the 1 bits:
The largest hex digit f is 1111, or 15, so the largest byte ff is 1111 1111, or 255.
Example 3. From hexadecimal to dotted decimal.
The 32 bits 0102:ffff hold four bytes: 01, 02, ff and ff.Here 01 is 1, 02 is 2 and ff is 255.So in dotted decimal, 0102:ffff is 1.2.255.255.
A mask can end inside a byte.The byte 254 is 1111 1110, so 255.255.254.0 is 23 ones, then 9 zeros: the mask of a /23.A prefix also drops its trailing zero bytes:128.178/16 is 128.178.0.0/16.
Example 4. A valid address for H.
A router's north interface is 2.4.8.1/23, and its south interface is 2.4.7.1/23.A switch connects host H to the north interface.
Is 2.4.8.2 or 2.4.9.1 a valid address for H?
H shares a subnet with the north interface, so H needs its 23-bit prefix.Both candidates match it, because 8 and 9 differ only in a host bit.So 2.4.9/23 and 2.4.8/23 are the same prefix, and both candidates are valid.The south interface differs in prefix bits, so it lies in another subnet.
Reserved block. A reserved block is a prefix kept for a special use.
| Block | Use |
|---|---|
0.0.0.0 | absence of address, or any address |
127/8 | loopback: this host (e.g. 127.0.0.1) |
10/8, 172.16/12, 192.168/16 | private addresses (e.g. at home) |
100.64/10 | private addresses for internet service providers (ISPs) only: carrier-grade NAT |
192.88.99/24 | IPv6-to-IPv4 relays (deprecated, not reassigned) |
169.254/16 | link-local addresses |
224/4 | multicast |
240/4 | reserved for experimental or future use |
255.255.255.255/32 | link-local (LAN) broadcast |
Global address. A global address identifies one interface uniquely in the public internet.It is also called a public address.Every IPv4 address outside the reserved blocks is global.
Private address. A private address is an IP address that any network can use inside itself, but never in the public internet.Internet routers drop packets destined to a private address.So many private LANs can use the same addresses without routing conflicts.
Link-local address. A link-local address is an IP address that is valid only on one LAN, so no router forwards it.
Private and link-local addresses differ in how they are allocated.A local network administrator allocates private addresses, either statically or through one configured server of the Dynamic Host Configuration Protocol (DHCP).A computer allocates itself a link-local address automatically, e.g. by stateless address autoconfiguration (SLAAC), when it has no static address and finds no DHCP server.
Link-local addresses have their own block.So they never conflict with the addresses of a local but temporarily unavailable DHCP server.
IPv4 Packets and Forwarding
An IPv4 packet is a header, then a payload.The header is protocol overhead, and the payload carries the useful bits.
The Protocol field names the higher-layer protocol:1 for ICMP, 6 for TCP and 17 for UDP.
Internet Control Message Protocol (ICMP). ICMP is the protocol that carries network-layer error messages.
SWITCH is the Swiss academic network.Router ed0-swi links EPFL to SWITCH, and through it to the internet.Router ed0-swi holds this forwarding table.
| Destination | Next hop | Interface |
|---|---|---|
128.178.29/24 | 128.178.100.2, ed2-el | south |
128.178/16 | 128.178.100.3, ed2-in | south |
0/0 | 128.178.47.3, sw-la-01 | north |
Each destination is in aggregated or subnet form:128.178/16 aggregates all of EPFL, and 128.178.29/24 is the subnet of LEMA.Each next hop is named by both its IP address and the interface.
Default route. The default route is the entry 0/0, with an empty prefix.It matches every address, so longest prefix match picks it only when nothing longer matches.
Longest prefix match turns the table into nested tests, with the longest prefix innermost:
if destination in 128.178.*
if destination in 128.178.29.*
forward to ed2-el
else
forward to ed2-in
else
forward to sw-la-01The IPv4 Address Shortage
IPv4's 32 bits give about addresses, too few for every device.Public IPv4 addresses are now traded and charged.
Addresses go down a hierarchy:from the Internet Assigned Numbers Authority (IANA) to a regional internet registry (RIR), then to an ISP or organization.
Free pool. A registry's free pool is the stock of unallocated addresses that it still holds and can hand out on request.
IANA's IPv4 free pool ran out in February 2011, and its last five /8s went one to each of the five RIRs.The RIRs then ran out in turn.
| RIR | Region | IPv4 free pool ran out |
|---|---|---|
| APNIC | Asia-Pacific | 2011 |
| RIPE NCC | Europe | 2012, and fully in November 2019 |
| LACNIC | Latin America | 2014 |
| ARIN | North America | 2015 |
| AFRINIC | Africa | 2017 |
So an organization can no longer request IPv4 addresses from its RIR.It buys them from someone who already holds them.On this secondary market, an address costs roughly $20 in mid-2026, down from a peak near $60 in 2021.About 30 million addresses changed hands in the first half of 2026.
Since 1 February 2024, Amazon Web Services (AWS) bills each public IPv4 address attached to a resource:$0.005 per hour, or about $3.60 per month.Before that date, an address in use on a resource was free.
IPv6 Addresses
IPv6 redefines the packet format with 128-bit addresses, about of them.It offers the same services as IPv4.
Hextet. A hextet is one 16-bit group of an IPv6 address.It is written with 1 to 4 hex digits, or with none when it lies inside a ::.
An IPv6 address is 8 hextets separated by colons, in hexadecimal with lowercase letters.
A hextet may omit its leading 0s:0c26 becomes c26, and :0bad: becomes :bad:.Only leading 0s go, so 0a00 becomes a00, never a.A double colon :: replaces one run of all-zero hextets.It appears at most once, because two would be ambiguous.
uncompressed 2002:0000:0000:0000:0000:ffff:80b2:0c26
compressed 2002::ffff:80b2:c26
uncompressed 2001:0000:0000:01a6:0000:20ff:fe78:30f9
compressed 2001::1a6:0:20ff:fe78:30f9In the second address, :: already replaces the first zero run, so the later zero hextet stays 0.
Example 5. Expanding an IPv6 address.
The address 2001::ada:bada shows three hextets.The double colon fills in five zero hextets, to reach eight.The hextet ada expands to 0ada.
2001:0000:0000:0000:0000:0000:0ada:badaIt is not 2001:0000:0ada:bada.That string holds only four hextets, or 64 bits.
IPv6's link-local broadcast lies inside its multicast block.
| Block | Use |
|---|---|
::/128 | absence of address, or any address |
::1/128 | loopback: this host |
fc00::/7 | unique local addresses (ULA) |
fe80::/10 | link-local addresses |
ff00::/8 | multicast |
ff02::1:ff00:0/104 | solicited-node multicast, used by the Neighbor Discovery Protocol (NDP) |
ff02::2/128 | multicast to all routers on the link |
ff02::1/128 | link-local broadcast (e.g. for periodic router advertisements) |
A prefix length that cuts through a hex digit is read in binary.
| Block | Fixed bits | First hextet |
|---|---|---|
2000::/3 | 001 | 2xxx, 3xxx |
fc00::/7 | 1111 110 | fcxx, fdxx |
fe80::/10 | 1111 1110 10 | fe80 to febf |
Unique local address (ULA). A ULA is a private IPv6 address, used only inside one site such as EPFL.A site's ULAs start with fd, then a 40-bit global ID that the site draws at random (RFC 4193).Two networks are very unlikely to draw the same ID.
Currently, only the block 2000::/3 holds global unicast addresses.A global address can be configured in several places at once, and it is then an anycast address.
| Block | Holder |
|---|---|
2001:620::/32 | SWITCH |
2001:620:618::/48 | EPFL |
2001:620:8::/48 | ETHZ |
2a02:1200::/27 | Swisscom |
2001:678::/29 | provider-independent addresses |
2001::/32 | Teredo: tunnels IPv6 in IPv4 |
2002::/16 | 6to4: tunnels IPv6 in IPv4 |
Allocation is hierarchical:the networks a provider serves use blocks inside its block.EPFL's and ETHZ's /48s lie inside SWITCH's /32, and each EPFL subnet is a /64 inside EPFL's /48.
IPv6 Packets and Forwarding
An IPv6 packet also has a header, options if any, and a payload.
The IPv6 header is twice as long as the IPv4 header, and its 16-byte addresses are four times longer.The Next Header field replaces IPv4's Protocol field:6 for TCP, 17 for UDP, and 58 for ICMPv6, the IPv6 version of ICMP.
A next hop always sits on an attached link, so its link-local address is enough to name it.
Zone index. A zone index %n names one interface of a machine with several interfaces, such as a router.So fe80::1%2 means destination fe80::1 on interface 2.Windows machines typically show zone indexes, but a zone index never appears inside an IP packet.
Router ed0-swi holds this IPv6 forwarding table.
| Destination | Next hop%interface | IPv4 counterpart |
|---|---|---|
2001:620:618:1a4::/64 | fe80::4%2, ed2-el | 128.178.29/24 |
2001:620:618::/48 | fe80::1%2, ed2-in | 128.178/16 |
::/0 | fe80::1%1, sw-la-01 | 0/0 |
Routers sw-la-01 and ed2-in both use fe80::1 on different links, so each IPv6 entry also names the interface: %1 or %2.
Example 6. Three IPv6 destinations at ed0-swi.
| Destination | Matching entries | Next hop |
|---|---|---|
2001:620:618:1a4::1 | /64, /48, ::/0 | fe80::4%2 |
2001:620:618:1ad:a00:20ff:fe78:30f9 | /48, ::/0 | fe80::1%2 |
2001:620:8::1 | ::/0 | fe80::1%1 |
The first destination is ed2-el's interface on LEMA.The second is host lrcmac4 on LRC, in subnet 1ad, so the /64 misses it.The third is at ETHZ, outside EPFL's /48.
Source Address Selection
An IPv4 interface usually has one address.An IPv6 host interface typically has several:one or more link-local addresses, plus one or more global unicast ones.The global ones can include temporary addresses and secure, cryptographically generated addresses (CGA).Temporary addresses protect privacy.
The scope of an address is where it is valid:one link for a link-local address, the whole internet for a global one.
Source address selection. Source address selection is the algorithm by which the operating system picks a packet's source address, with these default preferences (RFC 6724).
- Prefer an address of the same scope as the destination.
- Prefer a temporary address over a stable one.
- Prefer the longest prefix match with the destination.
Dual Stack
IPv6 is incompatible with IPv4, so routers and hosts must handle the two separately.
Dual stack. A dual-stack system runs IPv4 and IPv6 side by side, under the same transport layer.A dual-stack router keeps two forwarding tables, one per version.
W can talk to A over IPv4 and to B over IPv6.A and B cannot communicate at the network layer.
A dual-stack client also picks a destination address, because a name can have both kinds.DNS stores an IPv4 address in an A record, and an IPv6 address in an AAAA record.
Happy Eyeballs. Happy Eyeballs is the procedure (RFC 8305) by which a dual-stack client picks IPv6 or IPv4 for one destination.
IPv6 is preferred but never waited for.
- Ask DNS for both the A and the AAAA records.
- Sort the destination candidates (RFC 6724).
- Start the IPv6 connection first.
- If it has not completed within a short delay (about 250 ms), start IPv4 in parallel.
So a broken IPv6 path costs a few hundred milliseconds, not a failure, and operators could turn IPv6 on without breaking anything.
Network Address Translation
NAT box. A NAT (network address translation) box is a gateway that translates an internal IP address and port into a NAT IP address and a NAT port.Translating is also called masquerading.
The internal addresses are typically private, and the ports are UDP or TCP ports.From outside, only the public NAT IP address and a NAT port are visible.
A NAT lets devices share a single public IP address.So a private LAN hides its many private addresses behind one or a few public addresses.
- An internet service provider gives a home one IPv4 address for its devices.
- A virtualization platform hosts guest virtual machines (VMs).They reach the outside with the physical machine's single IP address.
Middlebox. A middlebox is a device on the path that does more than forward packets.
A NAT is a network-layer middlebox, and it violates two principles.
- Layering: a NAT rewrites transport-layer ports as well as network-layer IP addresses.
- The IP rule that public addresses identify hosts uniquely:one NAT address stands for many hosts.
A NAT's internal network is also called its LAN, and its external network its WAN (wide area network).
NAT table. A NAT table holds one row per mapping:an internal IP address and port, the protocol, and the NAT IP address and NAT port that replace them.
At home, a NAT has internal address 192.168.10.1 and external address 130.104.228.200.Both interfaces of a NAT can hold private addresses, if they lie in different subnets (IP rule 1).
Host A, at 192.168.10.11, sends a DNS query from UDP port 1029 to server S, at 1.2.3.4, on UDP port 53.
On a packet from the LAN to the WAN, the NAT:
- translates the source IP address and port, in the IP header and in the UDP or TCP header,
- stores this mapping in the NAT table,
- forwards the packet.
On a packet from the WAN to the LAN, the NAT translates the destination IP address and port.
The NAT creates each entry on the fly, from outbound traffic, when an internal client contacts an external server.It chooses a NAT port that creates no collision in the table.On Linux, iptables implements the NAT table.
Host B, at 192.168.10.10, also sends a DNS query from UDP port 1029.NAT port 3441 already maps A, so B gets NAT port 3442.B's query leaves from 130.104.228.200, port 3442, to S at 1.2.3.4, port 53.
In both directions, the NAT finds a mapping by exact match, not by longest prefix match.B's next packet goes to 1.2.3.4, port 80.It matches B's entry exactly on 192.168.10.10, 1029 and udp, so it leaves again from NAT port 3442.If no entry matches a packet from the WAN, the NAT drops it.
If A only listens as a server on TCP port 5000, it sends no outbound packet.So the NAT creates no automatic mapping for this server.
Port forwarding. With port forwarding, the NAT sends every packet that arrives on a chosen public port to a fixed internal IP and port.An administrator configures this entry by hand.
For A's server, the administrator adds this entry to the NAT table.
| Internal IP | Port | Protocol | NAT IP | NAT port |
|---|---|---|---|---|
192.168.10.11 | 5000 | tcp | 130.104.228.200 | 5000 |
Client C, at 11.12.13.14, then connects to A at 130.104.228.200, port 5000.A must know its NAT IP address in advance, and advertise it to clients like C.A can discover it through a STUN server (Session Traversal Utilities for NAT), or through UPnP (Universal Plug and Play) if the NAT supports it.
As a side benefit, the NAT protects the LAN.A server port is reachable from outside only through a configured entry, and every other server port stays unreachable.
NAT and IPv6
NAT was motivated by the lack of IPv4 addresses.In IPv6, home routers often use no NAT, because the provider typically allocates a block, not one address.A /60 block holds 16 subnets of /64.
Filtering router. A filtering router lets in outside communication only if an inside host initiated it, unless manually configured.It gives an IPv6 home the protection that a NAT gives an IPv4 one.So A's server on TCP port 5000 is reachable at A's own address, once the filter allows it.
Some systems still use NAT with IPv6, when the local network receives one IPv6 address, not a prefix.The internal network then uses link-local addresses, so it is a single LAN.This is typical in virtualization (e.g. VirtualBox's "NAT network").
Carrier-Grade NAT
Carrier-grade NAT (CGNAT). A CGNAT is a NAT that a network operator runs for many users' networks.It shares external addresses among internal hosts.
A virtual private network (VPN) connects remote users into a site's network.EPFL's VPN access uses a CGNAT with the block 128.178.15/24, so .
User 1 appears as 192.168.178.34 at home, as 10.252.13.211 in EPFL's VPN, and as 128.178.15.x in the public internet.User 2 holds the same home address, because each home is its own private network.In the public internet, user 2 can appear with user 1's address, or not.
Some ISPs use CGNAT to give end users fewer public IPv4 addresses.A home network behind the CGNAT can already use 10/8, so these ISPs use the block 100.64/10 inside instead.