2

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.

2
  • This video discusses everything that happens for a host to communicate through a switch and a router. It will shed light on all your questions.
    – Eddie
    Dec 20, 2016 at 18:41
  • Did any answer help you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you could provide and accept your own answer.
    – Ron Maupin
    Aug 15, 2017 at 17:02

3 Answers 3

2

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.

2
  • Okay thank you for this. However, as you mentioned DHCP I still have a small misunderstanding. How would the router's DHCP allocate an IP address to computer A and Computer B? Really appreciate your time.
    – Lockey
    Dec 20, 2016 at 19:43
  • That is actually a different subject, and you need to ask it in a different question. By the way, DHCP addresses are assigned by DHCP servers.
    – Ron Maupin
    Dec 20, 2016 at 22:12
1

Plugging in a computer and visiting google is a surprisingly complex process. This answer tries to explain the basic process without going into too much detail.

When you plug your computer in to the network it uses a protocol called DHCP to request IP configuration for itself. This configuration will include IP address, subnet mask, default gateway and DNS servers.

The DHCP request is sent as a broadcast to all systems on the local Ethernet network and is answered by the DHCP server. In a typical home or small buisness environment the DHCP server will be running on the router. The switch doesn't (normally) know or care about DHCP it just passes the requests and responses around.

When you go to visit google.com your browser asks the operating system to convert google.com to an IP address. The operating system generates a DNS request and queues it for sending to the DNS server address.

It then looks up the DNS server's address in it's routing table. To determine a "next hop IP address" and interface. By default if the destination is on the local subnet then the next hop IP address will be the same as the destination IP address. Otherwise the next hop IP address will be the default gateway.

That is when ARP first comes into play. The "next hop IP address" must be translated to a MAC address. The computer will look in it's ARP table, if it finds an entry then great, otherwise it will make an ARP request to try and locate the MAC address that corresponds to the next hop IP address.

What happens next depends a bit on configuration. Maybe the router routes the packet out to a DNS server on the Internet, maybe it performs NAT, maybe the router has a local DNS server.

Either way the router will end up with a DNS reply addressed to your client machine. It will look up your machine's IP address in it's routing table and determine it is on the local network. It will then look it up in it's ARP table so it can send it to your machine.

your computer gets a DNS reply. So it has an IP address for google.com and can attempt to connect to it. The connection process starts by sending a TCP syn packet.

Once again the destination IP address is looked up in the routing and arp tables and the packet is sent to the router. In a typical home/small buisness setup the router will perform network address translation so the packet appears to come from the router and send the packet out towards the internet.

When the reply arrives the router will reverse any network address translation and send the packet back to your host. Again it uses it's arp table to find your host's MAC address.

The switch is largely ignorant of all of this. It just uses the source MAC addresses to build a table mapping MAC addresses to ports. If it knows which port the destination MAC address is behind it will send the packet there, otherwise it will flood it everywhere.

-1

It's the Internet, not internet (capital I). Internet Protocol, IP, is the (no so) secret sauce. Link layer protocols such as Ethernet and WiFi enable communications links, but network layer protocols such as IP allow internetworking, the passing of traffic/communications over multiple links.

IP is basically some open source software.

1
  • Can someone elaborate on what's wrong with my information/answer? Why the downvote? Jan 21, 2017 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.