Sub topic 24
GATE Problem Patterns and Shortcuts
This chapter assumes you know binary-to-decimal conversion, the IPv4 header fields, and the basics of subnetting, CIDR, NAT and the two routing algorithms. It turns those topics into repeatable procedures for GATE-style numerical questions.
Choose the pattern first
Most questions in this area reduce to one of six patterns. Identify the pattern before you start calculating.
Pattern 1: Block size shortcut
Let h be the number of host bits, which is 32 minus the prefix length. The block size is 2^h addresses, and the usable host count is 2^h − 2.
To find the network address, divide the relevant octet by the block size and keep the whole-number part. Multiply that quotient by the block size.
For 200.10.5.77/27, h = 5 and the block size is 32. Since 77 / 32 = 2 (whole part), the network address ends in 2 × 32 = 64. The broadcast address ends in 64 + 31 = 95. Usable hosts are .65 to .94, which is 30 addresses.
Pattern 2: VLSM, largest first
- For each requirement, find the smallest block with size ≥ hosts + 2.
- Sort requirements from largest to smallest.
- Allocate blocks one after another from the base address. Each block starts at a multiple of its own size.
Allocating the largest block first keeps every block aligned without gaps.
import ipaddress
def vlsm(base, needs):
addr = int(ipaddress.ip_network(base).network_address)
result = []
for name, hosts in sorted(needs.items(), key=lambda item: -item[1]):
bits = (hosts + 1).bit_length()
sub = ipaddress.ip_network((addr, 32 - bits))
addr += 2 ** bits
result.append((name, str(sub), sub.num_addresses - 2))
return result
for row in vlsm("192.168.10.0/24", {"A": 100, "B": 50, "C": 25}):
print(row)The output is A = 192.168.10.0/25 (126 hosts), B = 192.168.10.128/26 (62 hosts) and C = 192.168.10.192/27 (30 hosts).
Pattern 3: Longest prefix and aggregation
For a destination, first discard every entry whose prefix does not match. Among the entries that remain, pick the one with the largest prefix length. The default route 0.0.0.0/0 matches everything and always loses to a more specific match.
To aggregate blocks, check three conditions:
- The blocks all have the same size.
- The number of blocks is a power of two.
- The first block's address is a multiple of the combined size.
192.168.4.0/24 to 192.168.7.0/24 are four blocks, and 4 is a multiple of 4. They merge into 192.168.4.0/22. In contrast, 192.168.1.0/24 and 192.168.2.0/24 are adjacent but cannot merge, because 1 is not a multiple of 2.
Pattern 4: Fragment arithmetic
Follow these steps in order:
- Data bytes = total length − header length (20 unless options are present).
- Data per fragment = floor((MTU − 20) / 8) × 8.
- Number of fragments = ceil(data / per-fragment data).
- Offset field = bytes already sent / 8.
- MF = 1 for every fragment except the last.
For a 4000-byte datagram and MTU 1500, the data is 3980 bytes and each fragment carries 1480. The fragments are 1480, 1480 and 1020 bytes of data. Their total lengths are 1500, 1500 and 1040, their offsets are 0, 185 and 370, and their MF flags are 1, 1 and 0.
Some further rules:
- A fragment can be fragmented again by a router with a smaller MTU. The final piece of a fragment that already had MF = 1 keeps MF = 1.
- Only the destination reassembles fragments.
- If one fragment is lost, the destination discards the others and the sender must retransmit the whole datagram.
Pattern 5: NAT and PAT tables
With PAT, many private hosts share one public address. Each outbound connection gets a row in the table:
| Private (IP:port) | Public (IP:port) |
|---|---|
| 10.0.0.5:3000 | 203.0.113.1:40001 |
| 10.0.0.6:3000 | 203.0.113.1:40002 |
The router rewrites the source on the way out. On the way back it rewrites the destination by looking up the public port. The 16-bit port field limits one public address to about 65,000 simultaneous translations. Inbound connections with no table entry are dropped, which is why servers behind NAT need port forwarding.
Pattern 6: Routing tables
For distance-vector questions, apply Bellman-Ford: each node's new distance to a destination is the minimum over its neighbours of (link cost + neighbour's advertised distance). After k exchange rounds, a node knows the best path that uses at most k links. In link-state questions, every router has the whole map, so run Dijkstra from the router named in the question.
| Feature | RIP (distance-vector) | OSPF (link-state) | BGP (path-vector) |
|---|---|---|---|
| Metric | Hop count, infinity = 16 | Cost | Path attributes and policy |
| Scope | Inside one AS | Inside one AS | Between ASes |
| Loop defence | Split horizon, poison reverse | Full map | AS path check |
Count-to-infinity happens when a link fails and neighbours keep feeding each other stale routes, so the metric climbs one step per exchange. Split horizon stops a router advertising a route back to the neighbour it learned it from. Poison reverse advertises that route back with an infinite metric instead. Neither fully prevents loops that span three or more routers.
Exercise
A 3000-byte IPv4 datagram (20-byte header) crosses a link with MTU 1000. Give the total length, offset and MF for each fragment.
Show answer
Data = 2980 bytes. Data per fragment = floor(980 / 8) × 8 = 976. The fragments carry 976, 976, 976 and 52 bytes. They have total lengths 996, 996, 996 and 72, offsets 0, 122, 244 and 366, and MF = 1, 1, 1, 0.
Exercise
Can 200.1.0.0/24, 200.1.1.0/24, 200.1.2.0/24 and 200.1.3.0/24 be aggregated? If so, to what?
Show answer
Yes. They are four equal blocks, 4 is a power of two, and the third octet 0 is a multiple of 4. The aggregate is 200.1.0.0/22.