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:

  1. Physical Layer: Hardware connections like Ethernet cables and Wi-Fi radios.
  2. Data Link Layer: Protocols like Ethernet and 802.11 (Wi-Fi) manage access to the physical medium.
  3. Network Layer: Handles routing and forwarding (e.g., IP).
  4. Transport Layer: Provides reliable communication (TCP) or faster, connectionless services (UDP).
  5. Session Layer: Establishes and maintains connections.
  6. Presentation Layer: Translates data formats.
  7. 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:

  1. Link Layer: Corresponds to the OSI Physical and Data Link layers.
  2. Network Layer: Manages IP and routing.
  3. Transport Layer: Implements protocols like TCP, UDP, and SCTP.
  4. Application Layer: Handles protocols like HTTP, DNS, and SMTP.

Linux Source Code Examples

Located in /drivers/net/. For example:

1
2
3
4
5
6
7
// Ethernet example (e1000_main.c)
static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent) {
struct net_device *netdev = alloc_etherdev(sizeof(struct e1000_adapter));
pci_enable_device(pdev);
// Other hardware initialization
return register_netdev(netdev);
}

Network Layer (IP Routing)

The routing implementation resides in /net/ipv4/route.c.

1
2
3
4
5
6
// Example: ip_forward()
int ip_forward(struct sk_buff *skb) {
struct iphdr *iph = ip_hdr(skb);
// Check packet validity, route, and forward
return dst_output(dev_net(skb->dev), skb->sk, skb);
}

Transport Layer (TCP Implementation)

TCP is implemented in /net/ipv4/tcp.c.

1
2
3
4
5
6
// Example: tcp_transmit_skb()
void tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int rst) {
// Prepare and send TCP packet
tcp_queue_skb(sk, skb);
ip_queue_xmit(skb, NULL);
}

Application Layer

Applications interact with the kernel through system calls like socket(). Example:

1
2
3
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
connect(sockfd, &server_addr, sizeof(server_addr));
send(sockfd, data, len, 0);

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 handlers are implemented in /net/netlink/.

1
2
3
4
struct nlmsghdr *nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(struct rtmsg), 0);
struct rtmsg *rtm = nlmsg_data(nlh);
// Fill rtm fields
netlink_unicast(sock, skb, pid, MSG_DONTWAIT);

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

  1. Initialization:

    • Probed via bus-specific interfaces (PCI, USB, etc.).
    • Registers itself with the networking subsystem using register_netdev().
  2. Packet Transmission:

    • Implements ndo_start_xmit() to handle outgoing packets.
  3. Packet Reception:

    • Triggers a hardware interrupt when packets arrive.
    • Uses netif_rx() or napi_schedule() to pass packets to the stack.

Driver Example: Ethernet

1
2
3
4
5
6
static netdev_tx_t my_driver_start_xmit(struct sk_buff *skb, struct net_device *dev) {
struct my_driver_priv *priv = netdev_priv(dev);
dma_map_single(&priv->pdev->dev, skb->data, skb->len, DMA_TO_DEVICE);
netdev_tx_completed_queue(priv->tx_queue, 1, skb->len);
return NETDEV_TX_OK;
}

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
2
3
4
5
static int my_wifi_probe(struct pci_dev *pdev, const struct pci_device_id *id) {
struct ieee80211_hw *hw = ieee80211_alloc_hw(sizeof(struct my_priv), &my_ops);
pci_enable_device(pdev);
return ieee80211_register_hw(hw);
}

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
2
hci_dev = hci_register_dev(hdev);
hci_register_proto(&my_proto);

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:

  1. Unified Sockets API: Applications use socket() regardless of the underlying transport (Ethernet, Wi-Fi, Bluetooth).
  2. Common Configuration Tools: Tools like ifconfig, ip, and iw configure all types of network interfaces.
  3. 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.