rustnet-host 0.2.0

Per-connection process attribution for rustnet: eBPF/procfs on Linux, PKTAP/lsof on macOS, IP Helper on Windows, sockstat on FreeBSD, behind one ProcessLookup trait
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! eBPF map interaction utilities for libbpf-rs

use super::ProcessInfo;
use anyhow::Result;
use libbpf_rs::MapCore;
use std::net::{Ipv4Addr, Ipv6Addr};

/// Connection key matching the eBPF program structure (supports IPv4 and IPv6)
#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
pub struct ConnKey {
    pub saddr: [u32; 4], // IPv4 uses only saddr[0], IPv6 uses all 4
    pub daddr: [u32; 4], // IPv4 uses only daddr[0], IPv6 uses all 4
    pub sport: u16,
    pub dport: u16,
    pub proto: u8,  // IPPROTO_TCP or IPPROTO_UDP
    pub family: u8, // AF_INET or AF_INET6
}

/// Connection info matching the eBPF program structure
#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
pub struct ConnInfo {
    pub pid: u32,
    pub uid: u32,
    pub comm: [u8; 16],
    pub timestamp: u64,
}

// AF_INET / AF_INET6 (matches kernel `<linux/socket.h>` numeric values).
const AF_INET: u8 = 2;
const AF_INET6: u8 = 10;

// IPPROTO_* values used by the eBPF socket map.
const IPPROTO_ICMP: u8 = 1;
const IPPROTO_TCP: u8 = 6;
const IPPROTO_UDP: u8 = 17;
const IPPROTO_ICMPV6: u8 = 58;

impl ConnKey {
    /// Build an empty key for an IPv4 connection. Address fields stay zeroed
    /// and are filled by [`Self::fill_v4`].
    fn empty_v4(sport: u16, dport: u16, proto: u8) -> Self {
        Self {
            saddr: [0; 4],
            daddr: [0; 4],
            sport,
            dport,
            proto,
            family: AF_INET,
        }
    }

    /// Build an empty key for an IPv6 connection. Address fields stay zeroed
    /// and are filled by [`Self::fill_v6`].
    fn empty_v6(sport: u16, dport: u16, proto: u8) -> Self {
        Self {
            saddr: [0; 4],
            daddr: [0; 4],
            sport,
            dport,
            proto,
            family: AF_INET6,
        }
    }

    fn fill_v4(&mut self, src_ip: Ipv4Addr, dst_ip: Ipv4Addr) {
        // Use little-endian to match kernel/eBPF native format.
        self.saddr[0] = u32::from_le_bytes(src_ip.octets());
        self.daddr[0] = u32::from_le_bytes(dst_ip.octets());
    }

    fn fill_v6(&mut self, src_ip: Ipv6Addr, dst_ip: Ipv6Addr) {
        let src_bytes = src_ip.octets();
        let dst_bytes = dst_ip.octets();

        // Convert 16-byte IPv6 addresses to 4 u32 values (big-endian).
        for i in 0..4 {
            let start = i * 4;
            self.saddr[i] = u32::from_be_bytes([
                src_bytes[start],
                src_bytes[start + 1],
                src_bytes[start + 2],
                src_bytes[start + 3],
            ]);
            self.daddr[i] = u32::from_be_bytes([
                dst_bytes[start],
                dst_bytes[start + 1],
                dst_bytes[start + 2],
                dst_bytes[start + 3],
            ]);
        }
    }

    pub fn new_v4(
        src_ip: Ipv4Addr,
        dst_ip: Ipv4Addr,
        src_port: u16,
        dst_port: u16,
        is_tcp: bool,
    ) -> Self {
        let proto = if is_tcp { IPPROTO_TCP } else { IPPROTO_UDP };
        let mut key = Self::empty_v4(src_port, dst_port, proto);
        key.fill_v4(src_ip, dst_ip);
        key
    }

    pub(crate) fn new_v6(
        src_ip: Ipv6Addr,
        dst_ip: Ipv6Addr,
        src_port: u16,
        dst_port: u16,
        is_tcp: bool,
    ) -> Self {
        let proto = if is_tcp { IPPROTO_TCP } else { IPPROTO_UDP };
        let mut key = Self::empty_v6(src_port, dst_port, proto);
        key.fill_v6(src_ip, dst_ip);
        key
    }

    /// Create an IPv4 ICMP lookup key. `icmp_id` acts as the "source port",
    /// `dport` is 0.
    pub fn new_icmp_v4(src_ip: Ipv4Addr, dst_ip: Ipv4Addr, icmp_id: u16) -> Self {
        let mut key = Self::empty_v4(icmp_id, 0, IPPROTO_ICMP);
        key.fill_v4(src_ip, dst_ip);
        key
    }

    /// Create an IPv6 ICMPv6 lookup key. `icmp_id` acts as the "source port",
    /// `dport` is 0.
    pub fn new_icmp_v6(src_ip: Ipv6Addr, dst_ip: Ipv6Addr, icmp_id: u16) -> Self {
        let mut key = Self::empty_v6(icmp_id, 0, IPPROTO_ICMPV6);
        key.fill_v6(src_ip, dst_ip);
        key
    }

    /// Convert to bytes for map lookup
    pub fn as_bytes(&self) -> [u8; 38] {
        // SAFETY: ConnKey is #[repr(C, packed)] with no padding (4×u32 + 4×u32 +
        // u16 + u16 + u8 + u8 = 38 bytes). All bit patterns are valid u8 values.
        unsafe { std::mem::transmute(*self) }
    }
}

impl From<ConnInfo> for ProcessInfo {
    fn from(info: ConnInfo) -> Self {
        // Convert C string to Rust String
        let comm_len = info.comm.iter().position(|&x| x == 0).unwrap_or(16);
        let comm = String::from_utf8_lossy(&info.comm[..comm_len]).to_string();

        Self {
            pid: info.pid,
            uid: info.uid,
            comm,
            timestamp: info.timestamp,
        }
    }
}

pub struct MapReader;

impl MapReader {
    /// Query the socket map for connection information using libbpf-rs
    pub fn lookup_connection(map: &libbpf_rs::Map, key: ConnKey) -> Result<Option<ProcessInfo>> {
        let key_bytes = key.as_bytes();

        match map.lookup(&key_bytes, libbpf_rs::MapFlags::empty()) {
            Ok(Some(value_bytes)) => {
                if value_bytes.len() != 32 {
                    return Err(anyhow::anyhow!(
                        "Invalid map value size: expected 32, got {}",
                        value_bytes.len()
                    ));
                }

                let mut info_bytes = [0u8; 32];
                info_bytes.copy_from_slice(&value_bytes);
                // SAFETY: ConnInfo is #[repr(C, packed)] with size 32 bytes
                // (u32 + u32 + [u8; 16] + u64). All field types accept arbitrary
                // bit patterns, so any 32 bytes constitute a valid ConnInfo.
                let conn_info: ConnInfo = unsafe { std::mem::transmute(info_bytes) };
                Ok(Some(conn_info.into()))
            }
            Ok(None) => Ok(None),
            Err(e) => {
                log::debug!("eBPF map lookup failed: {}", e);
                Ok(None)
            }
        }
    }

    /// Clean up stale entries from the map based on timestamp
    pub fn cleanup_stale_entries(map: &libbpf_rs::Map, stale_threshold_ns: u64) -> Result<u32> {
        use std::time::{SystemTime, UNIX_EPOCH};

        let current_time_ns = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| anyhow::anyhow!("Time error: {}", e))?
            .as_nanos() as u64;

        let mut cleanup_count = 0u32;
        let mut keys_to_delete = Vec::new();

        // Try to iterate using MapKeyIter
        for key in map.keys() {
            // We have a key, check if its value is stale
            if let Ok(Some(value_bytes)) = map.lookup(&key, libbpf_rs::MapFlags::empty())
                && value_bytes.len() >= 32
            {
                // Extract timestamp from last 8 bytes
                let timestamp_bytes = &value_bytes[24..32];
                let timestamp = u64::from_ne_bytes([
                    timestamp_bytes[0],
                    timestamp_bytes[1],
                    timestamp_bytes[2],
                    timestamp_bytes[3],
                    timestamp_bytes[4],
                    timestamp_bytes[5],
                    timestamp_bytes[6],
                    timestamp_bytes[7],
                ]);

                if current_time_ns.saturating_sub(timestamp) > stale_threshold_ns {
                    // Entry is stale, mark for deletion
                    keys_to_delete.push(key);
                    log::debug!(
                        "Found stale entry, timestamp: {}, current: {}, threshold: {}",
                        timestamp,
                        current_time_ns,
                        stale_threshold_ns
                    );
                }
            }
        }

        // Delete all stale entries
        for key in keys_to_delete {
            if let Err(e) = map.delete(&key) {
                log::debug!("Failed to delete stale entry: {}", e);
            } else {
                cleanup_count += 1;
            }
        }

        if cleanup_count > 0 {
            log::info!("eBPF cleanup: removed {} stale entries", cleanup_count);
        }

        Ok(cleanup_count)
    }

    /// Log map lookup details for debugging
    pub fn debug_lookup_miss(map: &libbpf_rs::Map, lookup_key: &ConnKey) -> Result<()> {
        log::info!("=== eBPF Map Lookup Miss Debug ===");

        // Copy fields to avoid packed struct alignment issues
        let saddr = lookup_key.saddr[0];
        let daddr = lookup_key.daddr[0];
        let sport = lookup_key.sport;
        let dport = lookup_key.dport;
        let proto = lookup_key.proto;
        let family = lookup_key.family;

        log::info!(
            "Looking for key: saddr={:08x} ({}.{}.{}.{}), daddr={:08x} ({}.{}.{}.{}), sport={}, dport={}, proto={}, family={}",
            saddr,
            saddr & 0xff,
            (saddr >> 8) & 0xff,
            (saddr >> 16) & 0xff,
            (saddr >> 24) & 0xff,
            daddr,
            daddr & 0xff,
            (daddr >> 8) & 0xff,
            (daddr >> 16) & 0xff,
            (daddr >> 24) & 0xff,
            sport,
            dport,
            proto,
            family
        );

        log::info!("Key bytes: {:02x?}", lookup_key.as_bytes());

        // Get map info
        let info = map.info();
        match info {
            Ok(map_info) => {
                log::info!(
                    "Map type: {:?}, max_entries: {}, key_size: {}, value_size: {}",
                    map_info.map_type(),
                    map_info.info.max_entries,
                    map_info.info.key_size,
                    map_info.info.value_size
                );
            }
            Err(e) => {
                log::debug!("Failed to get map info: {}", e);
            }
        }

        log::info!("=== End Lookup Debug ===");
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ConnKey is #[repr(C, packed)], so test asserts copy each field into
    // a local first — taking a reference to a packed field (including the
    // implicit one assert_eq! creates) is E0793 under Rust 2024.
    #[test]
    fn new_v4_writes_little_endian_addrs_and_tcp_proto() {
        let key = ConnKey::new_v4(
            Ipv4Addr::new(10, 0, 0, 1),
            Ipv4Addr::new(192, 168, 1, 100),
            12345,
            443,
            true,
        );
        let saddr = key.saddr;
        let daddr = key.daddr;
        let sport = key.sport;
        let dport = key.dport;
        assert_eq!(key.family, AF_INET);
        assert_eq!(key.proto, IPPROTO_TCP);
        assert_eq!(sport, 12345);
        assert_eq!(dport, 443);
        // Octets reinterpreted as host-order u32 — matches what
        // u32::from_le_bytes(ip.octets()) produced previously.
        assert_eq!(
            saddr[0],
            u32::from_le_bytes(Ipv4Addr::new(10, 0, 0, 1).octets())
        );
        assert_eq!(
            daddr[0],
            u32::from_le_bytes(Ipv4Addr::new(192, 168, 1, 100).octets())
        );
        // Upper IPv6 slots stay zeroed for IPv4 keys.
        assert_eq!(&saddr[1..], &[0, 0, 0]);
        assert_eq!(&daddr[1..], &[0, 0, 0]);
    }

    #[test]
    fn new_v4_marks_udp_when_not_tcp() {
        let key = ConnKey::new_v4(
            Ipv4Addr::new(127, 0, 0, 1),
            Ipv4Addr::new(8, 8, 8, 8),
            53000,
            53,
            false,
        );
        assert_eq!(key.proto, IPPROTO_UDP);
    }

    #[test]
    fn new_v6_writes_big_endian_addrs_across_all_four_slots() {
        let src = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
        let dst = Ipv6Addr::new(0xfe80, 0, 0, 0, 0x1234, 0x5678, 0x9abc, 0xdef0);
        let key = ConnKey::new_v6(src, dst, 1, 2, true);
        let saddr = key.saddr;
        let daddr = key.daddr;
        assert_eq!(key.family, AF_INET6);
        assert_eq!(key.proto, IPPROTO_TCP);

        let src_bytes = src.octets();
        let dst_bytes = dst.octets();
        for i in 0..4 {
            let start = i * 4;
            assert_eq!(
                saddr[i],
                u32::from_be_bytes([
                    src_bytes[start],
                    src_bytes[start + 1],
                    src_bytes[start + 2],
                    src_bytes[start + 3],
                ])
            );
            assert_eq!(
                daddr[i],
                u32::from_be_bytes([
                    dst_bytes[start],
                    dst_bytes[start + 1],
                    dst_bytes[start + 2],
                    dst_bytes[start + 3],
                ])
            );
        }
    }

    #[test]
    fn new_icmp_v4_uses_icmp_proto_and_zero_dport() {
        let key = ConnKey::new_icmp_v4(
            Ipv4Addr::new(10, 0, 0, 1),
            Ipv4Addr::new(8, 8, 8, 8),
            0x4242,
        );
        let sport = key.sport;
        let dport = key.dport;
        assert_eq!(key.proto, IPPROTO_ICMP);
        assert_eq!(key.family, AF_INET);
        assert_eq!(sport, 0x4242);
        assert_eq!(dport, 0);
    }

    #[test]
    fn new_icmp_v6_uses_icmpv6_proto_and_zero_dport() {
        let src = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
        let dst = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 2);
        let key = ConnKey::new_icmp_v6(src, dst, 0x0101);
        let sport = key.sport;
        let dport = key.dport;
        assert_eq!(key.proto, IPPROTO_ICMPV6);
        assert_eq!(key.family, AF_INET6);
        assert_eq!(sport, 0x0101);
        assert_eq!(dport, 0);
    }
}