netlink-socket2 0.3.2-1

Type-safe Rust bindings for Netlink generated from YAML specifications
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
use std::{
    collections::{hash_map::Entry, HashMap},
    io::{self, ErrorKind, IoSlice},
    marker::PhantomData,
    os::fd::{AsRawFd, FromRawFd, OwnedFd},
    sync::Arc,
};

#[allow(unused_imports)]
use super::{Read, Socket, Write};
use crate::{ReplyError, RECV_BUF_SIZE};

use netlink_bindings::{
    builtin::Nlmsghdr,
    nlctrl,
    traits::{NetlinkRequest, Protocol},
    utils,
};

pub struct NetlinkSocket {
    pub(crate) buf: Arc<[u8; RECV_BUF_SIZE]>,
    pub(crate) cache: HashMap<&'static [u8], u16>,
    pub(crate) sock: HashMap<u16, Socket>,
    pub(crate) seq: u32,
}

impl Default for NetlinkSocket {
    fn default() -> Self {
        Self::new()
    }
}

impl NetlinkSocket {
    pub fn new() -> Self {
        Self {
            buf: Arc::new([0u8; RECV_BUF_SIZE]),
            cache: HashMap::default(),
            sock: HashMap::new(),
            seq: 1,
        }
    }

    pub(crate) fn get_socket_cached(
        cache: &mut HashMap<u16, Socket>,
        protonum: u16,
    ) -> io::Result<&mut Socket> {
        match cache.entry(protonum) {
            Entry::Occupied(sock) => Ok(sock.into_mut()),
            Entry::Vacant(ent) => {
                let sock = Self::get_socket_new(protonum)?;
                Ok(ent.insert(sock))
            }
        }
    }

    pub(crate) fn get_socket_new(family: u16) -> io::Result<Socket> {
        let fd = unsafe {
            libc::socket(
                libc::AF_NETLINK,
                libc::SOCK_RAW | libc::SOCK_CLOEXEC,
                family as i32,
            )
        };
        if fd < 0 {
            return Err(io::Error::from_raw_os_error(-fd));
        }
        let fd = unsafe { OwnedFd::from_raw_fd(fd) };

        // Enable extended attributes in libc::NLMSG_ERROR and libc::NLMSG_DONE
        let res = unsafe {
            libc::setsockopt(
                fd.as_raw_fd(),
                libc::SOL_NETLINK,
                libc::NETLINK_EXT_ACK,
                &1u32 as *const u32 as *const libc::c_void,
                4,
            )
        };
        if res < 0 {
            return Err(io::Error::from_raw_os_error(-res));
        }

        Self::convert_sock(fd.into())
    }

    #[super::only_async]
    fn convert_sock(sock: std::net::TcpStream) -> io::Result<Socket> {
        sock.set_nonblocking(true)?;
        Socket::try_from(sock)
    }

    #[super::only_sync]
    fn convert_sock(sock: std::net::TcpStream) -> io::Result<Socket> {
        Ok(sock)
    }

    /// Reserve a sequential chunk of `seq` values, so chained messages don't
    /// get confused. A random `seq` number might be used just as well.
    pub fn reserve_seq(&mut self, len: u32) -> u32 {
        let seq = self.seq;
        self.seq = self.seq.wrapping_add(len);
        seq
    }

    #[super::strip_async]
    pub async fn request<'sock, Request>(
        &'sock mut self,
        request: &Request,
    ) -> io::Result<NetlinkReply<'sock, Request>>
    where
        Request: NetlinkRequest,
    {
        let (protonum, request_type) = match request.protocol() {
            Protocol::Raw {
                protonum,
                request_type,
            } => (protonum, request_type),
            Protocol::Generic(name) => (libc::GENL_ID_CTRL as u16, self.resolve(name).await?),
        };

        self.request_raw(request, protonum, request_type).await
    }

    #[super::strip_async]
    async fn resolve(&mut self, family_name: &'static [u8]) -> io::Result<u16> {
        if let Some(id) = self.cache.get(family_name) {
            return Ok(*id);
        }

        let mut request = nlctrl::Request::new().op_getfamily_do();
        request.encode().push_family_name_bytes(family_name);

        let Protocol::Raw {
            protonum,
            request_type,
        } = request.protocol()
        else {
            unreachable!()
        };
        assert_eq!(protonum, libc::NETLINK_GENERIC as u16);
        assert_eq!(request_type, libc::GENL_ID_CTRL as u16);

        let mut iter = self.request_raw(&request, protonum, request_type).await?;
        if let Some(reply) = iter.recv().await {
            let Ok(id) = reply?.get_family_id() else {
                return Err(ErrorKind::Unsupported.into());
            };
            self.cache.insert(family_name, id);
            return Ok(id);
        }

        Err(ErrorKind::UnexpectedEof.into())
    }

    #[super::strip_async]
    async fn request_raw<'sock, Request>(
        &'sock mut self,
        request: &Request,
        protonum: u16,
        request_type: u16,
    ) -> io::Result<NetlinkReply<'sock, Request>>
    where
        Request: NetlinkRequest,
    {
        let seq = self.reserve_seq(1);
        let sock = Self::get_socket_cached(&mut self.sock, protonum)?;

        let header = Nlmsghdr {
            len: Nlmsghdr::len() as u32 + request.payload().len() as u32,
            r#type: request_type,
            flags: request.flags() | libc::NLM_F_REQUEST as u16 | libc::NLM_F_ACK as u16,
            seq,
            pid: 0,
        };

        Self::write_buf(
            sock,
            &[
                IoSlice::new(header.as_slice()),
                IoSlice::new(request.payload()),
            ],
        )
        .await?;

        Ok(NetlinkReply {
            sock,
            buf: &mut self.buf,
            inner: NetlinkReplyInner {
                buf_offset: 0,
                buf_read: 0,
            },
            seq: header.seq,
            done: false,
            phantom: PhantomData,
        })
    }

    #[super::not_tokio]
    #[super::strip_async]
    pub(crate) async fn write_buf(sock: &mut Socket, payload: &[IoSlice<'_>]) -> io::Result<()> {
        loop {
            match sock.write_vectored(payload).await {
                Ok(sent) if sent != payload.iter().map(|s| s.len()).sum() => {
                    return Err(io::Error::other("Couldn't send the whole message"));
                }
                Ok(_) => return Ok(()),
                Err(err) if err.kind() == ErrorKind::Interrupted => continue,
                Err(err) => return Err(err),
            }
        }
    }

    #[super::only_tokio]
    #[super::strip_async]
    pub(crate) async fn write_buf(sock: &mut Socket, payload: &[IoSlice<'_>]) -> io::Result<()> {
        loop {
            // Some subsystems don't correctly implement io notifications, which tokio runtime
            // expects to receive before doing any actual io, hence we instead always attempt an io
            // operation first.
            let res = sock.try_write_vectored(payload);
            if matches!(&res, Err(err) if err.kind() == ErrorKind::WouldBlock) {
                sock.writable().await?;
                continue;
            }

            match res {
                Ok(sent) if sent != payload.iter().map(|s| s.len()).sum() => {
                    return Err(io::Error::other("Couldn't send the whole message"));
                }
                Ok(_) => return Ok(()),
                Err(err) if err.kind() == ErrorKind::Interrupted => continue,
                Err(err) => return Err(err),
            }
        }
    }
}

pub(crate) struct NetlinkReplyInner {
    pub(crate) buf_offset: usize,
    pub(crate) buf_read: usize,
}

impl NetlinkReplyInner {
    #[super::not_tokio]
    #[super::strip_async]
    async fn read_buf(sock: &mut Socket, buf: &mut [u8]) -> io::Result<usize> {
        loop {
            match sock.read(&mut buf[..]).await {
                Ok(read) => return Ok(read),
                Err(err) if err.kind() == ErrorKind::Interrupted => continue,
                Err(err) => return Err(err),
            }
        }
    }

    #[super::only_tokio]
    #[super::strip_async]
    async fn read_buf(sock: &mut Socket, buf: &mut [u8]) -> io::Result<usize> {
        loop {
            // Some subsystems don't correctly implement io notifications, which tokio
            // runtime expects to receive before doing any actual io, hence we instead
            // always attempt an io operation first.
            let res = sock.try_read(&mut buf[..]);
            if matches!(&res, Err(err) if err.kind() == ErrorKind::WouldBlock) {
                sock.readable().await?;
                continue;
            }

            match res {
                Ok(read) => return Ok(read),
                Err(err) if err.kind() == ErrorKind::Interrupted => continue,
                Err(err) => return Err(err),
            }
        }
    }

    #[allow(clippy::type_complexity)]
    #[super::strip_async]
    pub async fn recv(
        &mut self,
        sock: &mut Socket,
        buf: &mut [u8; RECV_BUF_SIZE],
    ) -> io::Result<(u32, u16, Result<(usize, usize), ReplyError>)> {
        if self.buf_offset == self.buf_read {
            self.buf_read = Self::read_buf(sock, &mut buf[..]).await?;
            self.buf_offset = 0;
        }
        self.parse_next(buf).await
    }

    #[allow(clippy::type_complexity)]
    #[super::strip_async]
    pub(crate) async fn parse_next(
        &mut self,
        buf: &[u8; RECV_BUF_SIZE],
    ) -> io::Result<(u32, u16, Result<(usize, usize), ReplyError>)> {
        let packet = &buf[self.buf_offset..self.buf_read];

        let too_short_err = || io::Error::other("Received packet is too short");

        let Some(header) = packet.get(..Nlmsghdr::len()) else {
            return Err(too_short_err());
        };
        let header = Nlmsghdr::from_slice(header);

        let payload_start = self.buf_offset + Nlmsghdr::len();
        self.buf_offset += header.len as usize;

        match header.r#type as i32 {
            libc::NLMSG_DONE | libc::NLMSG_ERROR => {
                let Some(code) = packet.get(16..20) else {
                    return Err(too_short_err());
                };
                let code = utils::parse_i32(code).unwrap();

                let (echo_start, echo_end) =
                    if code == 0 || header.r#type == libc::NLMSG_DONE as u16 {
                        (20, 20)
                    } else {
                        let Some(echo_header) = packet.get(20..(20 + Nlmsghdr::len())) else {
                            return Err(too_short_err());
                        };
                        let echo_header = Nlmsghdr::from_slice(echo_header);

                        if echo_header.flags & libc::NLM_F_CAPPED as u16 == 0 {
                            let start = echo_header.len;
                            if packet.len() < start as usize + 20 {
                                return Err(too_short_err());
                            }

                            (20 + 16, 20 + start as usize)
                        } else {
                            let ext_ack_start = 20 + Nlmsghdr::len();
                            (ext_ack_start, ext_ack_start)
                        }
                    };

                let ext_ack_start =
                    utils::align_up(echo_end, utils::NLA_ALIGNTO).min(self.buf_offset);

                Ok((
                    header.seq,
                    header.r#type,
                    Err(ReplyError {
                        code: io::Error::from_raw_os_error(-code),
                        request_bounds: (echo_start as u32, echo_end as u32),
                        ext_ack_bounds: (ext_ack_start as u32, self.buf_offset as u32),
                        reply_buf: None,
                        chained_name: None,
                        lookup: None,
                    }),
                ))
            }
            libc::NLMSG_NOOP => Ok((
                header.seq,
                header.r#type,
                Err(io::Error::other("Received NLMSG_NOOP").into()),
            )),
            libc::NLMSG_OVERRUN => Ok((
                header.seq,
                header.r#type,
                Err(io::Error::other("Received NLMSG_OVERRUN").into()),
            )),
            _ => Ok((
                header.seq,
                header.r#type,
                Ok((payload_start, self.buf_offset)),
            )),
        }
    }
}

pub struct NetlinkReply<'sock, Request: NetlinkRequest> {
    inner: NetlinkReplyInner,
    sock: &'sock mut Socket,
    buf: &'sock mut Arc<[u8; RECV_BUF_SIZE]>,
    seq: u32,
    done: bool,
    phantom: PhantomData<Request>,
}

impl<Request: NetlinkRequest> NetlinkReply<'_, Request> {
    #[super::strip_async]
    pub async fn recv_one(&mut self) -> Result<Request::ReplyType<'_>, ReplyError> {
        if let Some(res) = self.recv().await {
            return res;
        }
        Err(io::Error::other("Reply didn't contain data").into())
    }

    #[super::strip_async]
    pub async fn recv_ack(&mut self) -> Result<(), ReplyError> {
        if let Some(res) = self.recv().await {
            res?;
            return Err(io::Error::other("Reply isn't just an ack").into());
        }
        Ok(())
    }

    #[super::strip_async]
    pub async fn recv(&mut self) -> Option<Result<Request::ReplyType<'_>, ReplyError>> {
        if self.done {
            return None;
        }

        let buf = Arc::make_mut(self.buf);

        loop {
            match self.inner.recv(self.sock, buf).await {
                Err(io_err) => {
                    self.done = true;
                    return Some(Err(io_err.into()));
                }
                Ok((seq, _type, res)) => {
                    if seq != self.seq {
                        continue;
                    }
                    return match res {
                        Ok((l, r)) => Some(Ok(Request::decode_reply(&self.buf[l..r]))),
                        Err(mut err) => {
                            self.done = true;
                            if err.code.raw_os_error().unwrap() == 0 {
                                None
                            } else {
                                if err.has_context() {
                                    err.lookup = Some(Request::lookup);
                                    err.reply_buf = Some(self.buf.clone());
                                }
                                Some(Err(err))
                            }
                        }
                    };
                }
            };
        }
    }
}

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

    #[allow(unused)]
    trait SpawnCompatible: Send {}
    impl<'a> SpawnCompatible for NetlinkSocket {}
}