Computer Science and IT‹›⌂ 0:00 studied

Module 11 · 17 min read · Free chapter

IP addressing, CIDR, ARP, DHCP, ICMP, NAT and routing

In short

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.

What are you preparing for? We show the practice and previous year questions of your exam.

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 IHL=8\mathit{IHL} = 8 and total length 1,000.

  • The header is 8×4=328 \times 4 = 32 bytes, so it carries 12 bytes of options.
  • The data is 1000−32=9681000 - 32 = 968 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 224−2=16,777,2142^{24} - 2 = 16{,}777{,}214
B 10 128 to 191 /16 216−2=65,5342^{16} - 2 = 65{,}534
C 110 192 to 223 /24 28−2=2542^8 - 2 = 254
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 nn has 232−n2^{32-n} 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 232−n−22^{32-n} - 2 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 232−n2^{32-n}.

Subnetting numericals and longest prefix match

Subnetting means borrowing some host bits to make smaller networks inside one block. Borrowing s bits makes 2s2^s 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 26=642^6 = 64 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 45=00101101245 = 00101101_2, and keeping the top 4 bits gives 001000002=3200100000_2 = 32.
  • 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 212−2=40942^{12} - 2 = 4094 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 1500−20=14801500 - 20 = 1480 data bytes, and 1,480 is a multiple of 8, so it can be used as it is.
  • The data splits as 1480+1480+1020=39801480 + 1480 + 1020 = 3980 bytes.
  • The offsets are 0, 14808=185\dfrac{1480}{8} = 185 and 29608=370\dfrac{2960}{8} = 370.
  • 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 D(x,y)=min⁡v[c(x,v)+D(v,y)]D(x, y) = \min_{v} \left[ c(x, v) + D(v, y) \right], the minimum taken over all neighbours vv. 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 2+4=62 + 4 = 6, and through C it is 3+2=53 + 2 = 5.
  • 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 21282^{128} 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:

  1. Design a subnetting plan for an organisation with departments of given sizes.
  2. Explain fragmentation with a worked example of offsets and flags.
  3. Compare distance vector and link state routing, including count to infinity and its remedies.
  4. Describe how ARP, DHCP, ICMP and NAT help a host communicate.

Practice questions (our own, not previous-year questions).

  1. 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)
  2. 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)
  3. 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 27−2=1262^7 - 2 = 126 is enough, so it takes 200.10.20.0/25 with broadcast 200.10.20.127.
  • Give 50 hosts a /26, since 26−2=622^6 - 2 = 62 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 25−2=302^5 - 2 = 30 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 nn has 232−n2^{32-n} addresses and 232−n−22^{32-n} - 2 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.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. An IPv4 address is how many bits long?

  2. An IPv4 header has IHL=8\mathit{IHL} = 8. How many bytes of options does it carry?

  3. Which of these is a private IPv4 address?

  4. How many usable host addresses does a /27 block have?

  5. What is the network address of the host 172.16.45.130/20?

  6. A routing table has 10.0.0.0/8 → eth1, 10.1.0.0/16 → eth2, 10.1.2.0/24 → eth3. A packet for 10.1.9.9 leaves on:

  7. A 4,000-byte datagram (20-byte header) crosses a link with MTU 1,500. The fragment offset of the third fragment is:

  8. ARP is used to find:

  9. The correct order of DHCP messages is:

  10. Traceroute discovers the routers on a path by using which ICMP message?

  11. In flooding, duplicate packets are usually limited by:

  12. Router A has neighbours B (cost 2) and C (cost 3). B reports distance 4 to D and C reports distance 2 to D. A's distance to D is:

  13. Which statement about link state routing is TRUE?

  14. The size of the IPv6 base header is:

  15. Which item does the GATE 2026 CS syllabus list under Computer Networks?

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

How many hosts does a /26 subnet have?

It has 2⁶ = 64 addresses, and 62 can be given to hosts after removing the network and broadcast addresses.

How is the fragment offset calculated?

Divide the position of the fragment's first data byte in the original datagram by 8.

What does ARP do?

ARP finds the MAC address that belongs to an IP address on the same LAN, using a broadcast request.

What is the count-to-infinity problem?

After a link fails, distance vector routers keep raising each other's distances slowly until they reach infinity.

More in this module: Computer networks

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Computer Networks and Internet Protocol (IIT Kharagpur, 106105183): IP addressing, IP support protocols and routing