Introduction to TCP/IP (Part 1) - Routers, Switches and Addressing
In this self-paced training, you will learn what routers, switches, IP addresses, and MAC addresses are, and how they work on a local network. We will conclude this class by observing some simplified TCP/IP communications between a PC and a development board connected to the same local network.
Routers in Local Networks
Routers connect one network to another. They create local networks, control access to them, and route TCP/IP traffic on them. They also enable local network TCP/IP traffic to move to and from the Internet using Network Address Translation (NAT). Routers also use a firewall to restrict public Internet access to the local network.
What is an IP Address?
IP addresses are used to uniquely identify every host (also known as a network node) on a Transmission Control Protocol/Internet Protocol (TCP/IP) network. They are virtual addresses assigned by routers. Each of the four 8-bit fields is represented by a decimal number ranging from 0 to 255. IP addresses are typically owned and controlled by a DHCP server running in the local network's router. Devices requesting to join a local network could be assigned any available local IP address and the assigned IP address could change at any time.
As a side note, the IP address examples shown here are IPv4 addresses. IPv4 is still used for the vast majority of internet communications, but it will eventually be replaced with IPv6 which uses eight 16-bit fields of addressing.
- IPv4 allows 4.3 billion addresses (about one per person).
- IPv6 allows 3.4 x 1038 addresses (5 x 1028 addresses per person).
Obtaining IP Addresses
Before communicating with a device over a local network, we need to join the network. The router creates a local network and controls access to it. It has the job of allocating and assigning the IP addresses used on its local network. When a device connects to a network, it will automatically request an IP address from the router.
The following steps show a simplified TCP/IP transaction to get an IP address.
- PC Generates Request for IP Address
- IP Address Request Received in Router
- Router offers an IP address to the PC
- PC receives IP address from router
- PC configures its IP address
PC Generates Request for IP Address
Note that the PC’s IP Address is 0.0.0.0 before it is assigned. Also note the PC and the router are physically connected together with an Ethernet cable. The end of every Ethernet cable or WIFI antenna is connected to a network PHY. This PHY is a transceiver responsible for generating and driving the signals that propagate on the wire. It will also receive and decode signals generated by the PHY at the other end of the connection.
IP Address Request Received in Router
The router receives the packet and finds it has been sent to everyone on the network. It, therefore, has to pay attention to the packet. The router sees the sender of the packet needs an IP address assigned to it. That’s the router's job, so it creates a new IP address for it. All other hosts on the local network will eventually discard the packet once they discover the content of the message and realize they cannot provide an IP address.
Router offers an IP address to the PC
The router allocates a new IP address for the PC and generates a broadcast message containing this IP address.
Question:
Why does the router use the broadcast IP address instead of sending it directly to the PC’s IP address?
Answer:
It can’t send it to the PC because the PC doesn’t know its IP address yet and therefore doesn’t have the ability to filter packets based on its IP address.
PC receives IP address from router
The PC receives a packet that has been broadcast to everyone on the network. It opens the packet and finds it contains a message for someone requesting to have an IP address assigned to it. This is the message it has been waiting for!!
PC configures its IP address
The PC uses the IP address contained in the message to configure its network interface. It is now able to communicate on the network with it.
Switches in Local Networks
Now let’s learn what switches are and how to use them in a local network.
A switch enables the connection of multiple devices to the same network.
Note that each network interface to the switch has its own dedicated PHY responsible for driving the signals on each wire.
The uplink port on a switch is the same as the regular ports except that the transmit (Tx) and receive (Rx) signals are reversed. This means a crossover cable is not required to connect one switch to another. Most new switches have Auto-MDIX interfaces which automatically switch the Tx and Rx signals if needed.
Switches Inside Routers
Most routers for homes and small business have a built-in switch.
Switches use MAC Addresses
We’ve seen how routers use IP addresses to address hosts on the network. Switches don’t have the intelligence to use IP addresses. They instead use something called a Media Access Controller (MAC) address.
A switch uses a Media Access Controller (MAC) to forward and filter data based on a host’s MAC address, not its IP address.
The MAC controls Layer 2 network functions. It forwards and filters frames based on their MAC addresses.
Every network host has two addresses:
- IP Address (Layer 3 virtual address)
- MAC Address (Layer 2 physical address)
A switch has a MAC, but no MAC address. It is transparent to the network. It is never the final destination for network traffic, so it doesn’t need a MAC address. A router, on the other hand, has two MAC addresses, one used for the local area network (LAN), and one used for the Internet or wide area network (WAN).
What is a MAC Address?
All hosts that have an IP address also have a MAC (Media Access Controller) address. Unlike IP addresses which are virtual, MAC addresses are fixed hardware-based addresses that never change. They are programmed into a device when it is manufactured and all MAC addresses are globally unique. They are assigned and managed by the IEEE registration authority.
MAC addresses contain six eight-bit fields expressed as hex numbers.
A switch has a Media Access Controller, but no MAC address. It is transparent to the network. It is never the final destination for network traffic, so it doesn’t need a MAC address. A router, however, has two MAC addresses: one used for the local network or LAN, and one used for the Internet or WAN.
Example:
Switch Operation on a Local Network
A switch uses a routing table to associate the switch’s port number with the MAC address connected at the other end of the wire. The following steps will show how the switch’s routing table is populated.
- PC Sends a Frame to the Switch
- Switch Receives Frame
- Switch Broadcasts Frame to all Nodes
- Router Sends Reply to PC
- Switch Forwards Frame to PC
PC Sends a Frame to the Switch
When a host first connects to a network, it attempts to communicate with a [[router>>url:https://microchipdeveloper.com/tcpip:routers]] to obtain an [[IP addresses>>url:https://microchipdeveloper.com/tcpip:ip-addresses]].
It creates a [[packet>>url:https://microchipdeveloper.com/tcpip:tcp-ip-network-layer-layer-3]] with a broadcast IP address, then encapsulates the packet into a [[frame>>url:https://microchipdeveloper.com/tcpip:tcp-ip-data-link-layer-layer-2]] with a broadcast [[MAC address>>url:https://microchipdeveloper.com/tcpip:mac-addresses]]. This frame is then sent to all hosts.
Switch Receives Frame
When the switch receives this frame, it uses its routing table to associate the host's MAC address with the interface that received the frame.
Switch Broadcasts Frame to all Nodes
The MAC in the switch sees this is a broadcast frame, so the switch forwards the frame to all hosts connected to it.
Router Sends Reply to PC
When the switch receives the reply from the router, it associates the router’s MAC address with the interface that received the frame.
Switch Forwards Frame to PC
The switch looks at this frame to find the destination MAC address and sees that it is already in its routing table. The frame is then forwarded to the appropriate port. No other port will see this frame.
Example: Simplified Local Network TCP/IP Communication
Now that we know how IP addresses are assigned and how switches and routers work, let's observe how two hosts communicate with each other on a local network.
The following steps show a simplified Transmission Control Protocol (TCP)/Internet Protocol (IP) transaction to get an IP address.
- Open the Web Browser and Enter the IP Address for the Development Board
- PC Generates and Transmits a Frame
- Frame is Forwarded Through the Switch
- Frame Arrives at the Development Board and is Forwarded to the Web Server
- Web Server Sends the Webpage to the PC
Assume we have an embedded network device we want to control and monitor. One way we could do this is to create a web page on the embedded device that displays the status of the embedded device. This web page could also be used to control the device.
Open the Web Browser and Enter the IP Address for the Development Board
We will use a web browser on the PC to access a web page on the development board.
Note that both the PC and development board in this example currently have an IP address. This also means the switch has seen a frame sent from each node, so the switch’s routing table has been updated to show which MAC address is connected to each port.
We open a web browser on the PC and enter the IP address for the development board.
PC Generates and Transmits a Frame
The PC creates a frame of data then sends it to the PHY to transmit on the local network.
This frame was initiated by the web browser when it created a message requesting to download the web page found at IP address 192.168.1.102
The source and destination IP addresses were added to the message to create a packet, then the source and destination MAC addresses were added to the packet to create the frame.
This step has been simplified. It does not show the Transport Layer (TCP). It also doesn’t show the Address Resolution Protocol (ARP) process of obtaining the destination MAC address.
Frame is Forwarded Through the Switch
The frame arrives at the switch and it is opened to find the destination MAC address. This MAC address is found in the routing table so it sends it out to Port 0.
Note that the switch only opens the frame to determine the MAC address. A switch is a layer 2 device and is only concerned with layer 2 (MAC) addresses. It doesn’t touch the packet or care what the IP address is.
Frame Arrives at the Development Board and is Forwarded to the Web Server
The frame arrives at the development board and the message is delivered to the web server application. Let’s look at this process in more detail:
When the frame arrives at the development board, the destination MAC address is checked to determine if it needs to pay attention to the frame. The MAC address matches its own, so it opens the frame to check the destination IP address in the packet. The development board finds the destination IP address matches its own and opens the packet to see what the message is. The development board finds that the message is for a web server. The board happens to have a web server running on it, so it sends the message to the web server application.
- If the MAC address in the frame doesn’t match the device’s MAC address, the frame will be discarded by the Data Link Layer (Layer 2).
- If the IP address in the packet doesn’t match the device’s IP address, the packet will be discarded by the Network Layer (Layer 3).
- If a message is sent to an application (port number) that is not running in the device it will be discarded by the Transport Layer (Layer 4).
Web Server Sends the Webpage to the PC
Now it’s time for the development board to send the web page. The development board generates a message containing the web page then sends the frame to the PC. Let’s look at this process in more detail:
The web server running on the development board generates a message containing the web page. The board then adds the source and destination IP addresses to create a packet and adds the source and destination MAC addresses, creating a frame. This frame is then sent to the PHY for transmission on the local network.
Question: What happens next?
Answer:
- The frame is received at the switch.
- The switch finds the frame’s destination MAC address and uses its routing table to determine what port to forward the frame to.
- The frame is sent to the PC
- The PC opens the frame and checks the destination MAC address to determine if it needs to pay attention to it.
- The PC opens the packet and checks the destination IP address.
- The PC opens the message and finds the web page (which is just an HTML file) it requested.