nlink 0.13.0

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
//! Socket information structures.
//!
//! This module provides strongly-typed representations of socket information
//! returned by the kernel's SOCK_DIAG interface.

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

use serde::{Deserialize, Serialize};

use super::types::{AddressFamily, MemInfo, Protocol, SocketState, TcpInfo, TcpState, Timer};

/// Common socket information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SocketInfo {
    /// TCP/UDP/SCTP socket over IPv4/IPv6.
    Inet(Box<InetSocket>),
    /// Unix domain socket.
    Unix(UnixSocket),
    /// Netlink socket.
    Netlink(NetlinkSocket),
    /// Packet (raw) socket.
    Packet(PacketSocket),
}

impl SocketInfo {
    /// Get the socket state.
    pub fn state(&self) -> SocketState {
        match self {
            SocketInfo::Inet(s) => s.state,
            SocketInfo::Unix(s) => s.state,
            SocketInfo::Netlink(_) => SocketState::Close,
            SocketInfo::Packet(_) => SocketState::Close,
        }
    }

    /// Get the inode number.
    pub fn inode(&self) -> u32 {
        match self {
            SocketInfo::Inet(s) => s.inode,
            SocketInfo::Unix(s) => s.inode,
            SocketInfo::Netlink(s) => s.inode,
            SocketInfo::Packet(s) => s.inode,
        }
    }

    /// Get the socket UID.
    pub fn uid(&self) -> Option<u32> {
        match self {
            SocketInfo::Inet(s) => Some(s.uid),
            SocketInfo::Unix(s) => s.uid,
            SocketInfo::Netlink(s) => Some(s.portid),
            SocketInfo::Packet(s) => Some(s.uid),
        }
    }

    /// Get the Inet socket if this is an Inet variant.
    pub fn as_inet(&self) -> Option<&InetSocket> {
        match self {
            SocketInfo::Inet(s) => Some(s),
            _ => None,
        }
    }
}

/// Internet (TCP/UDP/SCTP) socket information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InetSocket {
    /// Address family (IPv4 or IPv6).
    pub family: AddressFamily,
    /// Protocol (TCP, UDP, etc.).
    pub protocol: Protocol,
    /// Socket state.
    pub state: SocketState,
    /// Local address and port.
    pub local: SocketAddr,
    /// Remote address and port.
    pub remote: SocketAddr,
    /// Interface index (0 = any).
    pub interface: u32,
    /// Socket cookie (unique identifier).
    pub cookie: u64,
    /// Timer information.
    pub timer: Timer,
    /// Receive queue size.
    pub recv_q: u32,
    /// Send queue size.
    pub send_q: u32,
    /// Socket owner UID.
    pub uid: u32,
    /// Inode number.
    pub inode: u32,
    /// Reference count.
    pub refcnt: u32,
    /// Socket mark.
    pub mark: Option<u32>,
    /// Cgroup ID.
    pub cgroup_id: Option<u64>,
    /// TCP-specific information.
    pub tcp_info: Option<TcpInfo>,
    /// Memory information.
    pub mem_info: Option<MemInfo>,
    /// Congestion control algorithm.
    pub congestion: Option<String>,
    /// Type of service.
    pub tos: Option<u8>,
    /// Traffic class (IPv6).
    pub tclass: Option<u8>,
    /// Shutdown state (read/write).
    pub shutdown: Option<u8>,
    /// IPv6 only flag.
    pub v6only: Option<bool>,
}

impl InetSocket {
    /// Create a new InetSocket with minimal information.
    pub fn new(
        family: AddressFamily,
        protocol: Protocol,
        state: TcpState,
        local: SocketAddr,
        remote: SocketAddr,
    ) -> Self {
        Self {
            family,
            protocol,
            state: SocketState::Tcp(state),
            local,
            remote,
            interface: 0,
            cookie: 0,
            timer: Timer::Off,
            recv_q: 0,
            send_q: 0,
            uid: 0,
            inode: 0,
            refcnt: 0,
            mark: None,
            cgroup_id: None,
            tcp_info: None,
            mem_info: None,
            congestion: None,
            tos: None,
            tclass: None,
            shutdown: None,
            v6only: None,
        }
    }

    /// Check if this is a listening socket.
    pub fn is_listening(&self) -> bool {
        matches!(self.state, SocketState::Tcp(TcpState::Listen))
    }

    /// Check if this is a connected socket.
    pub fn is_connected(&self) -> bool {
        matches!(self.state, SocketState::Tcp(TcpState::Established))
    }

    /// Get the netid string for output.
    pub fn netid(&self) -> &'static str {
        match (self.protocol, self.family) {
            (Protocol::Tcp, AddressFamily::Inet) => "tcp",
            (Protocol::Tcp, AddressFamily::Inet6) => "tcp6",
            (Protocol::Udp, AddressFamily::Inet) => "udp",
            (Protocol::Udp, AddressFamily::Inet6) => "udp6",
            (Protocol::Sctp, AddressFamily::Inet) => "sctp",
            (Protocol::Sctp, AddressFamily::Inet6) => "sctp6",
            (Protocol::Dccp, AddressFamily::Inet) => "dccp",
            (Protocol::Dccp, AddressFamily::Inet6) => "dccp6",
            (Protocol::Mptcp, AddressFamily::Inet) => "mptcp",
            (Protocol::Mptcp, AddressFamily::Inet6) => "mptcp6",
            (Protocol::Raw, AddressFamily::Inet) => "raw",
            (Protocol::Raw, AddressFamily::Inet6) => "raw6",
            _ => "unknown",
        }
    }
}

impl Default for InetSocket {
    fn default() -> Self {
        Self::new(
            AddressFamily::Inet,
            Protocol::Tcp,
            TcpState::Unknown,
            SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0),
            SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0),
        )
    }
}

/// Unix socket type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum UnixType {
    /// Stream socket (SOCK_STREAM).
    Stream = libc::SOCK_STREAM as u8,
    /// Datagram socket (SOCK_DGRAM).
    Dgram = libc::SOCK_DGRAM as u8,
    /// Seqpacket socket (SOCK_SEQPACKET).
    Seqpacket = libc::SOCK_SEQPACKET as u8,
}

impl UnixType {
    /// Parse from raw value.
    pub fn from_u8(value: u8) -> Option<Self> {
        match value as i32 {
            libc::SOCK_STREAM => Some(Self::Stream),
            libc::SOCK_DGRAM => Some(Self::Dgram),
            libc::SOCK_SEQPACKET => Some(Self::Seqpacket),
            _ => None,
        }
    }

    /// Get the netid string.
    pub fn netid(&self) -> &'static str {
        match self {
            Self::Stream => "u_str",
            Self::Dgram => "u_dgr",
            Self::Seqpacket => "u_seq",
        }
    }
}

/// Unix domain socket information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnixSocket {
    /// Socket type.
    pub socket_type: UnixType,
    /// Socket state.
    pub state: SocketState,
    /// Socket path (None for abstract or unnamed).
    pub path: Option<String>,
    /// Abstract name (starts with @).
    pub abstract_name: Option<String>,
    /// Inode number.
    pub inode: u32,
    /// Socket cookie.
    pub cookie: u64,
    /// Peer inode (for connected sockets).
    pub peer_inode: Option<u32>,
    /// VFS device.
    pub vfs_dev: Option<u32>,
    /// VFS inode.
    pub vfs_inode: Option<u32>,
    /// Receive queue size.
    pub recv_q: Option<u32>,
    /// Send queue size.
    pub send_q: Option<u32>,
    /// Pending connections (for listening sockets).
    pub pending_connections: Option<Vec<u32>>,
    /// Socket owner UID.
    pub uid: Option<u32>,
    /// Memory information.
    pub mem_info: Option<MemInfo>,
    /// Shutdown state.
    pub shutdown: Option<u8>,
}

impl UnixSocket {
    /// Create a new UnixSocket with minimal information.
    pub fn new(socket_type: UnixType, state: SocketState, inode: u32) -> Self {
        Self {
            socket_type,
            state,
            path: None,
            abstract_name: None,
            inode,
            cookie: 0,
            peer_inode: None,
            vfs_dev: None,
            vfs_inode: None,
            recv_q: None,
            send_q: None,
            pending_connections: None,
            uid: None,
            mem_info: None,
            shutdown: None,
        }
    }

    /// Get the socket name for display.
    pub fn name(&self) -> String {
        if let Some(ref path) = self.path {
            path.clone()
        } else if let Some(ref name) = self.abstract_name {
            format!("@{}", name)
        } else {
            String::new()
        }
    }

    /// Get the netid string.
    pub fn netid(&self) -> &'static str {
        self.socket_type.netid()
    }
}

/// Netlink socket information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetlinkSocket {
    /// Netlink protocol (NETLINK_ROUTE, etc.).
    pub protocol: u8,
    /// Port ID.
    pub portid: u32,
    /// Destination port ID.
    pub dst_portid: u32,
    /// Destination group.
    pub dst_group: u32,
    /// Subscribed groups bitmask.
    pub groups: u32,
    /// Inode number.
    pub inode: u32,
    /// Socket cookie.
    pub cookie: u64,
    /// Receive queue size.
    pub recv_q: Option<u32>,
    /// Send queue size.
    pub send_q: Option<u32>,
    /// Memory information.
    pub mem_info: Option<MemInfo>,
}

impl NetlinkSocket {
    /// Get the protocol name.
    pub fn protocol_name(&self) -> &'static str {
        match self.protocol {
            0 => "route",
            1 => "unused",
            2 => "usersock",
            3 => "firewall",
            4 => "sock_diag",
            5 => "nflog",
            6 => "xfrm",
            7 => "selinux",
            8 => "iscsi",
            9 => "audit",
            10 => "fib_lookup",
            11 => "connector",
            12 => "netfilter",
            13 => "ip6_fw",
            14 => "dnrtmsg",
            15 => "kobject_uevent",
            16 => "generic",
            18 => "scsitransport",
            19 => "ecryptfs",
            20 => "rdma",
            21 => "crypto",
            22 => "smc",
            _ => "unknown",
        }
    }
}

/// Packet (raw) socket information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PacketSocket {
    /// Socket type (SOCK_RAW or SOCK_DGRAM).
    pub socket_type: u8,
    /// Protocol (ETH_P_*).
    pub protocol: u16,
    /// Interface index.
    pub interface: u32,
    /// Inode number.
    pub inode: u32,
    /// Socket cookie.
    pub cookie: u64,
    /// Socket owner UID.
    pub uid: u32,
    /// Receive queue size.
    pub recv_q: Option<u32>,
    /// Send queue size.
    pub send_q: Option<u32>,
    /// Fanout ID (if in fanout group).
    pub fanout: Option<u32>,
    /// Memory information.
    pub mem_info: Option<MemInfo>,
}

impl PacketSocket {
    /// Get the netid string.
    pub fn netid(&self) -> &'static str {
        match self.socket_type as i32 {
            libc::SOCK_RAW => "p_raw",
            libc::SOCK_DGRAM => "p_dgr",
            _ => "packet",
        }
    }

    /// Get the protocol name.
    pub fn protocol_name(&self) -> &'static str {
        match self.protocol {
            0x0003 => "802.3", // ETH_P_802_3
            0x0004 => "ax25",
            0x0800 => "ip",
            0x0806 => "arp",
            0x8035 => "rarp",
            0x86DD => "ipv6",
            0x8863 => "pppoe_disc",
            0x8864 => "pppoe_sess",
            0x888E => "802.1x",
            0x88A8 => "802.1ad",
            0x88CC => "lldp",
            _ => "unknown",
        }
    }
}

/// Parse an IPv4 address from 4 bytes (network byte order).
pub fn parse_ipv4(data: &[u8]) -> Ipv4Addr {
    if data.len() >= 4 {
        Ipv4Addr::new(data[0], data[1], data[2], data[3])
    } else {
        Ipv4Addr::UNSPECIFIED
    }
}

/// Parse an IPv6 address from 16 bytes.
pub fn parse_ipv6(data: &[u8]) -> Ipv6Addr {
    if data.len() >= 16 {
        let mut octets = [0u8; 16];
        octets.copy_from_slice(&data[..16]);
        Ipv6Addr::from(octets)
    } else {
        Ipv6Addr::UNSPECIFIED
    }
}

/// Parse a port from 2 bytes (network byte order).
pub fn parse_port(data: &[u8]) -> u16 {
    if data.len() >= 2 {
        u16::from_be_bytes([data[0], data[1]])
    } else {
        0
    }
}