11

I have read a lot on how switches forward within LAN, but little about how switch talks to router.

When does a switch send packet to router ? Is it when it does not find recipient on the same LAN ?

6 Answers 6

10

It kind of depends.

Normal primary operation for a switch is to forward frames from one host on the L2 domain to another host based on the destination MAC address. So in this sense, a switch will only forward frames to the router if the frame it received has the router's MAC address as the destination.

However if it is a managed switch, then it is also acting as a host on the network as well as a L2 device. So management traffic that needs to be routed (i.e. to hosts on a different subnet/network) will be sent by the switch to the router (assuming correct routing/default gateway are in place).

3
  • That's true, but I didn't want to get too deep into exceptions and confuse the OP.
    – Ron Maupin
    Commented Sep 18, 2015 at 17:18
  • 1
    I know, but sometimes the exceptions that are omitted can cause confusion too (especially when there is little context/background behind the question). I figured I would add my answer and hopefully provide a another view that helps the OP's understanding.
    – YLearn
    Commented Sep 18, 2015 at 17:43
  • The first part of the answer is correct but I don't think it explains it adequately. The second part of the answer is also correct but I think it's irrelevant to the question and adds confusion. Commented Oct 16, 2015 at 13:33
9

Switches don't send packets, they send frames. A switch only forwards layer-2 frames from interface to interface. To the switch, a router is just another host. The switch talks to the router when it has a frame addressed to the router's MAC address.

6

Based on the way you phrased your question, I think the important point to know is that the decision of whether the packet should go to the router or not is made by the switch, but by the sending host on that LAN.

The host (PC or router) determines that the destination is not on the local network, and that it needs to be forwarded to the router. If so, the host sets the destination MAC address to that of the router. The switch then forwards packets based on the MAC address. It doesn't know whether it's a router or something else.

1
  • Exactly. A switch sends every packet it receives to every device it cannot be sure does not need to receive it. So a switch talks to a router when it receives a packet it cannot be sure is not supposed to go the router. Commented Jan 16, 2019 at 20:29
5

Switches come in different flavours JavaDeveloper. Why do you need a switch? Because you want more than 2 hosts to share a common communication medium. If you have 2 PCs and they are not far away from each other you could connect an ethernet cable between them, assign IP addresses and start communicating. What if you have 15 PCs? Then you want to have a shared medium, a meeting ground where all are available.

Let's carry on with the same analogy. You plugged in your switch, did the cabling, employed an IP distribution mechanism and assigned each a 192.168.1.x/24 address. You will see that they can reach other. You can create a shared folder on one and send/receive files from others, you are connected.

Now imagine you wanted you and your neighbours' PCs to communicate, he uses a different subnet plus you don't have enough ethernet ports. You now need another mechanism that will route the traffic between you two. You found a router with two interfaces and connected a cable from your switch to one interface and another cable from your neighbours's switch to the oher interface. Why did you plug this cable to switches but not a printer? Because everybody is there and available. If you see the world from the router's point of view, I have 2 networks on each side, one speaks German, other one speaks French. In order to be able to let you 2 communicate it has to speak both languages. That's why on each router interface, you assign an IP address from each subnet to the correct side (yours will be 192.168.1.x/24). He is your virtual gate to your neighbour's house. If your network needs to communicate your neighbours, you have to knock on the virtual door. The address you just assigned at the router interface is your network's gateway to the other network which makes sense right? If you have a PC that only communicates with your printer, you don't need to assign him a gateway address why should you? He always stays at home.

When does a switch talk to router? When it needs to contact other adjacent or reachable networks.

(as other people wrote down, there are layer 3 switches thatt can be assigned IP addresses from other networks enabling you to meet everybody there but I won't talk about that. I wrote long, please don't get me wrong, there are other people who can make use of this thread)

Hope this helps.

1
  • 1
    A managed switch with an IP address is not necessarily a layer-3 switch. The switch (unless it has its own IP address for management) doesn't ever try to reach another network. The switch has no concept of networks, which are a layer-3 construct, because it is a layer-2 device; it doesn't look deeper into the frame to discover the layer-3 address so it doesn't know the layer-3 packet needs to go to a different network.
    – Ron Maupin
    Commented Sep 18, 2015 at 19:02
2

A normal (layer 2) switch sends frames from one port (in) to another port (out) based on the destination MAC address in the frame. It wil search its MAC table

  • if the destination MAC address is in there and when found, it will use the documented port for this MAC.
  • If it isn't found, it will flood all ports with this frame instead.
1

The host will package the packet/frame with the MAC address of the router, based on the knowledge of whether the desired endpoint is on the same subnetwork (using is mask). If it is not on the subnet or network segment, it will then forward the packet/frame to the default gateway on the segment (router), which will first go through the connected switch. The switch knowing that the MAC address is not connected to the device on the incoming interface will then query the CAM table and forward the frame out of the necessary interface if MAC info is present for the desired gateway (router). If not, then the frame will be multicast out of all ports with an arp requesting the desired mac address until it receives a response from the DGW (router), which will then begin the process of establishing a link and the necessary communication. That's it in a nutshell. Hopefully, I didn't miss any important details.

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.