Network Stack (TCP/IP)
Relevant source files
The following files were used as context for generating this wiki page:
The Zephyr network stack is a native, highly modular, and configurable IP stack designed for resource-constrained IoT devices. It replaces legacy implementations (like uIP) with a scalable architecture that supports multiple hardware architectures and network technologies simultaneously. The stack is optimized for low memory consumption, allowing developers to enable only the specific features required for their application via build-time configuration.
Sources: doc/connectivity/networking/overview.rst:11-15, doc/releases/release-notes-1.7.rst:34-36, README.rst:22-24
Architecture and Design
The network stack follows a layered approach, abstracting hardware complexities through Layer 2 (L2) drivers while providing a consistent API for application-level protocols. It is integrated into the OS services layer of the Zephyr architecture, positioned between the kernel and application services.
Layered Structure
The stack is organized into distinct logical layers to ensure modularity:
- Application Layer: Includes high-level protocols like MQTT, HTTP, LwM2M, and CoAP.
- Transport Layer: Implements standard TCP and UDP protocols.
- Network Layer: Core IP functionality supporting both IPv4 and IPv6 concurrently (Dual Stack).
- Data Link Layer (L2): Abstracted interfaces for Ethernet, Wi-Fi, 802.15.4, and Bluetooth (IPSP).
Sources: doc/connectivity/networking/overview.rst:133-149, doc/security/security-overview.rst:193-199, doc/releases/release-notes-1.7.rst:75-80
Native IP Stack Flow
The following diagram illustrates how network data transitions from physical hardware through the native IP stack to the application.
The Zephyr network stack provides a common interface for different link layers, allowing applications to remain agnostic of the underlying technology.
Sources: doc/connectivity/networking/overview.rst:118-124, doc/releases/release-notes-1.7.rst:78-83
Core Networking Features
IP Protocol Support
Zephyr supports both modern and legacy IP versions, which can be enabled individually or simultaneously.
| Feature | Description | Reference |
|---|---|---|
| IPv6 | Full support including SLAAC, DHCPv6, and header compression (6lo). | RFC 8200, 4862 |
| IPv4 | Supported for Ethernet, Wi-Fi, and Cellular. Includes DHCP client/server. | RFC 791, 2131 |
| Dual Stack | Concurrent usage of both IPv4 and IPv6. | overview.rst:44 |
| TCP | Transmission Control Protocol with both client and server roles. | RFC 793 |
| UDP | User Datagram Protocol for connectionless communication. | RFC 768 |
Sources: doc/connectivity/networking/overview.rst:17-42, doc/releases/release-notes-1.7.rst:75-76
Application Protocols and APIs
The stack provides a rich set of libraries and APIs to simplify IoT development:
- BSD Sockets API: A compatible subset of the BSD sockets interface for both stream (TCP) and datagram (UDP) communication.
- Management APIs: Specialized APIs for Network, Wi-Fi, and Ethernet management, allowing applications to listen for events like address changes or interface status.
- IoT Protocols: Built-in support for MQTT (v3.1.1/v5.0), CoAP, LwM2M, and HTTP (1.1/2).
Sources: doc/connectivity/networking/overview.rst:54-94, doc/releases/release-notes-1.7.rst:95-101
Connectivity and Link Layers
The Zephyr OS can manage multiple network technologies at once. While no automatic routing is provided between technologies, the stack allows packets to be directed to specific network interfaces.
Security and Quality Assurance
Security is a primary consideration in the network stack's design. The stack leverages Mbed TLS for secure communication via TLS and DTLS.
Security Mechanisms
- Monolithic Design: The kernel and network stack are compiled into a single static binary, eliminating dynamic loading vulnerabilities.
- Execution Protection: Includes stack protection, memory separation, and thread separation to isolate network processing from other critical system tasks.
- Secure Sockets: Experimental support for TLS/DTLS integrated directly into the sockets API.
Sources: doc/security/security-overview.rst:56-78, doc/connectivity/networking/overview.rst:59-62
Quality Controls
The network stack undergoes rigorous validation:
- Static Analysis: Regular checks to identify coding errors and vulnerabilities.
- Code Review: Mandatory reviews by subsystem maintainers for every contribution.
- Traffic Classification: Support for prioritizing network packets based on application requirements.
Sources: doc/security/security-overview.rst:83-99, doc/connectivity/networking/overview.rst:110-112
Source Tree Structure
The networking stack is organized within the subsys/net/ directory of the Zephyr repository.
| Directory | Content |
|---|---|
subsys/net/ip/ | Core IP stack code (IPv4, IPv6, TCP, UDP). |
subsys/net/l2/ | Layer 2 code (Ethernet, Wi-Fi, 802.15.4). |
subsys/net/lib/ | Application-level protocols (DNS, MQTT, HTTP). |
include/zephyr/net/ | Public API header files. |
samples/net/ | Reference applications and sample code. |
Sources: doc/connectivity/networking/overview.rst:133-149, doc/releases/release-notes-1.7.rst:74
The Zephyr Network Stack (TCP/IP) provides a robust foundation for IoT connectivity, balancing the need for standard-compliant protocols with the strict resource constraints of embedded microcontrollers. Through its modular architecture and native implementation, it ensures high performance and security for a wide variety of hardware platforms.