Module 11 · 17 min read · Free chapter
IP addressing, CIDR, ARP, DHCP, ICMP, NAT and routing
An IPv4 address has 32 bits split into network and host parts. A /n block holds 2³²⁻ⁿ addresses and 2³²⁻ⁿ − 2 hosts, and routers use longest prefix match. Fragment offsets count 8-byte units. Distance vector routing uses Bellman-Ford and can count to infinity, while link state floods link costs and runs Dijkstra.
Start from zero: PIN codes and house numbers
When you write an address on a parcel, the PIN code tells the postal system which area it must go to. Only the post office of that area needs to know the house number. A sorting office in Delhi never looks at house numbers in Chennai; it looks only at the PIN code.
An IP address works the same way. One part of it, the network part, says which network the computer is on, like a PIN code. The rest, the host part, picks one machine inside that network, like a house number.
Routers in the middle look only at the network part, so they can store one entry for millions of hosts. This is the key idea behind IP addressing, subnetting and routing, which this chapter builds step by step.
Why it matters. The Internet links billions of devices. It works only because routers keep short tables of networks, not of single machines, and that depends entirely on how addresses are split.
The IPv4 address and header
An IPv4 address has 32 bits, written as four decimal numbers called octets, such as 192.168.10.37. Each octet is 8 bits, so it runs from 0 to 255. A subnet mask such as 255.255.255.0, or a prefix length such as /24, says how many leading bits are the network part.
Every IP packet starts with a header of 20 to 60 bytes. The fields that exams ask about most are listed below.
- Version (4 bits) is 4 for IPv4.
- Header length, IHL (4 bits) counts the header in 4-byte words, so 5 means 20 bytes and the maximum 15 means 60 bytes.
- Total length (16 bits) is the header plus data, so a datagram can be at most 65,535 bytes.
- Identification (16 bits), flags (3 bits: reserved, DF for "don't fragment", MF for "more fragments") and fragment offset (13 bits) are used for fragmentation.
- Time to live, TTL (8 bits) is reduced by 1 at each router, and the packet is dropped when it reaches 0, so a packet cannot loop for ever.
- Protocol (8 bits) says what is inside: 1 for ICMP, 6 for TCP and 17 for UDP.
- Header checksum (16 bits) covers only the header, and every router recomputes it because TTL changes.
- Source and destination addresses are 32 bits each, followed by optional options.
Worked example 1. A packet has and total length 1,000.
- The header is bytes, so it carries 12 bytes of options.
- The data is bytes.
Why it matters. Header fields give quick one-mark questions, and IHL and total length appear in many fragmentation numericals. Remember the units: IHL counts 4-byte words, total length counts bytes, and the offset counts 8-byte blocks.
Classful addressing, private ranges and CIDR
In the early Internet the network part had a fixed size, set by the first few bits of the address. This classful scheme is still asked in exams.
| Class | Leading bits | First octet | Default prefix | Hosts per network |
|---|---|---|---|---|
| A | 0 | 1 to 126 | /8 | |
| B | 10 | 128 to 191 | /16 | |
| C | 110 | 192 to 223 | /24 | |
| D | 1110 | 224 to 239 | none, multicast | not for hosts |
| E | 1111 | 240 to 255 | none, reserved | not for hosts |
The range 127.x.x.x is kept for loopback, so 127.0.0.1 always means "this machine". Three ranges are private and are never routed on the public Internet: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Homes and offices use them inside, behind address translation.
Classes wasted addresses badly. A company needing 2,000 addresses was too big for a class C and had to take a class B with 65,534, so most of it went unused. CIDR (classless inter-domain routing) removed the fixed sizes, so the prefix can have any length n.
A block with a prefix of length has addresses. The first, with all host bits 0, is the network address, and the last, with all host bits 1, is the broadcast address. So addresses are left for hosts.
CIDR also allows aggregation (supernetting). The four blocks 200.1.0.0/24, 200.1.1.0/24, 200.1.2.0/24 and 200.1.3.0/24 share their first 22 bits. So a router far away can store them as one entry, 200.1.0.0/22.
Why it matters. Without CIDR the Internet would have run out of IPv4 addresses much sooner, and routing tables would be far larger. Every subnetting question rests on the formula .
Subnetting numericals and longest prefix match
Subnetting means borrowing some host bits to make smaller networks inside one block. Borrowing s bits makes subnets, and each subnet has s fewer host bits.
Worked example 2. Split 192.168.10.0/24 into 4 equal subnets.
- Four subnets need 2 borrowed bits, so the new prefix is /26 and each subnet has addresses and 62 hosts.
- The subnets are 192.168.10.0/26, 192.168.10.64/26, 192.168.10.128/26 and 192.168.10.192/26.
- In the second one, 192.168.10.64 is the network address, 192.168.10.127 is the broadcast address, and hosts run from .65 to .126.
Worked example 3. Find the network and broadcast addresses of 172.16.45.130/20.
- A /20 keeps the first 20 bits, so the first two octets stay as they are and the third octet keeps only its top 4 bits.
- The third octet is , and keeping the top 4 bits gives .
- So the network address is 172.16.32.0 and the mask is 255.255.240.0.
- Setting all 12 host bits to 1 gives the broadcast address 172.16.47.255, and there are hosts.
Longest prefix match. A router's table often has several entries that match a destination. It picks the entry with the longest prefix, because that is the most specific route.
| Entry | Interface |
|---|---|
| 10.0.0.0/8 | eth1 |
| 10.1.0.0/16 | eth2 |
| 10.1.2.0/24 | eth3 |
| 0.0.0.0/0 | eth0 (default) |
With this table, 10.1.2.5 matches all four entries and goes to eth3, the longest match. Next, 10.1.9.9 matches /16 but not /24, so it goes to eth2, and 10.200.1.1 matches only /8, so it goes to eth1. Finally, 11.0.0.1 matches only the default route and leaves on eth0.
Why it matters. GATE asks for network addresses, host counts and the next hop almost every year. Writing only the one "interesting" octet in binary saves time and avoids errors.
Fragmentation and offset numericals
Every link has a maximum transmission unit (MTU), the largest datagram it can carry, such as 1,500 bytes on Ethernet. If a datagram is bigger than the MTU of the next link, the router cuts it into fragments. Each fragment is a full IP datagram with its own header.
Three header fields make reassembly possible. All fragments keep the original identification value, so the receiver knows they belong together. The MF flag is 1 on every fragment except the last, and the fragment offset gives the position of the fragment's data in the original, counted in 8-byte units.
Because the offset counts 8-byte units, the data in every fragment except the last must be a multiple of 8 bytes. Fragments are joined only at the final destination, never at routers on the way. If the DF flag is set, the router drops the datagram and sends back an ICMP error instead.
Worked example 4. A 4,000-byte datagram (20-byte header, 3,980 bytes of data) meets a link with MTU 1,500.
- Each fragment can carry at most data bytes, and 1,480 is a multiple of 8, so it can be used as it is.
- The data splits as bytes.
- The offsets are 0, and .
- The MF flags are 1, 1 and 0, and the total lengths are 1,500, 1,500 and 1,040 bytes.
If the MTU were 1,000, the data limit would be 980 bytes. But 980 is not a multiple of 8, so each full fragment carries 976 bytes instead.
Why it matters. Fragmentation numericals are a GATE favourite because they mix three units: bytes, 8-byte offsets and 4-byte header words. A neat table of data size, offset, MF and total length for each fragment avoids silly mistakes.
ARP, DHCP, ICMP and NAT
IP alone cannot run a network. Four helper protocols fill the gaps, and GATE lists all of them.
ARP (Address Resolution Protocol). To send a frame on a LAN, a host needs the receiver's MAC address, but it knows only the IP address. So it broadcasts an ARP request, "who has 192.168.1.20?", and the machine with that address replies directly with its MAC address. The answer is saved in an ARP cache for a while, and for a destination on another network the host asks ARP for the MAC address of its default gateway, not of the far host.
DHCP (Dynamic Host Configuration Protocol). A new laptop has no address at all, so DHCP hands one out for a lease time. The four steps are easy to remember as DORA: the client broadcasts Discover, servers send an Offer, the client sends a Request for one offer, and the server sends an Acknowledge. Along with the IP address it also gives the subnet mask, default gateway and DNS server, and it runs over UDP, with the server on port 67 and the client on port 68.
ICMP (Internet Control Message Protocol). ICMP carries error and test messages inside IP datagrams, with protocol number 1. The ping tool sends an echo request (type 8) and waits for an echo reply (type 0). When TTL reaches zero, a router sends back time exceeded (type 11), and a router that cannot deliver a packet sends destination unreachable (type 3).
Traceroute uses time exceeded cleverly. It sends packets with TTL 1, then 2, then 3, and each router on the path in turn reports itself. So the tool learns the whole path one hop at a time.
NAT (Network Address Translation). A home router has one public address but many devices with private addresses. When a device sends a packet out, the router replaces its private source address and port with the public address and a new port, and stores the mapping in a NAT table.
| Inside (private) | Outside (public) |
|---|---|
| 192.168.1.5 : 3345 | 203.0.113.7 : 5001 |
| 192.168.1.9 : 3345 | 203.0.113.7 : 5002 |
When a reply comes to 203.0.113.7:5002, the router looks up the table and sends it to 192.168.1.9:3345. NAT has saved IPv4 from running out, but outside machines cannot start a connection to a device inside unless a port is forwarded by hand.
Why it matters. Every time you open a browser, these four protocols run silently: DHCP gave you an address, ARP found your router, NAT hid your private address, and ICMP reports problems. Exams ask which protocol solves which problem, so match each one to its job.
Routing: flooding, distance vector and link state
Forwarding uses the table, but routing is how the table gets filled. The aim is usually the shortest path, where each link has a cost and the best route has the least total cost. Given the full map, Dijkstra's shortest path algorithm finds it, choosing at each step the nearest node not yet fixed.
Flooding. The simplest method sends every incoming packet out on every line except the one it came on. It surely reaches the destination by the shortest path, because every path is tried, and it survives almost any failure. But it makes a huge number of copies, so routers limit it with a hop count or by remembering sequence numbers already seen.
Distance vector. Each router keeps a vector of its best known distance to every destination and sends it only to its neighbours. On receiving their vectors, router x uses the Bellman-Ford equation , the minimum taken over all neighbours . RIP works this way and treats 16 hops as infinity.
Worked example 5. Router A has neighbours B at cost 2 and C at cost 3. B says its distance to D is 4, and C says its distance to D is 2.
- Through B the cost is , and through C it is .
- So A's distance to D is 5, with C as the next hop.
Count to infinity. Take three routers in a line, A–B–C, each link of cost 1, so A reaches C at cost 2 through B. Now the B–C link breaks. B no longer has a direct route, but it hears A say "C at cost 2", so it sets its own cost to 3 through A.
A then hears B's new value and changes to 4, and B changes to 5, and so on. The two keep counting up until they reach "infinity", which is 16 in RIP. Split horizon (do not tell a neighbour about a route learned from it) and poison reverse (tell it the route costs infinity) fix such two-router loops, but not every longer loop.
Link state. Each router finds the cost of its own links and floods a link state packet with this list to every router. So every router builds the same full map and runs Dijkstra on it. OSPF works this way, and it converges faster and avoids count to infinity, at the cost of more memory and computation.
| Feature | Distance vector | Link state |
|---|---|---|
| What is sent | Whole distance table | Only own link costs |
| Sent to whom | Neighbours only | All routers, by flooding |
| Algorithm | Bellman-Ford | Dijkstra |
| Convergence | Slow, count to infinity | Fast |
| Example | RIP | OSPF |
Why it matters. GATE often gives a small network and asks for a distance vector table after one exchange, or the number of rounds before count to infinity stops. Always apply Bellman-Ford using the neighbours' old vectors for one round.
IPv6 in brief
IPv4 has only about 4.3 billion addresses, which is not enough for every phone and sensor in the world. IPv6 uses 128-bit addresses, which gives of them. They are written as eight groups of four hex digits separated by colons.
Two rules shorten them. Leading zeros in any group can be dropped, and one run of groups that are all zero can be replaced by ::, but only once in an address. So 2001:0db8:0000:0000:0000:0000:0000:0001 becomes 2001:db8::1.
The IPv6 base header is a fixed 40 bytes and has no header checksum, which makes routers faster. The TTL field is renamed hop limit. Routers never fragment IPv6 packets; only the source can, after finding the smallest MTU on the path.
Why it matters. Exams ask for the address size, the header size and the shortening rules. Remember that a bigger address does not mean a bigger header than IPv4's maximum, since 40 bytes is less than 60.
Descriptive lens: how written papers ask this
Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about the network layer in four ways:
- Design a subnetting plan for an organisation with departments of given sizes.
- Explain fragmentation with a worked example of offsets and flags.
- Compare distance vector and link state routing, including count to infinity and its remedies.
- Describe how ARP, DHCP, ICMP and NAT help a host communicate.
Practice questions (our own, not previous-year questions).
- An organisation has the block 200.10.20.0/24 and needs four departments of 100, 50, 25 and 25 hosts. Design a subnetting plan and give each department's network and broadcast addresses. (15 marks)
- Explain IPv4 fragmentation. A 2,400-byte datagram with a 20-byte header crosses a link of MTU 820; list each fragment's data size, offset and MF flag. (10 marks)
- Compare distance vector and link state routing, and explain the count-to-infinity problem with an example. (10 marks)
Answer plan for question 1.
- Start with the largest department, because variable-length subnetting must place big blocks first to keep them aligned.
- Give 100 hosts a /25, since is enough, so it takes 200.10.20.0/25 with broadcast 200.10.20.127.
- Give 50 hosts a /26, since is enough, so it takes 200.10.20.128/26 with broadcast 200.10.20.191.
- Give each group of 25 hosts a /27, since is enough, so they take 200.10.20.192/27 and 200.10.20.224/27.
- Check that the four blocks cover the /24 exactly with no overlap, and state the usable host range of each.
- Conclude that variable-length masks used the whole block, while equal /26 subnets could not have fitted 100 hosts.
Quick revision
- An IPv4 address is 32 bits, and the IPv4 header is 20 to 60 bytes with IHL counted in 4-byte words.
- TTL falls by 1 at each router, and the protocol field is 1 for ICMP, 6 for TCP and 17 for UDP.
- Class A starts with 0, B with 10, C with 110, D with 1110 for multicast, and 127 is loopback.
- A block with a prefix of length has addresses and usable hosts.
- Routers forward by longest prefix match.
- Fragment offsets are in 8-byte units, MF is 0 only on the last fragment, and reassembly happens only at the destination.
- ARP maps IP to MAC, DHCP gives addresses by DORA, ICMP carries errors and ping, and NAT maps private to public addresses.
- Distance vector uses Bellman-Ford with neighbours and suffers count to infinity, while link state floods link costs and runs Dijkstra.
- IPv6 has 128-bit addresses, a fixed 40-byte header and no router fragmentation.