⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

veridian_kernel/net/
mod.rs

1//! Network stack for VeridianOS
2//!
3//! Provides TCP/IP networking capabilities including:
4//! - IP layer (IPv4/IPv6)
5//! - TCP protocol
6//! - UDP protocol
7//! - Socket API
8//! - Network device abstraction
9
10pub mod arp;
11pub mod asn1;
12pub mod device;
13pub mod dhcp;
14pub mod dma_pool;
15pub mod epoll;
16pub mod ethernet;
17pub mod icmpv6;
18pub mod integration;
19pub mod ip;
20pub mod ipv6;
21pub mod kerberos;
22pub mod ldap;
23pub mod socket;
24pub mod tcp;
25pub mod udp;
26pub mod unix_socket;
27pub mod zero_copy;
28
29// Phase 7.5 Wave 4: Networking Foundations
30pub mod bonding;
31pub mod congestion;
32pub mod dns;
33pub mod multicast;
34pub mod tcp_sack;
35pub mod vlan;
36
37// Phase 7.5 Wave 5: Crypto & Protocols
38pub mod http;
39pub mod mdns;
40pub mod ntp;
41pub mod quic;
42pub mod ssh;
43pub mod tls;
44pub mod wireguard;
45
46// Phase 8 Wave 2: Networking v2
47pub mod firewall;
48
49// Phase 10: Netlink IPC
50pub mod netlink;
51pub mod routing;
52pub mod vpn;
53
54use alloc::vec::Vec;
55
56use crate::error::KernelError;
57
58/// MAC address (6 bytes)
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub struct MacAddress(pub [u8; 6]);
61
62impl MacAddress {
63    pub const BROADCAST: Self = Self([0xFF; 6]);
64    pub const ZERO: Self = Self([0x00; 6]);
65
66    pub const fn new(bytes: [u8; 6]) -> Self {
67        Self(bytes)
68    }
69}
70
71/// IPv4 address (4 bytes)
72#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
73pub struct Ipv4Address(pub [u8; 4]);
74
75impl Ipv4Address {
76    pub const LOCALHOST: Self = Self([127, 0, 0, 1]);
77    pub const BROADCAST: Self = Self([255, 255, 255, 255]);
78    pub const ANY: Self = Self([0, 0, 0, 0]);
79    pub const UNSPECIFIED: Self = Self([0, 0, 0, 0]);
80
81    pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Self {
82        Self([a, b, c, d])
83    }
84
85    pub fn from_u32(addr: u32) -> Self {
86        Self(addr.to_be_bytes())
87    }
88
89    pub fn to_u32(&self) -> u32 {
90        u32::from_be_bytes(self.0)
91    }
92}
93
94/// IPv6 address (16 bytes)
95#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
96pub struct Ipv6Address(pub [u8; 16]);
97
98impl Ipv6Address {
99    pub const LOCALHOST: Self = Self([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
100    pub const UNSPECIFIED: Self = Self([0; 16]);
101    pub const ANY: Self = Self([0; 16]);
102    pub const ALL_NODES_LINK_LOCAL: Self =
103        Self([0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
104    pub const ALL_ROUTERS_LINK_LOCAL: Self =
105        Self([0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]);
106}
107
108/// IP address (v4 or v6)
109#[derive(Debug, Clone, Copy, PartialEq, Eq)]
110pub enum IpAddress {
111    V4(Ipv4Address),
112    V6(Ipv6Address),
113}
114
115/// Port number
116pub type Port = u16;
117
118/// Socket address (IP + port)
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub struct SocketAddr {
121    pub ip: IpAddress,
122    pub port: Port,
123}
124
125impl SocketAddr {
126    pub fn new(ip: IpAddress, port: Port) -> Self {
127        Self { ip, port }
128    }
129
130    pub fn v4(addr: Ipv4Address, port: Port) -> Self {
131        Self {
132            ip: IpAddress::V4(addr),
133            port,
134        }
135    }
136
137    pub fn v6(addr: Ipv6Address, port: Port) -> Self {
138        Self {
139            ip: IpAddress::V6(addr),
140            port,
141        }
142    }
143
144    pub fn ip(&self) -> IpAddress {
145        self.ip
146    }
147
148    pub fn port(&self) -> Port {
149        self.port
150    }
151}
152
153/// Network packet
154#[derive(Clone)]
155pub struct Packet {
156    data: Vec<u8>,
157    length: usize,
158}
159
160impl Packet {
161    pub fn new(size: usize) -> Self {
162        Self {
163            data: alloc::vec![0u8; size],
164            length: 0,
165        }
166    }
167
168    pub fn from_bytes(bytes: &[u8]) -> Self {
169        Self {
170            data: bytes.to_vec(),
171            length: bytes.len(),
172        }
173    }
174
175    pub fn data(&self) -> &[u8] {
176        &self.data[..self.length]
177    }
178
179    pub fn len(&self) -> usize {
180        self.length
181    }
182
183    pub fn is_empty(&self) -> bool {
184        self.length == 0
185    }
186
187    pub fn set_length(&mut self, len: usize) {
188        self.length = len.min(self.data.len());
189    }
190}
191
192/// Network statistics
193#[derive(Debug, Default, Clone, Copy)]
194pub struct NetworkStats {
195    pub packets_sent: u64,
196    pub packets_received: u64,
197    pub bytes_sent: u64,
198    pub bytes_received: u64,
199    pub errors: u64,
200}
201
202static STATS: spin::Mutex<NetworkStats> = spin::Mutex::new(NetworkStats {
203    packets_sent: 0,
204    packets_received: 0,
205    bytes_sent: 0,
206    bytes_received: 0,
207    errors: 0,
208});
209
210/// Update network statistics
211pub fn update_stats_tx(bytes: usize) {
212    let mut stats = STATS.lock();
213    stats.packets_sent += 1;
214    stats.bytes_sent += bytes as u64;
215}
216
217pub fn update_stats_rx(bytes: usize) {
218    let mut stats = STATS.lock();
219    stats.packets_received += 1;
220    stats.bytes_received += bytes as u64;
221}
222
223pub fn get_stats() -> NetworkStats {
224    *STATS.lock()
225}
226
227/// Initialize network stack
228pub fn init() -> Result<(), KernelError> {
229    println!("[NET] Initializing network stack...");
230
231    // Initialize DMA buffer pool for zero-copy networking
232    dma_pool::init_network_pool(256)?;
233
234    // Initialize device layer
235    device::init()?;
236
237    // Initialize IP layer
238    ip::init()?;
239
240    // Initialize TCP
241    tcp::init()?;
242
243    // Initialize UDP
244    udp::init()?;
245
246    // Initialize IPv6
247    ipv6::init()?;
248
249    // Initialize ICMPv6
250    icmpv6::init()?;
251
252    // Initialize socket layer
253    socket::init()?;
254
255    // Initialize epoll I/O multiplexing
256    epoll::init()?;
257
258    // Register hardware network drivers
259    if let Err(_e) = integration::register_drivers() {
260        println!(
261            "[NET] Warning: driver registration failed (non-fatal): {:?}",
262            _e
263        );
264    }
265
266    println!("[NET] Network stack initialized");
267    Ok(())
268}
269
270#[cfg(test)]
271mod tests {
272    use super::*;
273
274    #[test]
275    fn test_ipv4_address() {
276        let addr = Ipv4Address::new(192, 168, 1, 1);
277        assert_eq!(addr.0, [192, 168, 1, 1]);
278    }
279
280    #[test]
281    fn test_mac_address() {
282        let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
283        assert_eq!(mac.0[0], 0x00);
284        assert_eq!(mac.0[5], 0x55);
285    }
286
287    #[test]
288    fn test_packet() {
289        let data = b"Hello, Network!";
290        let pkt = Packet::from_bytes(data);
291        assert_eq!(pkt.data(), data);
292    }
293}