Switch – How do computers connected to a switch (which is connected to a router) have access to the internet

internetrouterswitch

Okay,

So I am currently trying to understand this exact problem. I understand that switches operate at level 2, therefore use MAC addresses for comms. Let's assume that the switch is connected to a router and the router has internet access, and there are two computers connected to the switch A and B.

Q1.
If A would like to connect to the internet, let's say connect to Google, how does it do this? Does it simply look at its ARP cache and find that it doesn't know where to find google, so looks up the MAC address for the default gateway (router) and forward this frame onto the router? If this is the case, where do IP addresses come into this, what does the router then do?

Q2.
Does A and B have an IP address allocated to it? If so, how does this happen? My understanding is that the router has a DHCP, but how does it know that it needs to allocate two IP addresses to A and B? Plus, as the switch is level 2 how can it handle IP addresses?

I hope someone can help clear up my confusion.

Thanks in advance.

Best Answer

First, it is Layer 2, not level 2. The OSI model specifies seven layers. Layer 2 is the Data-Link Layer.

Layer-2 addresses, e.g. MAC addresses, are used in layer-2 frame headers to get the layer-2 frame from one host on a LAN to another host on the same LAN.

Layer-3 addresses, e.g. IPv4 addresses, are used in layer-3 packet headers to get the layer-3 packet from the source network to the destination network. A router (layer-3 device) will look up the destination layer-3 address of a layer-3 packet in its routing table to determine where to forward the layer-3 packet towards the destination layer-3 address.

Q1. If A would like to connect to the internet, let's say connect to Google, how does it do this? Does it simply look at its ARP cache and find that it doesn't know where to find google, so looks up the MAC address for the default gateway (router) and forward this frame onto the router? If this is the case, where do IP addresses come into this, what does the router then do?

A source host will compare its layer-3 network to the layer-3 network of the destination address. If the layer-3 destination address is on the same network as the source host's layer-3 address, the layer-2 destination address used is that of the destination host. If the layer-3 destination address is on a different network than the source host's layer-3 address, the source host will use the layer-2 address of its configured gateway (router). The source host will use something like ARP (Address Resolution Protocol) to resolve the layer-3 destination address, or layer-3 gateway address, to the layer-2 destination address, and it will build a layer-2 frame to encapsulate the layer-3 packet with the layer-2 destination address.

A router receiving a layer-2 frame will strip off the layer-2 frame from the layer-3 packet. The router will look up the destination layer-3 address in its routing table. If the router cannot find a match to the destination network, it will discard the layer-3 packet, other wise the routing table will tell the router where to forward the layer-3 packet. The router will build a new layer-2 frame for the interface it will use to forward the packet.

Q2. Does A and B have an IP address allocated to it? If so, how does this happen? My understanding is that the router has a DHCP, but how does it know that it needs to allocate two IP addresses to A and B? Plus, as the switch is level 2 how can it handle IP addresses?

The source and destination hosts will probably have some layer-3 address. If the path is across the Internet, the addresses will be IPv4 or IPv6 addresses. (IP stands for Internet Protocol.)

Layer-3 addresses can be assigned to a host by manually configuring it on the host, or by some automated process (DHCP for IPv4, DHCPv6, SLAAC, or Random Addressing for IPv6, etc.). How the layer-3 addresses are chosen depend on what method of assigning layer-3 addresses is used.

A switch is a layer-2 device, and it doesn't know or care what layer-3 protocols or addressing are used. A switch only looks at the layer-2 frame; it doesn't strip off the layer-2 frame to inspect the layer-3 packets the way a router does.

Related Topic