nlink 0.15.1

Async netlink library for Linux network configuration
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! Audit implementation for `Connection<Audit>`.
//!
//! This module provides methods for interacting with the Linux Audit subsystem
//! via the NETLINK_AUDIT protocol.
//!
//! # Example
//!
//! ```ignore
//! use nlink::netlink::{Connection, Audit};
//!
//! let conn = Connection::<Audit>::new()?;
//!
//! // Get audit status
//! let status = conn.get_status().await?;
//! println!("Audit enabled: {}", status.enabled);
//! println!("PID: {}", status.pid);
//! println!("Backlog: {}/{}", status.backlog, status.backlog_limit);
//! ```

use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

use super::{
    connection::Connection,
    error::{Error, Result},
    protocol::{Audit, ProtocolState},
    socket::NetlinkSocket,
};

// Netlink constants
const NLMSG_ERROR: u16 = 2;
const NLM_F_REQUEST: u16 = 0x01;
const NLM_F_ACK: u16 = 0x04;

// Netlink header size
const NLMSG_HDRLEN: usize = 16;

// Audit message types (from linux/audit.h)
/// Get status
const AUDIT_GET: u16 = 1000;
/// Set status
const AUDIT_SET: u16 = 1001;
/// List syscall rules (deprecated)
const AUDIT_LIST: u16 = 1002;
/// Add syscall rule (deprecated)
const AUDIT_ADD: u16 = 1003;
/// Delete syscall rule (deprecated)
const AUDIT_DEL: u16 = 1004;
/// User-space message
const AUDIT_USER: u16 = 1005;
/// Define the login id and information
const AUDIT_LOGIN: u16 = 1006;
/// Insert file/dir watch entry
const AUDIT_WATCH_INS: u16 = 1007;
/// Remove file/dir watch entry
const AUDIT_WATCH_REM: u16 = 1008;
/// List all file/dir watches
const AUDIT_WATCH_LIST: u16 = 1009;
/// Get info about sender of signal to auditd
const AUDIT_SIGNAL_INFO: u16 = 1010;
/// Add syscall filtering rule
const AUDIT_ADD_RULE: u16 = 1011;
/// Delete syscall filtering rule
const AUDIT_DEL_RULE: u16 = 1012;
/// List syscall filtering rules
const AUDIT_LIST_RULES: u16 = 1013;
/// Trim junk from watched tree
const AUDIT_TRIM: u16 = 1014;
/// Append to watched tree
const AUDIT_MAKE_EQUIV: u16 = 1015;
/// Get TTY auditing status
const AUDIT_TTY_GET: u16 = 1016;
/// Set TTY auditing status
const AUDIT_TTY_SET: u16 = 1017;
/// Turn an audit feature on or off
const AUDIT_SET_FEATURE: u16 = 1018;
/// Get audit feature state
const AUDIT_GET_FEATURE: u16 = 1019;

// Audit event message types
/// Syscall event
const AUDIT_SYSCALL: u16 = 1300;
/// Filename path information
const AUDIT_PATH: u16 = 1302;
/// IPC record
const AUDIT_IPC: u16 = 1303;
/// Socket address
const AUDIT_SOCKADDR: u16 = 1306;
/// Current working directory
const AUDIT_CWD: u16 = 1307;
/// execve arguments
const AUDIT_EXECVE: u16 = 1309;
/// IPC new permissions record
const AUDIT_IPC_SET_PERM: u16 = 1311;
/// POSIX MQ open record
const AUDIT_MQ_OPEN: u16 = 1312;
/// POSIX MQ send/receive record
const AUDIT_MQ_SENDRECV: u16 = 1313;
/// POSIX MQ notify record
const AUDIT_MQ_NOTIFY: u16 = 1314;
/// POSIX MQ get/set attribute record
const AUDIT_MQ_GETSETATTR: u16 = 1315;
/// End of multi-record event
const AUDIT_EOE: u16 = 1320;
/// Seccomp filter info
const AUDIT_SECCOMP: u16 = 1326;
/// Process title information
const AUDIT_PROCTITLE: u16 = 1327;
/// BPF subsystem info
const AUDIT_BPF: u16 = 1334;

// SELinux AVC message
const AUDIT_AVC: u16 = 1400;

// Audit status mask bits
const AUDIT_STATUS_ENABLED: u32 = 0x0001;
const AUDIT_STATUS_FAILURE: u32 = 0x0002;
const AUDIT_STATUS_PID: u32 = 0x0004;
const AUDIT_STATUS_RATE_LIMIT: u32 = 0x0008;
const AUDIT_STATUS_BACKLOG_LIMIT: u32 = 0x0010;
const AUDIT_STATUS_BACKLOG_WAIT_TIME: u32 = 0x0020;
const AUDIT_STATUS_LOST: u32 = 0x0040;

// Failure modes
/// Silent (log to syslog)
const AUDIT_FAIL_SILENT: u32 = 0;
/// Print rate limit
const AUDIT_FAIL_PRINTK: u32 = 1;
/// Panic
const AUDIT_FAIL_PANIC: u32 = 2;

/// Audit status structure (from linux/audit.h).
///
/// This structure is used to get/set the audit daemon configuration.
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, FromBytes, IntoBytes, Immutable, KnownLayout)]
pub struct AuditStatus {
    /// Bit mask for valid entries.
    pub mask: u32,
    /// 1 = enabled, 0 = disabled, 2 = immutable.
    pub enabled: u32,
    /// Failure-to-log action.
    pub failure: u32,
    /// PID of auditd process.
    pub pid: u32,
    /// Message rate limit (per second).
    pub rate_limit: u32,
    /// Waiting messages limit.
    pub backlog_limit: u32,
    /// Messages lost.
    pub lost: u32,
    /// Messages waiting in queue.
    pub backlog: u32,
    /// Kernel audit feature bitmap / version.
    pub feature_bitmap: u32,
    /// Message queue wait timeout (kernel >= 3.14).
    pub backlog_wait_time: u32,
    /// Backlog wait time actual (kernel >= 5.16).
    pub backlog_wait_time_actual: u32,
}

impl AuditStatus {
    /// Check if auditing is enabled.
    pub fn is_enabled(&self) -> bool {
        self.enabled == 1
    }

    /// Check if auditing is locked (immutable).
    pub fn is_locked(&self) -> bool {
        self.enabled == 2
    }

    /// Get the failure mode as an enum.
    pub fn failure_mode(&self) -> AuditFailureMode {
        AuditFailureMode::from_u32(self.failure)
    }
}

/// Audit failure mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuditFailureMode {
    /// Silent - discard failed audit messages.
    Silent,
    /// Printk - log to syslog on failure.
    Printk,
    /// Panic - kernel panic on audit failure.
    Panic,
    /// Unknown failure mode.
    Unknown(u32),
}

impl AuditFailureMode {
    fn from_u32(val: u32) -> Self {
        match val {
            AUDIT_FAIL_SILENT => Self::Silent,
            AUDIT_FAIL_PRINTK => Self::Printk,
            AUDIT_FAIL_PANIC => Self::Panic,
            other => Self::Unknown(other),
        }
    }

    /// Get the numeric value.
    pub fn as_u32(&self) -> u32 {
        match self {
            Self::Silent => AUDIT_FAIL_SILENT,
            Self::Printk => AUDIT_FAIL_PRINTK,
            Self::Panic => AUDIT_FAIL_PANIC,
            Self::Unknown(n) => *n,
        }
    }
}

/// Audit rule data structure.
#[repr(C)]
#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout)]
pub struct AuditRuleData {
    /// Flags (AUDIT_FILTER_*).
    pub flags: u32,
    /// Action to take (AUDIT_ALWAYS, AUDIT_NEVER).
    pub action: u32,
    /// Number of fields.
    pub field_count: u32,
    /// Syscall bitmask (AUDIT_BITMASK_SIZE = 64).
    pub mask: [u32; 64],
    /// Field types (AUDIT_MAX_FIELDS = 64).
    pub fields: [u32; 64],
    /// Field values.
    pub values: [u32; 64],
    /// Field flags (MNT_ID/operators).
    pub fieldflags: [u32; 64],
    /// Length of filter string buffer.
    pub buflen: u32,
    // Followed by variable-length buffer.
}

impl Default for AuditRuleData {
    fn default() -> Self {
        Self {
            flags: 0,
            action: 0,
            field_count: 0,
            mask: [0; 64],
            fields: [0; 64],
            values: [0; 64],
            fieldflags: [0; 64],
            buflen: 0,
        }
    }
}

/// Audit TTY status.
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, FromBytes, IntoBytes, Immutable, KnownLayout)]
pub struct AuditTtyStatus {
    /// Enable/disable TTY auditing.
    pub enabled: u32,
    /// Log passwords too.
    pub log_passwd: u32,
}

/// Audit features structure.
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, FromBytes, IntoBytes, Immutable, KnownLayout)]
pub struct AuditFeatures {
    /// Currently holds version number.
    pub vers: u32,
    /// Mask of all features.
    pub mask: u32,
    /// Features currently enabled.
    pub features: u32,
    /// Features locked.
    pub lock: u32,
}

/// Audit signal info.
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, FromBytes, IntoBytes, Immutable, KnownLayout)]
pub struct AuditSignalInfo {
    /// UID of sender.
    pub uid: u32,
    /// PID of sender.
    pub pid: u32,
    /// Context string follows.
    pub ctx: u32,
}

/// Audit event type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuditEventType {
    /// Syscall event.
    Syscall,
    /// Path information.
    Path,
    /// IPC record.
    Ipc,
    /// Socket address.
    Sockaddr,
    /// Current working directory.
    Cwd,
    /// Execve arguments.
    Execve,
    /// End of event.
    EndOfEvent,
    /// Seccomp filter.
    Seccomp,
    /// Process title.
    Proctitle,
    /// BPF subsystem.
    Bpf,
    /// SELinux AVC.
    Avc,
    /// User message.
    User,
    /// Other event type.
    Other(u16),
}

impl AuditEventType {
    /// Parse an event type from its numeric value.
    #[allow(dead_code)] // Used for event monitoring (future)
    pub fn from_u16(val: u16) -> Self {
        match val {
            AUDIT_SYSCALL => Self::Syscall,
            AUDIT_PATH => Self::Path,
            AUDIT_IPC => Self::Ipc,
            AUDIT_SOCKADDR => Self::Sockaddr,
            AUDIT_CWD => Self::Cwd,
            AUDIT_EXECVE => Self::Execve,
            AUDIT_EOE => Self::EndOfEvent,
            AUDIT_SECCOMP => Self::Seccomp,
            AUDIT_PROCTITLE => Self::Proctitle,
            AUDIT_BPF => Self::Bpf,
            AUDIT_AVC => Self::Avc,
            AUDIT_USER => Self::User,
            other => Self::Other(other),
        }
    }

    /// Get the numeric value.
    pub fn as_u16(&self) -> u16 {
        match self {
            Self::Syscall => AUDIT_SYSCALL,
            Self::Path => AUDIT_PATH,
            Self::Ipc => AUDIT_IPC,
            Self::Sockaddr => AUDIT_SOCKADDR,
            Self::Cwd => AUDIT_CWD,
            Self::Execve => AUDIT_EXECVE,
            Self::EndOfEvent => AUDIT_EOE,
            Self::Seccomp => AUDIT_SECCOMP,
            Self::Proctitle => AUDIT_PROCTITLE,
            Self::Bpf => AUDIT_BPF,
            Self::Avc => AUDIT_AVC,
            Self::User => AUDIT_USER,
            Self::Other(n) => *n,
        }
    }
}

/// An audit event received from the kernel.
#[derive(Debug, Clone)]
pub struct AuditEvent {
    /// Event type.
    pub event_type: AuditEventType,
    /// Raw message type value.
    pub msg_type: u16,
    /// Event data (text format).
    pub data: String,
}

impl Connection<Audit> {
    /// Create a new Audit connection.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::{Connection, Audit};
    ///
    /// let conn = Connection::<Audit>::new()?;
    /// ```
    pub fn new() -> Result<Self> {
        let socket = NetlinkSocket::new(Audit::PROTOCOL)?;
        Ok(Self::from_parts(socket, Audit))
    }

    /// Get the current audit status.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::{Connection, Audit};
    ///
    /// let conn = Connection::<Audit>::new()?;
    /// let status = conn.get_status().await?;
    ///
    /// println!("Audit enabled: {}", status.is_enabled());
    /// println!("Audit locked: {}", status.is_locked());
    /// println!("Failure mode: {:?}", status.failure_mode());
    /// println!("Audit daemon PID: {}", status.pid);
    /// println!("Rate limit: {} msgs/sec", status.rate_limit);
    /// println!("Backlog: {}/{}", status.backlog, status.backlog_limit);
    /// println!("Lost messages: {}", status.lost);
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "get_status"))]
    pub async fn get_status(&self) -> Result<AuditStatus> {
        let seq = self.socket().next_seq();
        let pid = self.socket().pid();

        // Build request message
        let mut buf = Vec::with_capacity(32);

        // Netlink header (16 bytes)
        buf.extend_from_slice(&0u32.to_ne_bytes()); // nlmsg_len (fill later)
        buf.extend_from_slice(&AUDIT_GET.to_ne_bytes()); // nlmsg_type
        buf.extend_from_slice(&(NLM_F_REQUEST | NLM_F_ACK).to_ne_bytes()); // nlmsg_flags
        buf.extend_from_slice(&seq.to_ne_bytes()); // nlmsg_seq
        buf.extend_from_slice(&pid.to_ne_bytes()); // nlmsg_pid

        // Update length
        let len = buf.len() as u32;
        buf[0..4].copy_from_slice(&len.to_ne_bytes());

        // Send request
        self.socket().send(&buf).await?;

        // Receive response
        let data = self.socket().recv_msg().await?;

        if data.len() < NLMSG_HDRLEN {
            return Err(Error::InvalidMessage("response too short".into()));
        }

        let nlmsg_type = u16::from_ne_bytes([data[4], data[5]]);

        if nlmsg_type == NLMSG_ERROR {
            if data.len() >= 20 {
                let errno = i32::from_ne_bytes([data[16], data[17], data[18], data[19]]);
                if errno != 0 {
                    return Err(Error::from_errno(-errno));
                }
            }
            // errno == 0 means success ACK, wait for actual response
            let data = self.socket().recv_msg().await?;
            return self.parse_status_response(&data);
        }

        self.parse_status_response(&data)
    }

    /// Parse status response.
    fn parse_status_response(&self, data: &[u8]) -> Result<AuditStatus> {
        if data.len() < NLMSG_HDRLEN {
            return Err(Error::InvalidMessage("response too short".into()));
        }

        let nlmsg_type = u16::from_ne_bytes([data[4], data[5]]);

        if nlmsg_type != AUDIT_GET {
            return Err(Error::InvalidMessage(format!(
                "unexpected message type: {}",
                nlmsg_type
            )));
        }

        if data.len() < NLMSG_HDRLEN + std::mem::size_of::<AuditStatus>() {
            // Handle older kernels with smaller status struct
            let mut status = AuditStatus::default();
            let payload = &data[NLMSG_HDRLEN..];

            if payload.len() >= 32 {
                status.mask = u32::from_ne_bytes([payload[0], payload[1], payload[2], payload[3]]);
                status.enabled =
                    u32::from_ne_bytes([payload[4], payload[5], payload[6], payload[7]]);
                status.failure =
                    u32::from_ne_bytes([payload[8], payload[9], payload[10], payload[11]]);
                status.pid =
                    u32::from_ne_bytes([payload[12], payload[13], payload[14], payload[15]]);
                status.rate_limit =
                    u32::from_ne_bytes([payload[16], payload[17], payload[18], payload[19]]);
                status.backlog_limit =
                    u32::from_ne_bytes([payload[20], payload[21], payload[22], payload[23]]);
                status.lost =
                    u32::from_ne_bytes([payload[24], payload[25], payload[26], payload[27]]);
                status.backlog =
                    u32::from_ne_bytes([payload[28], payload[29], payload[30], payload[31]]);
            }

            return Ok(status);
        }

        let (status, _) = AuditStatus::ref_from_prefix(&data[NLMSG_HDRLEN..])
            .map_err(|_| Error::InvalidMessage("failed to parse audit status".into()))?;

        Ok(*status)
    }

    /// Get the TTY auditing status.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::{Connection, Audit};
    ///
    /// let conn = Connection::<Audit>::new()?;
    /// let tty_status = conn.get_tty_status().await?;
    ///
    /// println!("TTY auditing enabled: {}", tty_status.enabled != 0);
    /// println!("Log passwords: {}", tty_status.log_passwd != 0);
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "get_tty_status"))]
    pub async fn get_tty_status(&self) -> Result<AuditTtyStatus> {
        let seq = self.socket().next_seq();
        let pid = self.socket().pid();

        // Build request message
        let mut buf = Vec::with_capacity(32);

        // Netlink header (16 bytes)
        buf.extend_from_slice(&0u32.to_ne_bytes()); // nlmsg_len (fill later)
        buf.extend_from_slice(&AUDIT_TTY_GET.to_ne_bytes()); // nlmsg_type
        buf.extend_from_slice(&(NLM_F_REQUEST | NLM_F_ACK).to_ne_bytes()); // nlmsg_flags
        buf.extend_from_slice(&seq.to_ne_bytes()); // nlmsg_seq
        buf.extend_from_slice(&pid.to_ne_bytes()); // nlmsg_pid

        // Update length
        let len = buf.len() as u32;
        buf[0..4].copy_from_slice(&len.to_ne_bytes());

        // Send request
        self.socket().send(&buf).await?;

        // Receive response - skip ACK
        loop {
            let data = self.socket().recv_msg().await?;

            if data.len() < NLMSG_HDRLEN {
                return Err(Error::InvalidMessage("response too short".into()));
            }

            let nlmsg_type = u16::from_ne_bytes([data[4], data[5]]);

            if nlmsg_type == NLMSG_ERROR {
                if data.len() >= 20 {
                    let errno = i32::from_ne_bytes([data[16], data[17], data[18], data[19]]);
                    if errno != 0 {
                        return Err(Error::from_errno(-errno));
                    }
                }
                // ACK, continue to next message
                continue;
            }

            if nlmsg_type == AUDIT_TTY_GET {
                if data.len() < NLMSG_HDRLEN + std::mem::size_of::<AuditTtyStatus>() {
                    return Err(Error::InvalidMessage(
                        "TTY status response too short".into(),
                    ));
                }

                let (status, _) = AuditTtyStatus::ref_from_prefix(&data[NLMSG_HDRLEN..])
                    .map_err(|_| Error::InvalidMessage("failed to parse TTY status".into()))?;

                return Ok(*status);
            }

            return Err(Error::InvalidMessage(format!(
                "unexpected message type: {}",
                nlmsg_type
            )));
        }
    }

    /// Get audit features.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::{Connection, Audit};
    ///
    /// let conn = Connection::<Audit>::new()?;
    /// let features = conn.get_features().await?;
    ///
    /// println!("Version: {}", features.vers);
    /// println!("Features: 0x{:08x}", features.features);
    /// println!("Mask: 0x{:08x}", features.mask);
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "get_features"))]
    pub async fn get_features(&self) -> Result<AuditFeatures> {
        let seq = self.socket().next_seq();
        let pid = self.socket().pid();

        // Build request message
        let mut buf = Vec::with_capacity(32);

        // Netlink header (16 bytes)
        buf.extend_from_slice(&0u32.to_ne_bytes()); // nlmsg_len (fill later)
        buf.extend_from_slice(&AUDIT_GET_FEATURE.to_ne_bytes()); // nlmsg_type
        buf.extend_from_slice(&(NLM_F_REQUEST | NLM_F_ACK).to_ne_bytes()); // nlmsg_flags
        buf.extend_from_slice(&seq.to_ne_bytes()); // nlmsg_seq
        buf.extend_from_slice(&pid.to_ne_bytes()); // nlmsg_pid

        // Update length
        let len = buf.len() as u32;
        buf[0..4].copy_from_slice(&len.to_ne_bytes());

        // Send request
        self.socket().send(&buf).await?;

        // Receive response - skip ACK
        loop {
            let data = self.socket().recv_msg().await?;

            if data.len() < NLMSG_HDRLEN {
                return Err(Error::InvalidMessage("response too short".into()));
            }

            let nlmsg_type = u16::from_ne_bytes([data[4], data[5]]);

            if nlmsg_type == NLMSG_ERROR {
                if data.len() >= 20 {
                    let errno = i32::from_ne_bytes([data[16], data[17], data[18], data[19]]);
                    if errno != 0 {
                        return Err(Error::from_errno(-errno));
                    }
                }
                // ACK, continue to next message
                continue;
            }

            if nlmsg_type == AUDIT_GET_FEATURE {
                if data.len() < NLMSG_HDRLEN + std::mem::size_of::<AuditFeatures>() {
                    return Err(Error::InvalidMessage("features response too short".into()));
                }

                let (features, _) = AuditFeatures::ref_from_prefix(&data[NLMSG_HDRLEN..])
                    .map_err(|_| Error::InvalidMessage("failed to parse features".into()))?;

                return Ok(*features);
            }

            return Err(Error::InvalidMessage(format!(
                "unexpected message type: {}",
                nlmsg_type
            )));
        }
    }
}

// Silence unused constant warnings - these are kept for documentation/future use
const _: () = {
    let _ = AUDIT_SET;
    let _ = AUDIT_LIST;
    let _ = AUDIT_ADD;
    let _ = AUDIT_DEL;
    let _ = AUDIT_LOGIN;
    let _ = AUDIT_WATCH_INS;
    let _ = AUDIT_WATCH_REM;
    let _ = AUDIT_WATCH_LIST;
    let _ = AUDIT_SIGNAL_INFO;
    let _ = AUDIT_ADD_RULE;
    let _ = AUDIT_DEL_RULE;
    let _ = AUDIT_LIST_RULES;
    let _ = AUDIT_TRIM;
    let _ = AUDIT_MAKE_EQUIV;
    let _ = AUDIT_TTY_SET;
    let _ = AUDIT_SET_FEATURE;
    let _ = AUDIT_IPC_SET_PERM;
    let _ = AUDIT_MQ_OPEN;
    let _ = AUDIT_MQ_SENDRECV;
    let _ = AUDIT_MQ_NOTIFY;
    let _ = AUDIT_MQ_GETSETATTR;
    let _ = AUDIT_STATUS_ENABLED;
    let _ = AUDIT_STATUS_FAILURE;
    let _ = AUDIT_STATUS_PID;
    let _ = AUDIT_STATUS_RATE_LIMIT;
    let _ = AUDIT_STATUS_BACKLOG_LIMIT;
    let _ = AUDIT_STATUS_BACKLOG_WAIT_TIME;
    let _ = AUDIT_STATUS_LOST;
};

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

    #[test]
    fn audit_status_size() {
        // 11 * 4 = 44 bytes
        assert_eq!(std::mem::size_of::<AuditStatus>(), 44);
    }

    #[test]
    fn audit_tty_status_size() {
        assert_eq!(std::mem::size_of::<AuditTtyStatus>(), 8);
    }

    #[test]
    fn audit_features_size() {
        assert_eq!(std::mem::size_of::<AuditFeatures>(), 16);
    }

    #[test]
    fn failure_mode_roundtrip() {
        assert_eq!(AuditFailureMode::Silent.as_u32(), 0);
        assert_eq!(AuditFailureMode::from_u32(0), AuditFailureMode::Silent);

        assert_eq!(AuditFailureMode::Printk.as_u32(), 1);
        assert_eq!(AuditFailureMode::from_u32(1), AuditFailureMode::Printk);

        assert_eq!(AuditFailureMode::Panic.as_u32(), 2);
        assert_eq!(AuditFailureMode::from_u32(2), AuditFailureMode::Panic);
    }

    #[test]
    fn event_type_roundtrip() {
        assert_eq!(AuditEventType::Syscall.as_u16(), 1300);
        assert_eq!(AuditEventType::from_u16(1300), AuditEventType::Syscall);

        assert_eq!(AuditEventType::Avc.as_u16(), 1400);
        assert_eq!(AuditEventType::from_u16(1400), AuditEventType::Avc);
    }

    #[test]
    fn audit_status_helpers() {
        // Test disabled state
        let status = AuditStatus {
            enabled: 0,
            ..Default::default()
        };
        assert!(!status.is_enabled());
        assert!(!status.is_locked());

        // Test enabled state
        let status = AuditStatus {
            enabled: 1,
            ..Default::default()
        };
        assert!(status.is_enabled());
        assert!(!status.is_locked());

        // Test locked state
        let status = AuditStatus {
            enabled: 2,
            ..Default::default()
        };
        assert!(!status.is_enabled());
        assert!(status.is_locked());
    }
}