# Learning Networking Basics - 2

Continuing my learning on networking basics, I learned about the **Ethernet Switch**, which is part of the physical layer of the **OSI Model** and the Network Access Layer of **TCP/IP.** I also discovered some interesting details about how the Ethernet switch forwards packets.

## Ethernet switch

### Ethernet Frame

The Ethernet switch, or simply switch, lets devices connect within a LAN. Its job is to forward or transmit packets (also known as **Ethernet Frames**) from a source device to a destination device using the MAC address of the **Network Interface Card (NIC)** in each device. A packet is just a sequence of bits (0s and 1s).

In the image below, you can see an example of a packet.

![Picture 1- Ethernet Frame Yapısı – Huseyin Pala](https://huseyinpala.site/wp-content/uploads/2023/04/ethernet-frame-format.png align="center")

So, we might ask, in a LAN, **how does the switch know the device's MAC address to determine where to forward a packet**? Let's look at this picture of a simple LAN.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749679778822/976f2017-7679-476f-b183-a4484ab6ac1c.jpeg align="center")

My initial thought was that when a device connects to the LAN, it sends its NIC's MAC address to the switch, like a registration. This way, the switch would have the MAC addresses of all devices connected to the LAN, and when it receives a packet, it could use the destination MAC address in the packet to send it to the correct device.

**But that's not exactly how it works**: In reality, the NIC in devices doesn't know if it's connected to a switch, so it doesn't need to send its MAC address. Also, the protocol must be designed to work independently of specific devices like the switch..

### MAC Address Table

The switch has a MAC address table where it stores device MAC addresses along with the port each device is connected to.

When a packet is sent:

1. The switch first checks if the **Source Address** is already in its MAC address table.
    
2. If it is, and the **Destination Address** is in the table, it forwards the packet to the port where the destination device is connected. If the **Destination Address** is not in the table, it broadcasts the packet to all ports except the source port.
    
3. If the Source Address is not in the table, the switch adds it with the corresponding port, then proceeds as in step 2, either broadcasting or directly forwarding the packet.
    
4. When a device connected to the switch receives the packet, it processes the packet if the Destination Address matches its MAC address; otherwise, it discards the packet.
    

Here is a short illustration of this process: that.

![Ethern frame foward politic by switch](https://cdn.hashnode.com/res/hashnode/image/upload/v1749970586734/156aedd1-c872-4097-95fa-60a4681d5042.png align="center")

As we can see, it's a "simple" method to send packets and save MAC addresses simultaneously, using fewer resources. The table updates each time a new packet is sent.

Let me share another thing I learned about IPv4..

## IPv4 (Network + Host)

In **IPv4**, there are **Network** and **Host** parts. The network part, also known as the **Subnet**, indicates which network the device is part of, while the host part identifies the specific device. Refer to the following image.

![IPv4: Network + Host](https://cdn.hashnode.com/res/hashnode/image/upload/v1749972485360/890a606a-f9ee-4355-a7c9-ef55447304ea.png align="center")

Here are a few things to understand:

* When we have an IPv4 address **192.168.5.123/24**, the **24** indicates that the first 24 bits (**192.168.5.0**) represent the network address.
    
* The subnet mask, **255.255.255.0**, serves the same purpose as the **24**: it identifies the network. So, if we have these two pieces of information: **192.168.5.123** and **255.255.255.0**, it means that **192.168.5.0** is the network address.
    

To conclude, there can be a broadcast address like **192.168.5.255**, which acts as a sub-group for devices with the same address. If a packet is sent to this address **192.168.5.255**, all devices with this broadcast address will receive the packet.

Now, on Linux, when we run `ifconfig -a` and see a display like the following, we understand what the second line means.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749994746836/b73dda91-efd9-4908-8289-7e7d38f13b06.png align="center")

## Conclusion

There are many things I read, use, and see, but I don’t really understand their meanings. It's good to have a little bit of knowledge about them, like the whole world of networking.
