Matheion

MA-NET1 · Module 10

Network layer — routers, IP, DHCP, NAT

Topics: Forwarding vs routing · IP datagrams · CIDR · DHCP and NAT

Overview

Session 10 — Network layer: routers, IP, DHCP, NAT

So far Mira has treated the network as a cloud that magically moves segments between her phone and a Northline Relay server. This session opens the cloud. You will meet the router, the device that hops each packet one step closer to its destination across BayLink, SpineNet, and other networks, and you will learn the two jobs it juggles: forwarding (a fast, local decision for each packet) and routing (the slower, network-wide planning that fills the tables forwarding relies on).

Along the way you will see what an IP datagram carries in its header, how addresses are grouped into prefixes using CIDR notation, why a router picks the longest matching prefix, how a brand-new device gets an address through DHCP even though it has none to begin with, and how NAT lets a whole household of devices share one public address.

Plan: ideas → worked numerical stories → short drills.

Time: about 65–80 minutes if you convert the address examples to binary by hand at least once.

Concepts

Session 10 — Network layer: routers, IP, DHCP, NAT

The one-paragraph story

The transport layer (TCP and UDP) moves data between two programs. The network layer moves datagrams between two hosts, possibly across dozens of different networks owned by different organisations. Every device along the way that reads a datagram's destination address and sends it onward is a router. The Skylark Forecast phone app never talks to a router directly, but every tile Mira downloads from Northline Relay is carried through a chain of them — first inside her home or café network, then through BayLink's access network, then across SpineNet's backbone, and finally into the relay's own network. Understanding what those routers do, and how addresses make their job possible, is the heart of this session.


Core ideas — moving datagrams across many networks

1. Two jobs: forwarding and routing

It helps to separate two verbs that people often blur together.

Forwarding is the action a router performs on each arriving packet: look at the destination address, consult the forwarding table, and push the packet out of the right output port. It is local and it must be fast — in the time it takes to read this sentence, a busy router may forward millions of packets. Think of it as a sorting clerk who glances at an address and drops the letter into the correct bin.

Routing is the work of figuring out what the forwarding tables should say. Routers exchange information with their neighbours (using routing protocols), build a picture of the network, compute good paths, and write the results into their forwarding tables. This happens on a slower timescale — seconds to minutes, or whenever a link goes up or down. It is the cartographer who draws the map the clerk uses.

An analogy: forwarding is a driver at a road junction choosing which exit to take, using a sign. Routing is the process by which the signs were put up in the first place.

2. Data plane and control plane

Networking people package that same split with two names. The data plane is everything that happens per packet and must run at line speed — typically in dedicated hardware. Forwarding lives here. The control plane is the logic that decides how packets should be handled — routing protocols, computing paths, installing table entries. It runs in software, more slowly and less often. In some modern designs, the control plane is not inside each router at all but runs on a logically central controller that pushes entries to routers; the data plane still does the actual per-packet work.

3. Inside a router, at teaching depth

A router has three main parts you should be able to name and describe.

  • Input ports receive bits from a link, handle the link-layer details, and — importantly — do the lookup: they examine the destination address in the datagram header and decide which output port it needs. Because lookups are time-critical, input ports often hold a copy of the forwarding table.
  • Switching fabric is the internal "crossbar" that moves a datagram from a particular input port to the output port chosen by the lookup. Fabrics can be built several ways (through shared memory, over a shared bus, or through an interconnected grid), and the details are a hardware topic; what matters here is that the fabric has a finite speed.
  • Output ports take datagrams delivered by the fabric, hold them in a queue if the outgoing link is busy, and then transmit them one at a time.

Where does delay and loss come from? From queues. If several input ports send datagrams to the same output port at the same moment, the output link can only transmit one at a time, so the rest wait. If the queue's buffer fills, new arrivals are dropped. (Input ports can queue too, if the fabric is slower than the combined arrival rate; one packet waiting at the head of an input queue can even hold up others behind it destined for free outputs, a problem known as head-of-line blocking.) This is the physical origin of the packet loss and delay that TCP reacts to in the previous session.

4. What an IP datagram carries

The network layer in the Internet is the Internet Protocol, IP. The unit it moves is the datagram: a header plus a payload (typically a TCP segment or UDP datagram). The header fields you should recognise are:

  • Version — which IP version (4 for the classic format). A router needs this first, to know how to read the rest.
  • Header length — how many bytes the header takes (20 when there are no options).
  • Total length — header plus payload, in bytes.
  • Identification, flags, fragment offset — used when a datagram is too large for a link's MTU and has to be split into fragments that the destination reassembles.
  • Time to live (TTL) — a hop counter. The sender sets it to some value; each router decrements it; if it reaches 0, the router discards the datagram. Without this, a datagram caught in a routing loop would circulate forever.
  • Protocol — which transport-layer protocol should receive the payload at the destination: the value 6 means TCP, and 17 means UDP.
  • Header checksum — an error check over the header only. Because TTL changes at every router, every router must recompute it.
  • Source address and destination address — 32 bits each in IPv4.
  • Options — rarely used extras.

Why does this matter? Because each field answers a question some device must ask: what format is this? how long? is it still alive? which program gets it? did the header survive intact? where did it come from, and where is it going? The destination address is the one that drives forwarding.

5. Addresses, prefixes, and CIDR

An IPv4 address is 32 bits, usually written as four numbers separated by dots, each number standing for 8 bits (0–255). For example, 198.51.100.77. A router cannot hold a separate table entry for every one of billions of addresses, so addresses are grouped. The group is a prefix: the first n bits are fixed, and the remaining bits can be anything.

CIDR notation writes a prefix as address/n, where n is the number of fixed leading bits. So 203.0.113.0/24 means "the first 24 bits are 203.0.113 and the last 8 bits are free", which covers the 256 addresses from 203.0.113.0 to 203.0.113.255. A longer prefix (bigger n) is a smaller group: /26 covers (2^{32-26} = 64) addresses, /30 covers 4. In practice, two addresses in each group have special roles (the first names the network, the last is a broadcast address), so a /24 has 254 usable host addresses.

The flexibility of CIDR has two payoffs. A network can be allocated exactly the size it needs, and neighbouring prefixes can be aggregated: two adjacent /25 blocks can be advertised to the world as a single /24, which shrinks routing tables.

6. Longest-prefix matching

Because prefixes can overlap — a specific small prefix sitting inside a larger one — a destination address may match several table entries. The rule is: choose the entry with the longest matching prefix. The most specific information wins. Mentally, ask of each entry, "do the first n bits of this destination equal the entry's prefix?" Among the "yes" entries, take the one with the biggest n. A default entry 0.0.0.0/0 matches everything with length 0, so it only wins when nothing else matches.

This rule is what lets an organisation announce a broad prefix to the Internet while carving out a more specific path for a part of it internally.

7. DHCP: getting an address when you have none

When a device joins a network, it needs an IP address, but it has none. How does it ask for one, when asking requires an address to put in the "source" field? DHCP — Dynamic Host Configuration Protocol handles this with a four-step exchange (often remembered by the letters DORA).

  1. Discover. The new device broadcasts "is there a DHCP server?" It uses source address 0.0.0.0 ("this host, no address yet") and destination 255.255.255.255 ("everyone on this link").
  2. Offer. A server replies with a proposed address and further settings.
  3. Request. The device formally asks for the offered address (this also tells any other servers that the device accepted someone else's offer).
  4. Acknowledge. The server confirms, and the device may now use the address.

The reply does more than hand out an address. It usually includes the subnet mask (the prefix length), the address of the first-hop router (the default gateway), and the address of a DNS server. The address is leased for a limited time; the device must renew before the lease expires, or the address may go to someone else. That is how a café can serve hundreds of passing phones from a small pool.

8. NAT: many devices behind one address

Public IPv4 addresses are scarce. A household or office typically gets one public address from its provider (say, from BayLink) and puts many devices on a private network using addresses from reserved ranges such as 10.0.0.0/8. Private addresses are not routable across the public Internet, so something must translate them. That something is a NAT — Network Address Translation device, usually the home router.

The idea is wonderfully simple. When a device on the inside sends a datagram out, the NAT router replaces the source address and port with its own public address and a fresh port number, and records the pairing in a NAT table. When a reply returns to that public address and port, the router looks up the table, swaps the destination back to the inside device's private address and port, and forwards it. To the outside world, every inside device appears to be the same single address, distinguished only by port number.

Consequences worth understanding: a packet arriving for a port with no table entry has nobody to be delivered to, so it is dropped — which means unsolicited connections from outside cannot easily reach an inside device. That is a mixed blessing: a mild shield, but also an obstacle for applications that need to receive connections. Purists also note that NAT makes routers peek at transport-layer port numbers, bending the layering rules.

9. Putting the whole journey together

Here is one request from Mira's phone, from the first step to the last, using everything in this session. The phone joined the home network earlier and obtained a private address through DHCP, along with the address of its default gateway. When the Skylark app opens a connection to Northline Relay, TCP hands the phone's IP layer a segment and the relay's destination address. The phone sees that the destination is not inside its own prefix, so it sends the datagram to the default gateway — the home router.

The home router is also a NAT. It rewrites the source to its single public address and a fresh port, records the mapping, and then performs a forwarding lookup: no specific entry matches, so the default route sends the datagram toward BayLink. Each router along the way — BayLink's, then SpineNet's — decrements TTL, recomputes the header checksum, performs a longest-prefix match on the destination address, moves the datagram through its switching fabric, and queues it at the chosen output port. Behind the scenes, the routing protocols those routers run were the reason their tables had sensible entries in the first place.

At the far end, the relay's network recognises the destination prefix, delivers the datagram to the server, and the protocol field tells the server's IP layer to hand the payload to TCP. The reply retraces the path, and the home NAT's table turns the public port back into the phone's private address and port. Nothing about this required the phone to know the route; it only needed to know where to send the first hop.

Reading that story slowly, once, with each bolded word in mind, is a good way to test whether the pieces fit together in your head.


Common confusions

"Forwarding and routing are the same word for the same thing." Forwarding is the per-packet action; routing is the computation that prepares the tables.

"A router reads the whole packet." A router's forwarding decision usually needs only the header's destination address. It does not look at the application data.

"TTL is a time in seconds." Despite the name, it is effectively a hop count: each router subtracts one.

"Longest prefix means the address with the most digits." It means the entry whose fixed-bit prefix length is greatest among entries that actually match the destination.

"Private addresses are secret." They are simply not globally unique, so they only mean something inside one network; NAT is how those devices are reached from the outside world.


Quick self-check before the worked examples

  1. In 192.0.2.0/27, how many addresses are in the block, and what is the last address?
  2. A router has two entries matching a destination: one /16 and one /22. Which is used?
  3. Why must a router recompute the header checksum after decrementing TTL?
  4. A NAT router receives a reply addressed to its public address on a port it has never mapped. What should it do?

Answers: (1) (2^{5} = 32) addresses, so 192.0.2.0 through 192.0.2.31; (2) the /22; (3) the TTL is part of the header, so the checksum no longer matches; (4) drop it, since the table gives no inside destination.


If this felt dense

Hold on to a postal picture. A forwarding table is the sorting-room chart on the wall: "mail starting with these digits goes to bin 3." Routing is the planning office that decides what the chart says. DHCP is the front desk that hands a newcomer a mailbox number. NAT is a company mailroom: letters go out with the company's single street address, and the mailroom remembers who inside each reply belongs to.

Examples

Session 10 — Worked examples

Example A — Longest-prefix match at a Northline Relay edge router

Scene. An edge router inside Northline Relay's network holds this forwarding table (all addresses are private, from the 10.x range):

| Entry | Prefix | Output port | |---|---|---| | 1 | 10.20.0.0/16 | 1 | | 2 | 10.20.64.0/18 | 2 | | 3 | 10.20.70.0/24 | 3 | | 4 | 0.0.0.0/0 (default) | 4 |

Making the prefixes concrete. Only the first two or three dotted numbers matter here.

  • /16 fixes the first two numbers: it covers 10.20.0.0 – 10.20.255.255.
  • /18 fixes 18 bits: the first two numbers plus the top 2 bits of the third number. The third number written in binary starts with 01 for values 64–127, so this prefix covers 10.20.64.0 – 10.20.127.255.
  • /24 fixes the first three numbers: 10.20.70.0 – 10.20.70.255.
  • /0 fixes nothing, so it matches every address.

Four destinations

  1. 10.20.70.9. Entry 1 matches (first two numbers are 10.20). Entry 2 matches (third number 70 lies in 64–127). Entry 3 matches (10.20.70.x). Entry 4 matches. The longest is the /24 → port 3.
  2. 10.20.100.5. Entry 1 matches. Entry 2 matches (100 lies in 64–127). Entry 3 does not (third number is 100, not 70). The longest is the /18 → port 2.
  3. 10.20.5.200. Entry 1 matches. Entry 2 does not (5 is not in 64–127). Entry 3 does not. → port 1 from the /16.
  4. 10.21.0.1. Second number is 21, not 20, so entries 1–3 all fail. Only the default matches → port 4.

Interpretation. Notice that destination 1 would also be reachable via the /16, but the router prefers the more specific /24; this is the whole point of "longest prefix." Destination 3 shows an easily missed detail: being inside the big /16 does not mean being inside the nested /18. Always test each prefix separately.


Example B — Slicing a block and merging blocks

Part 1: splitting. Skylark Forecast's operations team is given the block 203.0.113.0/24 (256 addresses) and wants four equal subnets for four office floors.

Four equal pieces means 2 extra bits of prefix, since (2^2 = 4). The prefix goes from /24 to /26, and each piece has (2^{32-26} = 64) addresses.

| Subnet | Prefix | Address range | Usable hosts | |---|---|---|---| | Floor 1 | 203.0.113.0/26 | .0 – .63 | 62 | | Floor 2 | 203.0.113.64/26 | .64 – .127 | 62 | | Floor 3 | 203.0.113.128/26 | .128 – .191 | 62 | | Floor 4 | 203.0.113.192/26 | .192 – .255 | 62 |

The usable count is 64 − 2 because the first address names the subnet and the last is the broadcast address. A device at 203.0.113.150 belongs to Floor 3, because 150 lies in 128–191.

Part 2: aggregation. Meanwhile, BayLink serves two customer blocks, 198.51.100.0/25 (addresses .0–.127) and 198.51.100.128/25 (addresses .128–.255). They are adjacent and the same size, together covering .0–.255. BayLink can therefore announce them to SpineNet as one prefix, 198.51.100.0/24.

Interpretation. Splitting adds bits to the prefix (longer, smaller blocks). Merging removes a bit (shorter, bigger block). Aggregation helps because SpineNet's routers need one table entry instead of two. It works only when the blocks are adjacent, equal in size, and aligned on the boundary a bigger block would have.


Example C — A newcomer gets an address with DHCP

Scene. Mira walks into a café and joins its Wi-Fi with her laptop. The café's network is 192.0.2.0/24. Its DHCP server sits at 192.0.2.1 and hands out addresses from .100 to .199.

| Step | From → To | Source address | Destination address | Content | |---|---|---|---|---| | Discover | laptop → everyone | 0.0.0.0 | 255.255.255.255 | "I need an address" | | Offer | server → laptop | 192.0.2.1 | 255.255.255.255 (broadcast, since the laptop has none yet) | proposes 192.0.2.137, mask /24, gateway 192.0.2.1, lease 3,600 s | | Request | laptop → everyone | 0.0.0.0 | 255.255.255.255 | "I accept 192.0.2.137" | | Acknowledge | server → laptop | 192.0.2.1 | 255.255.255.255 | "Confirmed; lease starts now" |

What the laptop learns. Besides its address 192.0.2.137, it learns the subnet size (/24, so it knows 192.0.2.0–.255 are on its own link), the default gateway 192.0.2.1 (where to send anything outside that range), and a DNS server address, which we leave out of the table to keep it short.

The lease clock. The lease is 3,600 seconds (one hour). A device typically tries to renew at about half the lease, here after about 1,800 seconds. If Mira stays and renews successfully, the same address continues. If she leaves and her laptop stops answering, the server can return 192.0.2.137 to the pool after the lease ends.

Why broadcasts at the start? The laptop has no address, so the server cannot reply to a specific one; and the laptop does not know whether the server's address even exists. Broadcasting solves both problems.

Interpretation. This exchange takes four short messages and happens in well under a second, but without it, nothing else the laptop wants to do could start. It is a classic bootstrapping problem: you need an address to communicate, and communicating is how you get one — solved by a special-case exchange that works with no address.


Example D — One public address, many private devices (NAT)

Scene. In Mira's apartment, the router has a public address of 203.0.113.58 (assigned by BayLink) and a private side 10.0.0.0/24. Her phone is 10.0.0.14 and her tablet is 10.0.0.22. Both open a connection to the Northline Relay server at 198.51.100.77, port 443, using the same source port 51200 by coincidence.

Outgoing packets are rewritten and recorded.

| Inside (before translation) | Outside (after translation) | |---|---| | 10.0.0.14 : 51200 → 198.51.100.77 : 443 | 203.0.113.58 : 60011 → 198.51.100.77 : 443 | | 10.0.0.22 : 51200 → 198.51.100.77 : 443 | 203.0.113.58 : 60012 → 198.51.100.77 : 443 |

The NAT table now contains

| Public side | Private side | |---|---| | 203.0.113.58 : 60011 | 10.0.0.14 : 51200 | | 203.0.113.58 : 60012 | 10.0.0.22 : 51200 |

Replies. The server replies to 203.0.113.58:60011. The router finds the first row, rewrites the destination to 10.0.0.14:51200, and forwards it to the phone. A reply to :60012 goes to the tablet. Notice that the router had to pick different public ports for the two flows; if it had reused 51200 for both, the replies would be indistinguishable.

A stray packet. Suppose a datagram arrives from some other host addressed to 203.0.113.58:60999. No row in the table has that port, so the router has no inside machine to give it to, and it drops it.

Interpretation. From the server's point of view, there is only one client at 203.0.113.58 using two ports. The private addresses never appear on the public Internet. The price is that the router must keep state for every active flow, and that nothing outside can start a conversation with 10.0.0.14 unless the router has been told how to map it.


Example E — Following one datagram: TTL, protocol, and length

Scene. A datagram leaves Mira's phone for the relay. Its header says: version 4, header length 20 bytes, total length 1,340 bytes, TTL 4, protocol 6, source 10.0.0.14, destination 198.51.100.77. Five routers stand between the phone and the server's network.

What is inside? Total length is 1,340 bytes and the header is 20, so the payload is (1340 - 20 = 1320) bytes. The protocol field says 6, which means TCP; so the payload is a TCP segment. If that segment has a 20-byte TCP header, it carries (1320 - 20 = 1300) bytes of application data.

Hop by hop. Each router subtracts 1 from TTL and discards the datagram if the result is 0.

| Router | TTL on arrival | After decrement | Outcome | |---|---|---|---| | R1 | 4 | 3 | forwarded | | R2 | 3 | 2 | forwarded | | R3 | 2 | 1 | forwarded | | R4 | 1 | 0 | discarded | | R5 | — | — | never reached |

At R4 the TTL hit zero, so the datagram never makes it to the server. R4 would typically send a small error message back to the source saying the datagram expired, which is exactly the trick used by route-tracing tools: send datagrams with TTL 1, 2, 3, … and note which router complains at each step.

Checksum. After every decrement, the header changed, so each router recomputed the header checksum before sending the datagram onward. (The payload is not covered by this checksum; TCP has its own.)

Interpretation. With TTL 4 and five routers on the path, the datagram can't complete the journey; it would need a TTL of at least 5 (so that R5 sees 1 and forwards it on). Real senders choose larger values such as 64 so ordinary paths succeed and only loops use up the budget.

Practice check — network layer: routers, IP, DHCP, NAT

Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.

  1. 1.Which statement correctly contrasts forwarding and routing?

  2. 2.Where does packet loss inside a router most commonly originate?

  3. 3.A datagram arrives with TTL = 1. What does a normal router do?

  4. 4.How many addresses does the prefix 198.51.100.64/26 cover, and what is the last one?

  5. 5.A forwarding table has these entries: 10.5.0.0/16 → port 1; 10.5.32.0/20 → port 2; 0.0.0.0/0 → port 3. Which port is used for destination 10.5.40.7?

  6. 6.Why does a device that has just joined a network send its DHCP discover message with source address 0.0.0.0 and destination 255.255.255.255?

  7. 7.Two private hosts behind one NAT router both use source port 51200 toward the same outside server. What does the router do to keep their replies apart?

  8. 8.A datagram has total length 900 bytes and a 20-byte header, and its protocol field is 17. What is in the payload, and how large is it?