ftth-rtnl 0.2.1

RTnetlink components for `ftth` suite of Rust FTTH CPE software.
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
#![allow(unreachable_patterns)]

use ftth_common::channel::{AsyncWorldClient, AsyncWorldServer};

use futures::TryStreamExt;

use std::fmt::{Debug, Display};
use std::io::{self, ErrorKind};

use netlink_packet_route::link::LinkFlags;
use rtnetlink::{LinkMessageBuilder, LinkUnspec};

pub(crate) type Client = AsyncWorldClient<RtnlLinkRequest, RtnlLinkResponse>;
pub(crate) type Server = AsyncWorldServer<RtnlLinkRequest, RtnlLinkResponse>;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct MacAddr {
    pub inner: [u8; 6],
}

impl MacAddr {
    pub const fn new(inner: [u8; 6]) -> Self {
        Self { inner }
    }
}

impl Default for MacAddr {
    fn default() -> Self {
        Self { inner: [0; 6] }
    }
}

impl Debug for MacAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("MacAddr({})", self))
    }
}

impl Display for MacAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!(
            "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
            self.inner[0],
            self.inner[1],
            self.inner[2],
            self.inner[3],
            self.inner[4],
            self.inner[5],
        ))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Interface {
    pub if_name: String,
    pub if_id: u32,
}

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum RtnlLinkRequest {
    InterfaceList,
    InterfaceGet { if_id: u32 },
    InterfaceGetByName { if_name: String },
    MacAddrGet { if_id: u32 },
    MacAddrSet { if_id: u32, mac_addr: MacAddr },
    MtuGet { if_id: u32 },
    InterfaceSetAdmin { if_id: u32, up: bool },
    InterfaceSetPromisc { if_id: u32, enable: bool },
    InterfaceSetArp { if_id: u32, enable: bool },
    InterfaceSetMtu { if_id: u32, mtu: u32 },
    InterfaceRename { if_id: u32, if_name: String },
    InterfaceSetAllMulticast { if_id: u32, enable: bool },
}

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum RtnlLinkResponse {
    Success,
    Failed,
    NotImplemented,
    NotFound,
    InterfaceList(Vec<Interface>),
    Interface(Interface),
    MacAddr(MacAddr),
    Mtu(u32),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RtnlLinkClient {
    client: Client,
}

impl RtnlLinkClient {
    pub(crate) fn new(client: Client) -> Self {
        Self { client }
    }

    pub fn interface_set_up(&self, if_id: u32) -> io::Result<()> {
        self.interface_set_admin_state(if_id, true)
    }

    pub fn interface_set_down(&self, if_id: u32) -> io::Result<()> {
        self.interface_set_admin_state(if_id, false)
    }

    pub fn interface_set_admin_state(&self, if_id: u32, up: bool) -> io::Result<()> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::InterfaceSetAdmin { if_id, up })?;
        let op = if up {
            "Set interface up"
        } else {
            "Set interface down"
        };
        handle_status_response(op, res)
    }

    pub fn interface_set_promiscuous(&self, if_id: u32, enable: bool) -> io::Result<()> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::InterfaceSetPromisc { if_id, enable })?;
        handle_status_response(
            if enable {
                "Enable promiscuous mode"
            } else {
                "Disable promiscuous mode"
            },
            res,
        )
    }

    pub fn interface_set_arp(&self, if_id: u32, enable: bool) -> io::Result<()> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::InterfaceSetArp { if_id, enable })?;
        handle_status_response(if enable { "Enable ARP" } else { "Disable ARP" }, res)
    }

    pub fn interface_set_mtu(&self, if_id: u32, mtu: u32) -> io::Result<()> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::InterfaceSetMtu { if_id, mtu })?;
        handle_status_response("Set MTU", res)
    }

    pub fn interface_rename(&self, if_id: u32, new_name: &str) -> io::Result<()> {
        let res = self.client.send_request(RtnlLinkRequest::InterfaceRename {
            if_id,
            if_name: new_name.to_owned(),
        })?;
        handle_status_response("Rename interface", res)
    }

    pub fn interface_get(&self, if_id: u32) -> io::Result<Interface> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::InterfaceGet { if_id })?;
        match res {
            RtnlLinkResponse::Interface(interface) => Ok(interface),
            RtnlLinkResponse::NotFound => {
                Err(io::Error::new(ErrorKind::NotFound, "Interface not found"))
            }
            _ => Err(io::Error::other("Failed to get interface")),
        }
    }

    pub fn interface_get_by_name(&self, name: &str) -> std::io::Result<Interface> {
        let name = name.to_owned();
        let res = self
            .client
            .send_request(RtnlLinkRequest::InterfaceGetByName { if_name: name })?;
        match res {
            RtnlLinkResponse::Interface(interface) => {
                return Ok(interface);
            }
            _ => {}
        }
        Err(std::io::Error::other("Not found"))
    }

    pub fn mac_addr_get(&self, if_id: u32) -> std::io::Result<Option<MacAddr>> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::MacAddrGet { if_id })?;
        match res {
            RtnlLinkResponse::MacAddr(addr) => {
                return Ok(Some(addr));
            }
            _ => {}
        }
        Ok(None)
    }

    pub fn mtu_get(&self, if_id: u32) -> io::Result<u32> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::MtuGet { if_id })?;
        match res {
            RtnlLinkResponse::Mtu(mtu) => Ok(mtu),
            RtnlLinkResponse::NotFound => {
                Err(io::Error::new(ErrorKind::NotFound, "Interface not found"))
            }
            _ => Err(io::Error::other("Failed to get MTU")),
        }
    }

    pub fn mac_addr_set(&self, if_id: u32, mac_addr: MacAddr) -> io::Result<()> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::MacAddrSet { if_id, mac_addr })?;
        handle_status_response("Set MAC address", res)
    }

    pub fn interface_set_all_multicast(&self, if_id: u32, enable: bool) -> io::Result<()> {
        let res = self
            .client
            .send_request(RtnlLinkRequest::InterfaceSetAllMulticast { if_id, enable })?;
        handle_status_response(
            if enable {
                "Enable all-multicast"
            } else {
                "Disable all-multicast"
            },
            res,
        )
    }

    pub fn interface_list(&self) -> std::io::Result<Vec<Interface>> {
        let res = self.client.send_request(RtnlLinkRequest::InterfaceList)?;
        match res {
            RtnlLinkResponse::InterfaceList(list) => {
                return Ok(list);
            }
            _ => {}
        }
        Err(std::io::Error::other("Unknown error"))
    }
}

fn handle_status_response(op: &str, response: RtnlLinkResponse) -> io::Result<()> {
    match response {
        RtnlLinkResponse::Success => Ok(()),
        RtnlLinkResponse::NotFound => Err(io::Error::new(
            ErrorKind::NotFound,
            format!("{}: interface not found", op),
        )),
        RtnlLinkResponse::Failed => Err(io::Error::other(format!("{} failed", op))),
        RtnlLinkResponse::NotImplemented => Err(io::Error::new(
            ErrorKind::Unsupported,
            format!("{} not implemented", op),
        )),
        other => Err(io::Error::other(format!(
            "{} returned unexpected response: {:?}",
            op, other
        ))),
    }
}

async fn apply_link_set<F>(
    handle: &rtnetlink::LinkHandle,
    if_id: u32,
    op: F,
) -> Result<(), rtnetlink::Error>
where
    F: FnOnce(LinkMessageBuilder<LinkUnspec>) -> LinkMessageBuilder<LinkUnspec>,
{
    let builder = LinkMessageBuilder::<LinkUnspec>::new().index(if_id);
    let message = op(builder).build();
    handle.set(message).execute().await
}

fn map_link_result(result: Result<(), rtnetlink::Error>, op: &str, if_id: u32) -> RtnlLinkResponse {
    match result {
        Ok(()) => RtnlLinkResponse::Success,
        Err(rtnetlink::Error::NetlinkError(err_msg)) => {
            let io_err = err_msg.to_io();
            if io_err.kind() == ErrorKind::NotFound {
                RtnlLinkResponse::NotFound
            } else {
                log::warn!("Failed to {} for ifindex {}: {}", op, if_id, io_err);
                RtnlLinkResponse::Failed
            }
        }
        Err(err) => {
            log::warn!("Failed to {} for ifindex {}: {}", op, if_id, err);
            RtnlLinkResponse::Failed
        }
    }
}

pub(crate) async fn run_server(mut server: Server, mut handle: rtnetlink::LinkHandle) {
    'reqloop: while let Some((req, respond)) = server.accept().await {
        match req {
            RtnlLinkRequest::InterfaceGet { if_id } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let response = handle.get().match_index(if_id).execute();
                futures::pin_mut!(response);
                while let Ok(Some(response)) = response.try_next().await {
                    let mut if_name = None;
                    for attr in response.attributes.iter() {
                        if let netlink_packet_route::link::LinkAttribute::IfName(name) = attr {
                            if_name = Some(name.clone());
                        }
                    }

                    if let Some(name) = if_name {
                        respond(RtnlLinkResponse::Interface(Interface {
                            if_id,
                            if_name: name,
                        }));
                        continue 'reqloop;
                    }
                }
                respond(RtnlLinkResponse::NotFound);
            }
            RtnlLinkRequest::InterfaceGetByName { if_name } => {
                let response = handle.get().match_name(if_name.to_owned()).execute();
                futures::pin_mut!(response);
                while let Ok(Some(response)) = response.try_next().await {
                    let if_index = response.header.index;
                    if if_index == 0 {
                        continue;
                    }

                    respond(RtnlLinkResponse::Interface(Interface {
                        if_id: if_index,
                        if_name: if_name.to_owned(),
                    }));
                    continue 'reqloop;
                }
                respond(RtnlLinkResponse::NotFound);
            }
            RtnlLinkRequest::MacAddrGet { if_id } => {
                let if_index = if_id;
                if if_index == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }
                let response = handle.get().match_index(if_index).execute();
                futures::pin_mut!(response);
                while let Ok(Some(response)) = response.try_next().await {
                    for link in response.attributes.iter() {
                        match link {
                            netlink_packet_route::link::LinkAttribute::Address(addr) => {
                                respond(RtnlLinkResponse::MacAddr(MacAddr::new(
                                    addr[0..6].try_into().unwrap_or([0; 6]),
                                )));
                                continue 'reqloop;
                            }
                            _ => {}
                        }
                    }
                }
                respond(RtnlLinkResponse::NotFound);
            }
            RtnlLinkRequest::MtuGet { if_id } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let response = handle.get().match_index(if_id).execute();
                futures::pin_mut!(response);
                while let Ok(Some(response)) = response.try_next().await {
                    for link in response.attributes.iter() {
                        if let netlink_packet_route::link::LinkAttribute::Mtu(mtu) = link {
                            respond(RtnlLinkResponse::Mtu(*mtu));
                            continue 'reqloop;
                        }
                    }
                }
                respond(RtnlLinkResponse::NotFound);
            }
            RtnlLinkRequest::InterfaceList => {
                let mut interfaces = Vec::new();
                let response = handle.get().execute();
                futures::pin_mut!(response);
                while let Ok(Some(response)) = response.try_next().await {
                    let if_index = response.header.index;
                    let mut if_name = None;
                    for link in response.attributes.iter() {
                        match link {
                            netlink_packet_route::link::LinkAttribute::IfName(name) => {
                                if_name = Some(name.clone());
                            }
                            _ => {}
                        }
                    }

                    if if_index == 0 || if_name.is_none() {
                        continue;
                    }

                    interfaces.push(Interface {
                        if_id: if_index,
                        if_name: if_name.unwrap(),
                    });
                }
                respond(RtnlLinkResponse::InterfaceList(interfaces));
            }
            RtnlLinkRequest::MacAddrSet { if_id, mac_addr } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let mac_bytes = mac_addr.inner.to_vec();
                let result =
                    apply_link_set(&handle, if_id, |builder| builder.address(mac_bytes)).await;
                respond(map_link_result(result, "set MAC address", if_id));
            }
            RtnlLinkRequest::InterfaceSetAdmin { if_id, up } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let op_desc = if up {
                    "set interface up"
                } else {
                    "set interface down"
                };
                let result = apply_link_set(&handle, if_id, |builder| {
                    if up { builder.up() } else { builder.down() }
                })
                .await;

                respond(map_link_result(result, op_desc, if_id));
            }
            RtnlLinkRequest::InterfaceSetPromisc { if_id, enable } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let op_desc = if enable {
                    "enable promiscuous mode"
                } else {
                    "disable promiscuous mode"
                };
                let result =
                    apply_link_set(&handle, if_id, |builder| builder.promiscuous(enable)).await;

                respond(map_link_result(result, op_desc, if_id));
            }
            RtnlLinkRequest::InterfaceSetArp { if_id, enable } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let op_desc = if enable { "enable ARP" } else { "disable ARP" };
                let result = apply_link_set(&handle, if_id, |builder| builder.arp(enable)).await;

                respond(map_link_result(result, op_desc, if_id));
            }
            RtnlLinkRequest::InterfaceSetMtu { if_id, mtu } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let result = apply_link_set(&handle, if_id, |builder| builder.mtu(mtu)).await;
                respond(map_link_result(result, "set MTU", if_id));
            }
            RtnlLinkRequest::InterfaceRename { if_id, if_name } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let new_name = if_name.clone();
                let result = apply_link_set(&handle, if_id, |builder| builder.name(new_name)).await;
                let op_desc = format!("rename interface to {}", if_name);
                respond(map_link_result(result, &op_desc, if_id));
            }
            RtnlLinkRequest::InterfaceSetAllMulticast { if_id, enable } => {
                if if_id == 0 {
                    respond(RtnlLinkResponse::NotFound);
                    continue 'reqloop;
                }

                let op_desc = if enable {
                    "enable all-multicast mode"
                } else {
                    "disable all-multicast mode"
                };

                let mut message = LinkMessageBuilder::<LinkUnspec>::new().index(if_id).build();
                if enable {
                    message.header.flags |= LinkFlags::Allmulti;
                } else {
                    message.header.flags.remove(LinkFlags::Allmulti);
                }
                message.header.change_mask |= LinkFlags::Allmulti;

                let result = handle.set(message).execute().await;

                respond(map_link_result(result, op_desc, if_id));
            }
            _ => respond(RtnlLinkResponse::NotImplemented),
        }
    }
}