Sub topic 4
Which device works at which layer
This chapter assumes you know the OSI and TCP/IP layer names, that data is wrapped in a header at each layer (encapsulation), and what a MAC address and an IP address are. It covers where common network devices sit in the stack, what each one reads, and what that implies for collisions, broadcasts, and speed. GATE asks this as direct questions, such as "at which layer does a bridge operate?" and "how many collision domains does this topology have?"
The rule: a device works at the highest layer whose header it reads
A device is assigned to a layer by the headers it examines and acts on. It does not matter that the device also has a physical port. Every device handles bits, so every device touches layer 1. The layer you assign is the highest one it processes.
| Device | Highest layer | What it reads | What it does |
|---|---|---|---|
| Repeater | 1 (Physical) | Nothing, only signal levels | Regenerates the signal |
| Hub | 1 (Physical) | Nothing | Repeats incoming bits out of every other port |
| Bridge | 2 (Data link) | Destination and source MAC address | Filters and forwards frames between two LAN segments |
| Layer-2 switch | 2 (Data link) | Destination and source MAC address | Forwards frames to the one port that leads to the destination |
| Router | 3 (Network) | Destination IP address | Forwards packets between networks using a routing table |
| Layer-3 switch | 3 (Network) | Destination IP address, plus MAC | Switches within a VLAN, routes between VLANs |
| Gateway | Up to 7 (Application) | Application or transport data | Translates between different protocol stacks |
Layer 1: repeaters and hubs
A repeater receives a weakened signal and sends out a clean, amplified copy. It extends the physical reach of a cable segment. It does not interpret the bits at all, so it cannot tell a good frame from a collision fragment.
A hub is a multiport repeater. A bit arriving on one port is copied to all other ports. A hub therefore has these properties:
- One collision domain. If two hosts send at the same time, their signals overlap on every port. All hub ports share one collision domain.
- One broadcast domain. Every frame reaches every host.
- Shared bandwidth. A 100 Mbps hub shared by 10 hosts gives 100 Mbps in total, not 100 Mbps each.
- Half duplex. Hosts use CSMA/CD, so a host cannot send and receive at the same time.
A hub never looks at addresses, so it cannot filter anything. That is the defining weakness that bridges and switches fix.
Layer 2: bridges and switches
A bridge connects two or more LAN segments and reads the MAC address in each frame. It keeps a table that maps MAC addresses to ports. If the destination is on the same segment as the sender, the bridge drops the frame (this is called filtering). Otherwise it forwards the frame to the correct segment.
A switch is, in effect, a multiport bridge, usually with a dedicated port for each host and the forwarding done in hardware. The next chapters explain how the table is built by learning. For this chapter, what matters is the effect on domains:
- Each port is its own collision domain. A host on a dedicated switch port, running full duplex, does not have collisions at all.
- All ports are still in one broadcast domain. A frame sent to the broadcast address
FF:FF:FF:FF:FF:FFis flooded out of every port. Frames to an unknown destination are also flooded. - Bandwidth is not shared. A 10-port 100 Mbps switch can carry several simultaneous conversations between different pairs of ports.
Bridges and switches differ in implementation, not in layer. Classic bridges have few ports and use software. Switches have many ports and use hardware. Both are layer 2.
A layer-2 device does not change the MAC addresses inside a frame it forwards. That is a key contrast with a router.
Layer 3: routers
A router reads the destination IP address, looks it up in its routing table, and forwards the packet out of the correct interface toward the next hop. Because the link on each side may use a different technology, the router discards the incoming frame header and builds a new one for the outgoing link. The IP header stays, except that the TTL field is decremented and the header checksum is recomputed.
A router therefore does the following:
- Separates broadcast domains. A router does not forward a layer-2 broadcast. Each router interface is a separate broadcast domain.
- Separates collision domains. Each interface is also a separate collision domain.
- Connects different networks. Each interface is on a different IP network.
- Rewrites MAC addresses at every hop. The source MAC becomes the router's outgoing interface, and the destination MAC becomes the next hop's MAC. The source and destination IP addresses stay the same end to end (ignoring NAT).
# What changes and what stays the same across one router hop.
frame_in = {"src_mac": "HostA", "dst_mac": "RouterIf1", "src_ip": "10.0.1.5", "dst_ip": "10.0.2.9", "ttl": 64}
def forward_through_router(frame, out_src_mac, next_hop_mac):
out = dict(frame)
out["src_mac"] = out_src_mac
out["dst_mac"] = next_hop_mac
out["ttl"] = frame["ttl"] - 1
return out
frame_out = forward_through_router(frame_in, "RouterIf2", "HostB")
print(frame_out)
# {'src_mac': 'RouterIf2', 'dst_mac': 'HostB', 'src_ip': '10.0.1.5', 'dst_ip': '10.0.2.9', 'ttl': 63}A router is slower per packet than a switch, because it does a longest-prefix-match lookup and rewrites the headers. It also adds a store-and-forward delay at each hop, as the delay chapters show.
Layer 3 switches and gateways
A layer-3 switch combines switching hardware with routing. It forwards frames by MAC address within a VLAN and routes packets by IP address between VLANs. In GATE answers, treat it as a layer-3 device.
The term gateway is used loosely. In the strict textbook sense, a gateway converts between different protocol stacks, for example between an email system with one format and another. It may work at the transport, session, or application layer, so the answer to "which layer?" is "up to layer 7 (or layer 4 to 7)." Many older texts also call a router a "default gateway", meaning the router that a host sends non-local traffic to. Read the question to see which meaning is intended.
Counting collision and broadcast domains
GATE often gives a diagram and asks for the number of collision and broadcast domains. Use these rules:
- A hub or repeater extends a domain. It does not split it.
- A switch or bridge splits collision domains (one per port) but not broadcast domains.
- A router splits both (one of each per interface).
- A link between two devices that each split a domain counts as its own collision domain.
| Device | Collision domains | Broadcast domains |
|---|---|---|
| Hub with n ports | 1 for the whole hub | 1 (unless the hub is attached to something that splits it) |
| Switch with n ports | n (one per port) | 1 |
| Router with n interfaces | n | n |
Worked example. A router has two interfaces. Interface 1 connects to a switch with 4 hosts attached. Interface 2 connects to a hub with 3 hosts attached.
- Broadcast domains: 2, one per router interface. The switch and the hub do not split them.
- Collision domains: the router-to-switch link counts as 1. The switch has 4 host ports, so 4 more. That makes 5 on the left side. On the right side, the router-to-hub link and the hub's 3 hosts all share one segment, so 1. Total: 5 + 1 = 6.
Check the left side again. The switch has 5 ports in total: one to the router and four to hosts. Each port is its own collision domain, so 5. The right side is one shared segment, so 1. The total is 6.
def count_domains(switch_ports, router_interfaces, hub_segments):
"""Collision domains: one per switch port + one per hub segment.
Broadcast domains: one per router interface (assumes each interface leads to one LAN)."""
collision = switch_ports + hub_segments
broadcast = router_interfaces
return collision, broadcast
# Switch has 5 ports (4 hosts + router), hub segment is 1 (router-to-hub plus 3 hosts)
print(count_domains(switch_ports=5, router_interfaces=2, hub_segments=1)) # (6, 2)Common errors
- "A switch separates broadcast domains." It does not. Only a router (or a VLAN configuration) does.
- "A hub is layer 2 because it has MAC-addressed hosts." The hub reads no addresses. It is layer 1.
- "A router keeps the MAC addresses of the sender." The router rewrites both MAC addresses at every hop. Only the IP addresses stay end to end.
- "A bridge and a switch are at different layers." Both are layer 2.
- Forgetting that switches flood unknown and broadcast frames. Learning does not remove flooding. It reduces it.
Which device should you choose?
| Need | Device |
|---|---|
| Extend cable length with no filtering | Repeater |
| Connect hosts on one shared medium cheaply (obsolete) | Hub |
| Connect many hosts in one LAN and avoid collisions | Switch |
| Join two different IP networks, or limit broadcasts | Router |
| Translate between incompatible protocol stacks | Gateway |
Exercise
A router has three interfaces. Interface 1 connects to a switch with 6 hosts attached. Interface 2 connects to a switch with 3 hosts attached. Interface 3 connects to a hub with 5 hosts attached. How many collision domains and broadcast domains are there?
Show answer
Broadcast domains: 3, one per router interface. Collision domains: the first switch has 7 ports (6 hosts and the router), so 7. The second switch has 4 ports (3 hosts and the router), so 4. The hub segment, including the router link, is 1. Total collision domains: 7 + 4 + 1 = 12.
Exercise
A frame travels from host A to host B through a switch, then a router, then another switch. Which of these change between A and B: the source MAC, the destination MAC, the source IP, the destination IP, the TTL?
Show answer
The switches change nothing. The router rewrites the source MAC and destination MAC and decrements the TTL. The source and destination IP addresses stay the same (no NAT assumed).
Exercise
Why is a hub's total bandwidth shared among all its hosts, while a switch's is not?
Show answer
A hub repeats every bit to every port, so all hosts share one collision domain and only one can transmit at a time. A switch forwards a frame only to the port that leads to the destination, so different pairs of ports can communicate at the same time, each at full port speed.