Understanding Linux Network Architecture: OSI Model, TCP/IP Stack, and Implementation Details
Linux’s networking subsystem is a robust and flexible implementation that supports various networking technologies, including Ethernet, Wi-Fi, Bluetooth, and cellular networks. This article explains how Linux aligns with networking models like the OSI and TCP/IP, the role of netlink, and how low-level network device drivers integrate with the system. Additionally, we’ll explore how these technologies work together to provide seamless connectivity.
Linux Networking and the OSI Model
The OSI (Open Systems Interconnection) model divides network functionality into seven layers:
- Physical Layer: Hardware connections like Ethernet cables and Wi-Fi radios.
- Data Link Layer: Protocols like Ethernet and 802.11 (Wi-Fi) manage access to the physical medium.
- Network Layer: Handles routing and forwarding (e.g., IP).
- Transport Layer: Provides reliable communication (TCP) or faster, connectionless services (UDP).
- Session Layer: Establishes and maintains connections.
- Presentation Layer: Translates data formats.
- Application Layer: Interacts with user applications (e.g., HTTP, FTP).
While the OSI model is theoretical, Linux implements networking based on the practical TCP/IP stack, which merges some layers for simplicity.
Linux Networking and the TCP/IP Stack
The TCP/IP stack used in Linux has four layers:
- Link Layer: Corresponds to the OSI Physical and Data Link layers.
- Network Layer: Manages IP and routing.
- Transport Layer: Implements protocols like TCP, UDP, and SCTP.
- Application Layer: Handles protocols like HTTP, DNS, and SMTP.
Linux Source Code Examples
Link Layer (Network Device Driver)
Located in /drivers/net/
. For example:
1 | // Ethernet example (e1000_main.c) |
Network Layer (IP Routing)
The routing implementation resides in /net/ipv4/route.c
.
1 | // Example: ip_forward() |
Transport Layer (TCP Implementation)
TCP is implemented in /net/ipv4/tcp.c
.
1 | // Example: tcp_transmit_skb() |
Application Layer
Applications interact with the kernel through system calls like socket()
. Example:
1 | int sockfd = socket(AF_INET, SOCK_STREAM, 0); |
Netlink: Kernel-User Communication
Netlink is a socket-based mechanism used to communicate between user space and kernel space for network configuration and management.
Key Features
- Configures routing, interfaces, and filters.
- Notifies user space of events like link changes.
Netlink Implementation
Netlink handlers are implemented in /net/netlink/
.
Example: Route Addition via Netlink
1 | struct nlmsghdr *nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(struct rtmsg), 0); |
User Space Example
Tools like ip
(from iproute2
) use netlink:
1 | ip link set eth0 up |
Low-Level Linux Network Device Drivers
Network device drivers interface between hardware and the kernel, abstracting hardware details for the kernel networking stack.
Key Operations
Initialization:
- Probed via bus-specific interfaces (PCI, USB, etc.).
- Registers itself with the networking subsystem using
register_netdev()
.
Packet Transmission:
- Implements
ndo_start_xmit()
to handle outgoing packets.
- Implements
Packet Reception:
- Triggers a hardware interrupt when packets arrive.
- Uses
netif_rx()
ornapi_schedule()
to pass packets to the stack.
Driver Example: Ethernet
1 | static netdev_tx_t my_driver_start_xmit(struct sk_buff *skb, struct net_device *dev) { |
Wireless Networking: Wi-Fi, Bluetooth, and Cellular
1. Wi-Fi
Wi-Fi is implemented via the mac80211 subsystem in /net/mac80211/
. It provides a common interface for Wi-Fi drivers.
Key Components
- Station Mode: Connects to access points.
- Access Point Mode: Acts as an AP for other devices.
Driver Example: Wi-Fi Device
1 | static int my_wifi_probe(struct pci_dev *pdev, const struct pci_device_id *id) { |
2. Bluetooth
Linux Bluetooth stack includes:
- HCI Layer: Interfaces with hardware.
- L2CAP Layer: Multiplexing protocol for higher layers.
- Profiles: Implement user-level services like A2DP.
Bluetooth Initialization
1 | hci_dev = hci_register_dev(hdev); |
3. Cellular (Modems)
Cellular network drivers communicate over USB or PCI. Implemented under /drivers/net/usb/
.
Binding Networking Technologies Together
Linux uses a unified approach for integrating diverse networking technologies:
- Unified Sockets API: Applications use
socket()
regardless of the underlying transport (Ethernet, Wi-Fi, Bluetooth). - Common Configuration Tools: Tools like
ifconfig
,ip
, andiw
configure all types of network interfaces. - Dynamic Device Management:
- Interfaces are dynamically created for each technology.
- Example:
wlan0
for Wi-Fi,eth0
for Ethernet.
How Users Can Work with These Technologies
1. Ethernet
- Bring up an interface:
1
ifconfig eth0 up
- Debug with
ethtool
:1
ethtool eth0
2. Wi-Fi
- Scan for networks:
1
iwlist wlan0 scan
- Connect to a network:
1
iwconfig wlan0 essid "MyNetwork" key s:password
3. Bluetooth
- Pair with a device:
1
bluetoothctl
- Send a file:
1
obexctl send <file>
4. Cellular
- Configure modem:
1
mmcli -m 0 --simple-connect="apn=myapn"
Conclusion
The Linux networking stack is a sophisticated system that integrates diverse technologies under a unified architecture. By understanding the OSI model, TCP/IP stack, netlink, and network device drivers, developers can better harness Linux’s networking capabilities. Wireless technologies like Wi-Fi, Bluetooth, and cellular seamlessly coexist alongside Ethernet, making Linux a versatile platform for modern networking needs.