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
//! FIB lookup implementation for `Connection<FibLookup>`.
//!
//! This module provides methods for performing FIB (Forwarding Information Base)
//! route lookups via the NETLINK_FIB_LOOKUP protocol.
//!
//! # Example
//!
//! ```ignore
//! use nlink::netlink::{Connection, FibLookup};
//! use std::net::Ipv4Addr;
//!
//! let conn = Connection::<FibLookup>::new()?;
//!
//! // Look up a route for an IP address
//! let result = conn.lookup(Ipv4Addr::new(8, 8, 8, 8)).await?;
//! println!("Route type: {:?}, table: {}, prefix_len: {}",
//!     result.route_type, result.table_id, result.prefix_len);
//! ```

use std::net::Ipv4Addr;

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

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

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

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

/// FIB result message structure.
///
/// This structure is sent to and received from the kernel for FIB lookups.
/// The kernel fills in the result fields after processing the lookup request.
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, FromBytes, IntoBytes, Immutable, KnownLayout)]
pub struct FibResultNl {
    /// Address to look up (network byte order).
    pub fl_addr: u32,
    /// Firewall mark.
    pub fl_fwmark: u32,
    /// Type of Service.
    pub fl_tos: u8,
    /// Scope for the lookup.
    pub fl_scope: u8,
    /// Input table ID.
    pub tb_id_in: u8,
    /// Result: table ID where route was found.
    pub tb_id: u8,
    /// Result: prefix length of the matched route.
    pub prefixlen: u8,
    /// Result: next hop selector.
    pub nh_sel: u8,
    /// Result: route type.
    pub route_type: u8,
    /// Result: scope.
    pub scope: u8,
    /// Result: error code.
    pub err: i32,
}

impl FibResultNl {
    /// Create a new FIB lookup request for an IPv4 address.
    pub fn for_addr(addr: Ipv4Addr) -> Self {
        Self {
            fl_addr: u32::from_be_bytes(addr.octets()),
            ..Default::default()
        }
    }

    /// Create a new FIB lookup request with a specific table.
    pub fn for_addr_in_table(addr: Ipv4Addr, table: u8) -> Self {
        Self {
            fl_addr: u32::from_be_bytes(addr.octets()),
            tb_id_in: table,
            ..Default::default()
        }
    }

    /// Create a new FIB lookup request with a firewall mark.
    pub fn for_addr_with_mark(addr: Ipv4Addr, mark: u32) -> Self {
        Self {
            fl_addr: u32::from_be_bytes(addr.octets()),
            fl_fwmark: mark,
            ..Default::default()
        }
    }

    /// Get the looked up address as an IPv4 address.
    pub fn addr(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.fl_addr.to_be_bytes())
    }
}

/// Route type from FIB lookup.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RouteType {
    /// Unknown or unspecified.
    Unspec,
    /// Local address.
    Local,
    /// Unicast route.
    Unicast,
    /// Broadcast route.
    Broadcast,
    /// Anycast route.
    Anycast,
    /// Multicast route.
    Multicast,
    /// Blackhole route (silently drop).
    Blackhole,
    /// Unreachable (generate ICMP unreachable).
    Unreachable,
    /// Prohibit (generate ICMP prohibited).
    Prohibit,
    /// Throw (continue lookup in another table).
    Throw,
    /// NAT route.
    Nat,
    /// External resolver.
    XResolve,
    /// Unknown type.
    Unknown(u8),
}

impl RouteType {
    fn from_u8(val: u8) -> Self {
        match val {
            0 => Self::Unspec,
            1 => Self::Unicast,
            2 => Self::Local,
            3 => Self::Broadcast,
            4 => Self::Anycast,
            5 => Self::Multicast,
            6 => Self::Blackhole,
            7 => Self::Unreachable,
            8 => Self::Prohibit,
            9 => Self::Throw,
            10 => Self::Nat,
            11 => Self::XResolve,
            other => Self::Unknown(other),
        }
    }

    /// Get the route type number.
    pub fn number(&self) -> u8 {
        match self {
            Self::Unspec => 0,
            Self::Unicast => 1,
            Self::Local => 2,
            Self::Broadcast => 3,
            Self::Anycast => 4,
            Self::Multicast => 5,
            Self::Blackhole => 6,
            Self::Unreachable => 7,
            Self::Prohibit => 8,
            Self::Throw => 9,
            Self::Nat => 10,
            Self::XResolve => 11,
            Self::Unknown(n) => *n,
        }
    }
}

/// Route scope from FIB lookup.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RouteScope {
    /// Universe (global) scope.
    Universe,
    /// Site scope.
    Site,
    /// Link scope.
    Link,
    /// Host scope.
    Host,
    /// Nowhere scope.
    Nowhere,
    /// Unknown scope.
    Unknown(u8),
}

impl RouteScope {
    fn from_u8(val: u8) -> Self {
        match val {
            0 => Self::Universe,
            200 => Self::Site,
            253 => Self::Link,
            254 => Self::Host,
            255 => Self::Nowhere,
            other => Self::Unknown(other),
        }
    }

    /// Get the scope number.
    pub fn number(&self) -> u8 {
        match self {
            Self::Universe => 0,
            Self::Site => 200,
            Self::Link => 253,
            Self::Host => 254,
            Self::Nowhere => 255,
            Self::Unknown(n) => *n,
        }
    }
}

/// Result of a FIB lookup.
#[derive(Debug, Clone)]
pub struct FibLookupResult {
    /// The address that was looked up.
    pub addr: Ipv4Addr,
    /// The routing table where the route was found.
    pub table_id: u8,
    /// The prefix length of the matched route.
    pub prefix_len: u8,
    /// The route type.
    pub route_type: RouteType,
    /// The route scope.
    pub scope: RouteScope,
    /// The next hop selector.
    pub nh_sel: u8,
    /// Error code (0 = success).
    pub error: i32,
}

impl FibLookupResult {
    /// Returns true if the lookup was successful.
    pub fn is_success(&self) -> bool {
        self.error == 0
    }

    /// Returns true if the route is a local address.
    pub fn is_local(&self) -> bool {
        self.route_type == RouteType::Local
    }

    /// Returns true if the route is a unicast route.
    pub fn is_unicast(&self) -> bool {
        self.route_type == RouteType::Unicast
    }

    /// Returns true if the route is a blackhole.
    pub fn is_blackhole(&self) -> bool {
        self.route_type == RouteType::Blackhole
    }

    /// Returns true if the destination is unreachable.
    pub fn is_unreachable(&self) -> bool {
        self.route_type == RouteType::Unreachable
    }
}

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

    /// Look up a route for an IPv4 address.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::{Connection, FibLookup};
    /// use std::net::Ipv4Addr;
    ///
    /// let conn = Connection::<FibLookup>::new()?;
    /// let result = conn.lookup(Ipv4Addr::new(8, 8, 8, 8)).await?;
    ///
    /// if result.is_success() {
    ///     println!("Route found: type={:?}, table={}, prefix=/{}",
    ///         result.route_type, result.table_id, result.prefix_len);
    /// }
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "lookup"))]
    pub async fn lookup(&self, addr: Ipv4Addr) -> Result<FibLookupResult> {
        self.lookup_with_options(FibResultNl::for_addr(addr)).await
    }

    /// Look up a route for an IPv4 address in a specific routing table.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::{Connection, FibLookup};
    /// use std::net::Ipv4Addr;
    ///
    /// let conn = Connection::<FibLookup>::new()?;
    /// // Look up in table 254 (main)
    /// let result = conn.lookup_in_table(Ipv4Addr::new(10, 0, 0, 1), 254).await?;
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "lookup_in_table"))]
    pub async fn lookup_in_table(&self, addr: Ipv4Addr, table: u8) -> Result<FibLookupResult> {
        self.lookup_with_options(FibResultNl::for_addr_in_table(addr, table))
            .await
    }

    /// Look up a route for an IPv4 address with a specific firewall mark.
    ///
    /// Firewall marks can be used for policy routing decisions.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::{Connection, FibLookup};
    /// use std::net::Ipv4Addr;
    ///
    /// let conn = Connection::<FibLookup>::new()?;
    /// let result = conn.lookup_with_mark(Ipv4Addr::new(8, 8, 8, 8), 0x100).await?;
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "lookup_with_mark"))]
    pub async fn lookup_with_mark(&self, addr: Ipv4Addr, mark: u32) -> Result<FibLookupResult> {
        self.lookup_with_options(FibResultNl::for_addr_with_mark(addr, mark))
            .await
    }

    /// Look up a route with custom options.
    ///
    /// This method allows full control over the lookup parameters.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use nlink::netlink::{Connection, FibLookup};
    /// use nlink::netlink::fib_lookup::FibResultNl;
    /// use std::net::Ipv4Addr;
    ///
    /// let conn = Connection::<FibLookup>::new()?;
    /// let request = FibResultNl {
    ///     fl_addr: u32::from_be_bytes(Ipv4Addr::new(8, 8, 8, 8).octets()),
    ///     fl_tos: 0x10,  // Specific TOS value
    ///     ..Default::default()
    /// };
    /// let result = conn.lookup_with_options(request).await?;
    /// ```
    #[tracing::instrument(level = "debug", skip_all, fields(method = "lookup_with_options"))]
    pub async fn lookup_with_options(&self, request: FibResultNl) -> Result<FibLookupResult> {
        let seq = self.socket().next_seq();
        let pid = self.socket().pid();

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

        // Netlink header (16 bytes)
        buf.extend_from_slice(&0u32.to_ne_bytes()); // nlmsg_len (fill later)
        buf.extend_from_slice(&0u16.to_ne_bytes()); // nlmsg_type (0 for FIB lookup)
        buf.extend_from_slice(&NLM_F_REQUEST.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

        // FIB result structure
        buf.extend_from_slice(request.as_bytes());

        // 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 && 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));
            }
        }

        // Parse the response
        if data.len() < NLMSG_HDRLEN + std::mem::size_of::<FibResultNl>() {
            return Err(Error::InvalidMessage(
                "response too short for FIB result".into(),
            ));
        }

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

        Ok(FibLookupResult {
            addr: result.addr(),
            table_id: result.tb_id,
            prefix_len: result.prefixlen,
            route_type: RouteType::from_u8(result.route_type),
            scope: RouteScope::from_u8(result.scope),
            nh_sel: result.nh_sel,
            error: result.err,
        })
    }
}

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

    #[test]
    fn fib_result_nl_size() {
        // The structure is 20 bytes (4 + 4 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 4)
        // with padding for i32 alignment
        assert_eq!(std::mem::size_of::<FibResultNl>(), 20);
    }

    #[test]
    fn fib_result_for_addr() {
        let addr = Ipv4Addr::new(192, 168, 1, 1);
        let result = FibResultNl::for_addr(addr);
        assert_eq!(result.addr(), addr);
        assert_eq!(result.fl_fwmark, 0);
        assert_eq!(result.tb_id_in, 0);
    }

    #[test]
    fn fib_result_for_addr_in_table() {
        let addr = Ipv4Addr::new(10, 0, 0, 1);
        let result = FibResultNl::for_addr_in_table(addr, 254);
        assert_eq!(result.addr(), addr);
        assert_eq!(result.tb_id_in, 254);
    }

    #[test]
    fn route_type_roundtrip() {
        assert_eq!(RouteType::Unicast.number(), 1);
        assert_eq!(RouteType::from_u8(1), RouteType::Unicast);

        assert_eq!(RouteType::Local.number(), 2);
        assert_eq!(RouteType::from_u8(2), RouteType::Local);

        assert_eq!(RouteType::Blackhole.number(), 6);
        assert_eq!(RouteType::from_u8(6), RouteType::Blackhole);
    }

    #[test]
    fn route_scope_roundtrip() {
        assert_eq!(RouteScope::Universe.number(), 0);
        assert_eq!(RouteScope::from_u8(0), RouteScope::Universe);

        assert_eq!(RouteScope::Link.number(), 253);
        assert_eq!(RouteScope::from_u8(253), RouteScope::Link);

        assert_eq!(RouteScope::Host.number(), 254);
        assert_eq!(RouteScope::from_u8(254), RouteScope::Host);
    }

    #[test]
    fn fib_lookup_result_helpers() {
        let result = FibLookupResult {
            addr: Ipv4Addr::new(8, 8, 8, 8),
            table_id: 254,
            prefix_len: 0,
            route_type: RouteType::Unicast,
            scope: RouteScope::Universe,
            nh_sel: 0,
            error: 0,
        };

        assert!(result.is_success());
        assert!(result.is_unicast());
        assert!(!result.is_local());
        assert!(!result.is_blackhole());
    }
}