If UDP and/or TCP send packets via IP to an Internet Protocol Address, how can a layer-2 switch forward these?
Is this possible or do you need a layer-3 switch?
Network Engineering Stack Exchange is a question and answer site for network engineers. It only takes a minute to sign up.Sign up to join this community
Layer-2 switches are oblivious to anything above layer-2. Layer-2 protocol carry a variety of layer-3 protocols (IPX, IPv4, IPv6, AppleTalk, etc.). The layer-2 switch knows nothing about this; it only sees the layer-2 frame header.
The layer-3 protocol can carry a variety of layer-4 protocols (TCP, UDP, etc.). The layer-2 switch would need to strip the layer-2 header to look in the layer-3 packet to see which layer-3 protocol the layer-2 frame is carrying, and it doesn't do that.
Routers (layer-3 devices, including layer-3 switches) will strip the layer-2 frame to reveal the layer-3 packet. A router will then switch the layer-3 packet, based on the layer-3 header to a new interface, and create a new layer-2 frame for the new interface.
A layer-3 switch is really a layer-2 switch, but with a router built in. The routing part of a layer-3 switch only gets involved when a frame is destined to a layer-3 interface in the switch.
Edit to answer your comment:
An application will send data to UDP, which encapsulates the data into layer-4 datagrams, including a UDP header, which are then sent to IP (either IPv4 or IPv6). IP encapsulates the UDP datagrams inside IP packets, including an IP header. IP sends the packets to ethernet, which encapsulates the IP packets inside ethernet frames, including an ethernet frame header. The ethernet frame header has the source and destination MAC addresses. Your host may not know the MAC address of the destination host. It will look in its ARP cache for the MAC address of the host with the destination IP address. If it is in the ARP cache, it uses that MAC address. If not, it will broadcast an ARP request, asking for the MAC address of the owner of the IP address. The destination host will reply with its MAC address, and the frame is built. The frame is then sent out the physical interface to the switch.
The next part involves the switch. A switch will build a MAC address table with the MAC address and port. Every time a switch receives a frame on a port, it will update its table with the MAC address and which port that MAC address comes from. When the switch receives a frame from your host, it will look at the destination MAC address, and it will look that up in its MAC address table so that it can deliver the frame to the port where the destination MAC address is. If it can't find that MAC address in its table, it will flood the frame to all ports.
When the destination host receives the frame, it will reverse the encapsulation process.
As you can see, each layer is independent of the other layers, and the switch knows nothing about IP or UDP.
The process is the same for a destination on a different network, except that your host will use the MAC address of its configured gateway (router, including the routing part of a layer-3 switch). The router will strip the frame, look at the packet, switch the packet to a different network interface, and build a new frame for the new interface.