Introduction to TCP/IP (Part 1) - Routers, Switches and Addressing

Last modified by Microchip on 2023/11/10 11:23

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.

router

Back to top

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.

IP address

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).

Back to top

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.

  1. PC Generates Request for IP Address
  2. IP Address Request Received in Router
  3. Router offers an IP address to the PC
  4. PC receives IP address from router
  5. 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.

  • The MAC address has been intentionally left out of this slide to simplify it. We will discuss level 2 (MAC) addressing when switches are introduced.
  • The broadcast IP address is 255.255.255.255.
  • Dynamic Host Configuration Protocol (DHCP) is the specific application used to request and grant IP addresses.

IP address


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.

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.

As a side note, this frame sent by the router will include the destination MAC address of the PC. The IP address is a broadcast address, but the frame also includes the specific destination MAC address of the PC. A switch will use the MAC address to forward this frame to the PC only.
The MAC address is the physical address of a network node. This will be covered in more detail when we describe how a switch works.


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.

IP address

Back to top

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.

Switch

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.

Back to top

Switches Inside Routers

Most routers for homes and small business have a built-in switch.

Switch Router

Back to top

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.

Switch MAC adddress

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).

  • ​Switches are referred to as Layer 2 devices because they work with Layer 2 (MAC) addresses only. routers are referred to as Layer 3 devices because they work with Layer 3 (IP) addresses only.
  • The MAC is responsible for generating the frames that will be sent to the network and receiving frames that come in from the network. Frames that are received on a network node that doesn't contain the node’s MAC address or MAC broadcast address (FF:FF:FF:FF:FF:FF) are not forwarded up to Layer 3.
  • The MAC allows multiple devices to access the same physical network using Carrier Sense Multiple Access with Collision Detection (CSMA/CD (Ethernet)), or Carrier Sense Multiple Access with Collision Avoidance (CSMA/CA (WLAN)).

Back to top

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.

MAC

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.

Back to top

Example:

Switch Operation on a Local Network

Switch Table

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.

  1. PC Sends a Frame to the Switch
  2. Switch Receives Frame
  3. Switch Broadcasts Frame to all Nodes
  4. Router Sends Reply to PC
  5. 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.

The MAC address used to broadcast a frame to all hosts is all ones (FF:FF:FF:FF:FF:FF).

Switch Table 1

Back to top


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.

Remember the frame sent to the Dynamic Host Configuration Protocol (DHCP) server in the router contains the source MAC address.

Switch Table 2


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.
Switch Table 3


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 Table 4


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.
Switch Table 5

Back to top


Example: Simplified Local Network TCP/IP Communication

Local 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.

  1. Open the Web Browser and Enter the IP Address for the Development Board
  2. PC Generates and Transmits a Frame
  3. Frame is Forwarded Through the Switch
  4. Frame Arrives at the Development Board and is Forwarded to the Web Server
  5. 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.

Local communications step 1


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.

Local communications step 2


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.

Local communications step 3


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).

Local communications step 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.

Local communications step 5


Question: What happens next?
Answer:

  1. The frame is received at the switch.
  2. The switch finds the frame’s destination MAC address and uses its routing table to determine what port to forward the frame to.
  3. The frame is sent to the PC
  4. The PC opens the frame and checks the destination MAC address to determine if it needs to pay attention to it.
  5. The PC opens the packet and checks the destination IP address.
  6. The PC opens the message and finds the web page (which is just an HTML file) it requested.

Learn More

Back to top