# Network Layer
## What is the Network Layer
The Network layer is the third layer of the OSI model. It is responsible for routing data packets from the source to the destination across multiple networks. While the Data Link layer handles communication between devices on the same local network, the Network layer handles communication between devices on different networks that may be separated by many routers. The primary protocol operating at the Network layer is the Internet Protocol, which defines IP addresses and how packets are routed.
## Functions of the Network Layer
Logical addressing assigns unique addresses to every device on a network so that any device can be uniquely identified anywhere in the world. IP addresses are the logical addresses used at the Network layer. Unlike MAC addresses which are tied to specific hardware, IP addresses are assigned based on the network structure and can be changed.
Routing is the process of determining the best path for a packet to travel from the source to the destination. Routers maintain routing tables that list known network destinations and the next hop to reach them. When a packet arrives at a router, the router examines the destination IP address and uses its routing table to determine where to forward the packet. This process repeats at each router along the path until the packet reaches its destination network.
Packet fragmentation allows the Network layer to break large packets into smaller pieces when a packet needs to pass through a network that has a smaller maximum transmission unit. The fragments are reassembled at the destination.
Congestion control at the Network layer involves managing traffic to prevent network congestion that would cause packets to be dropped. Various techniques are used to detect congestion and signal devices to reduce their transmission rates.
## Internet Protocol
The Internet Protocol defines the format of packets and the addressing scheme used across the internet. IPv4 uses 32-bit addresses written as four decimal numbers separated by dots. IPv6 uses 128-bit addresses to provide a much larger address space. Every packet contains the IP address of the source and the IP address of the destination. Routers use the destination address to determine where to forward each packet.
## Routing Protocols
Routers learn about network topology through routing protocols. Static routing involves manually configuring routes in the router's routing table. This is simple but does not adapt automatically to network changes. Dynamic routing protocols allow routers to automatically discover routes and update their routing tables when the network topology changes. OSPF uses the Dijkstra algorithm to calculate shortest paths. RIP uses the Bellman-Ford algorithm and counts the number of hops to each destination. BGP is the protocol used between autonomous systems on the internet.
## Network Layer Devices
Routers are the primary devices operating at the Network layer. A router examines the destination IP address of each incoming packet and forwards it toward its destination based on the routing table. Layer 3 switches can also perform routing functions in addition to the switching functions of Layer 2 switches.Back to Subject