1#![allow(unreachable_patterns)]
2
3use ftth_common::channel::{AsyncWorldClient, AsyncWorldServer};
4
5use futures::TryStreamExt;
6
7use std::fmt::{Debug, Display};
8use std::io::{self, ErrorKind};
9
10use netlink_packet_route::link::LinkFlags;
11use rtnetlink::{LinkMessageBuilder, LinkUnspec};
12
13pub(crate) type Client = AsyncWorldClient<RtnlLinkRequest, RtnlLinkResponse>;
14pub(crate) type Server = AsyncWorldServer<RtnlLinkRequest, RtnlLinkResponse>;
15
16#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
17pub struct MacAddr {
18 pub inner: [u8; 6],
19}
20
21impl MacAddr {
22 pub const fn new(inner: [u8; 6]) -> Self {
23 Self { inner }
24 }
25}
26
27impl Default for MacAddr {
28 fn default() -> Self {
29 Self { inner: [0; 6] }
30 }
31}
32
33impl Debug for MacAddr {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 f.write_str(&format!("MacAddr({})", self))
36 }
37}
38
39impl Display for MacAddr {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 f.write_str(&format!(
42 "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
43 self.inner[0],
44 self.inner[1],
45 self.inner[2],
46 self.inner[3],
47 self.inner[4],
48 self.inner[5],
49 ))
50 }
51}
52
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct Interface {
55 pub if_name: String,
56 pub if_id: u32,
57}
58
59#[derive(Debug, Clone, PartialEq)]
60#[non_exhaustive]
61pub enum RtnlLinkRequest {
62 InterfaceList,
63 InterfaceGet { if_id: u32 },
64 InterfaceGetByName { if_name: String },
65 MacAddrGet { if_id: u32 },
66 MacAddrSet { if_id: u32, mac_addr: MacAddr },
67 MtuGet { if_id: u32 },
68 InterfaceSetAdmin { if_id: u32, up: bool },
69 InterfaceSetPromisc { if_id: u32, enable: bool },
70 InterfaceSetArp { if_id: u32, enable: bool },
71 InterfaceSetMtu { if_id: u32, mtu: u32 },
72 InterfaceRename { if_id: u32, if_name: String },
73 InterfaceSetAllMulticast { if_id: u32, enable: bool },
74}
75
76#[derive(Debug, Clone, PartialEq)]
77#[non_exhaustive]
78pub enum RtnlLinkResponse {
79 Success,
80 Failed,
81 NotImplemented,
82 NotFound,
83 InterfaceList(Vec<Interface>),
84 Interface(Interface),
85 MacAddr(MacAddr),
86 Mtu(u32),
87}
88
89#[derive(Debug, Clone, PartialEq, Eq, Hash)]
90pub struct RtnlLinkClient {
91 client: Client,
92}
93
94impl RtnlLinkClient {
95 pub(crate) fn new(client: Client) -> Self {
96 Self { client }
97 }
98
99 pub fn interface_set_up(&self, if_id: u32) -> io::Result<()> {
100 self.interface_set_admin_state(if_id, true)
101 }
102
103 pub fn interface_set_down(&self, if_id: u32) -> io::Result<()> {
104 self.interface_set_admin_state(if_id, false)
105 }
106
107 pub fn interface_set_admin_state(&self, if_id: u32, up: bool) -> io::Result<()> {
108 let res = self
109 .client
110 .send_request(RtnlLinkRequest::InterfaceSetAdmin { if_id, up })?;
111 let op = if up {
112 "Set interface up"
113 } else {
114 "Set interface down"
115 };
116 handle_status_response(op, res)
117 }
118
119 pub fn interface_set_promiscuous(&self, if_id: u32, enable: bool) -> io::Result<()> {
120 let res = self
121 .client
122 .send_request(RtnlLinkRequest::InterfaceSetPromisc { if_id, enable })?;
123 handle_status_response(
124 if enable {
125 "Enable promiscuous mode"
126 } else {
127 "Disable promiscuous mode"
128 },
129 res,
130 )
131 }
132
133 pub fn interface_set_arp(&self, if_id: u32, enable: bool) -> io::Result<()> {
134 let res = self
135 .client
136 .send_request(RtnlLinkRequest::InterfaceSetArp { if_id, enable })?;
137 handle_status_response(if enable { "Enable ARP" } else { "Disable ARP" }, res)
138 }
139
140 pub fn interface_set_mtu(&self, if_id: u32, mtu: u32) -> io::Result<()> {
141 let res = self
142 .client
143 .send_request(RtnlLinkRequest::InterfaceSetMtu { if_id, mtu })?;
144 handle_status_response("Set MTU", res)
145 }
146
147 pub fn interface_rename(&self, if_id: u32, new_name: &str) -> io::Result<()> {
148 let res = self.client.send_request(RtnlLinkRequest::InterfaceRename {
149 if_id,
150 if_name: new_name.to_owned(),
151 })?;
152 handle_status_response("Rename interface", res)
153 }
154
155 pub fn interface_get(&self, if_id: u32) -> io::Result<Interface> {
156 let res = self
157 .client
158 .send_request(RtnlLinkRequest::InterfaceGet { if_id })?;
159 match res {
160 RtnlLinkResponse::Interface(interface) => Ok(interface),
161 RtnlLinkResponse::NotFound => {
162 Err(io::Error::new(ErrorKind::NotFound, "Interface not found"))
163 }
164 _ => Err(io::Error::other("Failed to get interface")),
165 }
166 }
167
168 pub fn interface_get_by_name(&self, name: &str) -> std::io::Result<Interface> {
169 let name = name.to_owned();
170 let res = self
171 .client
172 .send_request(RtnlLinkRequest::InterfaceGetByName { if_name: name })?;
173 match res {
174 RtnlLinkResponse::Interface(interface) => {
175 return Ok(interface);
176 }
177 _ => {}
178 }
179 Err(std::io::Error::other("Not found"))
180 }
181
182 pub fn mac_addr_get(&self, if_id: u32) -> std::io::Result<Option<MacAddr>> {
183 let res = self
184 .client
185 .send_request(RtnlLinkRequest::MacAddrGet { if_id })?;
186 match res {
187 RtnlLinkResponse::MacAddr(addr) => {
188 return Ok(Some(addr));
189 }
190 _ => {}
191 }
192 Ok(None)
193 }
194
195 pub fn mtu_get(&self, if_id: u32) -> io::Result<u32> {
196 let res = self
197 .client
198 .send_request(RtnlLinkRequest::MtuGet { if_id })?;
199 match res {
200 RtnlLinkResponse::Mtu(mtu) => Ok(mtu),
201 RtnlLinkResponse::NotFound => {
202 Err(io::Error::new(ErrorKind::NotFound, "Interface not found"))
203 }
204 _ => Err(io::Error::other("Failed to get MTU")),
205 }
206 }
207
208 pub fn mac_addr_set(&self, if_id: u32, mac_addr: MacAddr) -> io::Result<()> {
209 let res = self
210 .client
211 .send_request(RtnlLinkRequest::MacAddrSet { if_id, mac_addr })?;
212 handle_status_response("Set MAC address", res)
213 }
214
215 pub fn interface_set_all_multicast(&self, if_id: u32, enable: bool) -> io::Result<()> {
216 let res = self
217 .client
218 .send_request(RtnlLinkRequest::InterfaceSetAllMulticast { if_id, enable })?;
219 handle_status_response(
220 if enable {
221 "Enable all-multicast"
222 } else {
223 "Disable all-multicast"
224 },
225 res,
226 )
227 }
228
229 pub fn interface_list(&self) -> std::io::Result<Vec<Interface>> {
230 let res = self.client.send_request(RtnlLinkRequest::InterfaceList)?;
231 match res {
232 RtnlLinkResponse::InterfaceList(list) => {
233 return Ok(list);
234 }
235 _ => {}
236 }
237 Err(std::io::Error::other("Unknown error"))
238 }
239}
240
241fn handle_status_response(op: &str, response: RtnlLinkResponse) -> io::Result<()> {
242 match response {
243 RtnlLinkResponse::Success => Ok(()),
244 RtnlLinkResponse::NotFound => Err(io::Error::new(
245 ErrorKind::NotFound,
246 format!("{}: interface not found", op),
247 )),
248 RtnlLinkResponse::Failed => Err(io::Error::other(format!("{} failed", op))),
249 RtnlLinkResponse::NotImplemented => Err(io::Error::new(
250 ErrorKind::Unsupported,
251 format!("{} not implemented", op),
252 )),
253 other => Err(io::Error::other(format!(
254 "{} returned unexpected response: {:?}",
255 op, other
256 ))),
257 }
258}
259
260async fn apply_link_set<F>(
261 handle: &rtnetlink::LinkHandle,
262 if_id: u32,
263 op: F,
264) -> Result<(), rtnetlink::Error>
265where
266 F: FnOnce(LinkMessageBuilder<LinkUnspec>) -> LinkMessageBuilder<LinkUnspec>,
267{
268 let builder = LinkMessageBuilder::<LinkUnspec>::new().index(if_id);
269 let message = op(builder).build();
270 handle.set(message).execute().await
271}
272
273fn map_link_result(result: Result<(), rtnetlink::Error>, op: &str, if_id: u32) -> RtnlLinkResponse {
274 match result {
275 Ok(()) => RtnlLinkResponse::Success,
276 Err(rtnetlink::Error::NetlinkError(err_msg)) => {
277 let io_err = err_msg.to_io();
278 if io_err.kind() == ErrorKind::NotFound {
279 RtnlLinkResponse::NotFound
280 } else {
281 log::warn!("Failed to {} for ifindex {}: {}", op, if_id, io_err);
282 RtnlLinkResponse::Failed
283 }
284 }
285 Err(err) => {
286 log::warn!("Failed to {} for ifindex {}: {}", op, if_id, err);
287 RtnlLinkResponse::Failed
288 }
289 }
290}
291
292pub(crate) async fn run_server(mut server: Server, mut handle: rtnetlink::LinkHandle) {
293 'reqloop: while let Some((req, respond)) = server.accept().await {
294 match req {
295 RtnlLinkRequest::InterfaceGet { if_id } => {
296 if if_id == 0 {
297 respond(RtnlLinkResponse::NotFound);
298 continue 'reqloop;
299 }
300
301 let response = handle.get().match_index(if_id).execute();
302 futures::pin_mut!(response);
303 while let Ok(Some(response)) = response.try_next().await {
304 let mut if_name = None;
305 for attr in response.attributes.iter() {
306 if let netlink_packet_route::link::LinkAttribute::IfName(name) = attr {
307 if_name = Some(name.clone());
308 }
309 }
310
311 if let Some(name) = if_name {
312 respond(RtnlLinkResponse::Interface(Interface {
313 if_id,
314 if_name: name,
315 }));
316 continue 'reqloop;
317 }
318 }
319 respond(RtnlLinkResponse::NotFound);
320 }
321 RtnlLinkRequest::InterfaceGetByName { if_name } => {
322 let response = handle.get().match_name(if_name.to_owned()).execute();
323 futures::pin_mut!(response);
324 while let Ok(Some(response)) = response.try_next().await {
325 let if_index = response.header.index;
326 if if_index == 0 {
327 continue;
328 }
329
330 respond(RtnlLinkResponse::Interface(Interface {
331 if_id: if_index,
332 if_name: if_name.to_owned(),
333 }));
334 continue 'reqloop;
335 }
336 respond(RtnlLinkResponse::NotFound);
337 }
338 RtnlLinkRequest::MacAddrGet { if_id } => {
339 let if_index = if_id;
340 if if_index == 0 {
341 respond(RtnlLinkResponse::NotFound);
342 continue 'reqloop;
343 }
344 let response = handle.get().match_index(if_index).execute();
345 futures::pin_mut!(response);
346 while let Ok(Some(response)) = response.try_next().await {
347 for link in response.attributes.iter() {
348 match link {
349 netlink_packet_route::link::LinkAttribute::Address(addr) => {
350 respond(RtnlLinkResponse::MacAddr(MacAddr::new(
351 addr[0..6].try_into().unwrap_or([0; 6]),
352 )));
353 continue 'reqloop;
354 }
355 _ => {}
356 }
357 }
358 }
359 respond(RtnlLinkResponse::NotFound);
360 }
361 RtnlLinkRequest::MtuGet { if_id } => {
362 if if_id == 0 {
363 respond(RtnlLinkResponse::NotFound);
364 continue 'reqloop;
365 }
366
367 let response = handle.get().match_index(if_id).execute();
368 futures::pin_mut!(response);
369 while let Ok(Some(response)) = response.try_next().await {
370 for link in response.attributes.iter() {
371 if let netlink_packet_route::link::LinkAttribute::Mtu(mtu) = link {
372 respond(RtnlLinkResponse::Mtu(*mtu));
373 continue 'reqloop;
374 }
375 }
376 }
377 respond(RtnlLinkResponse::NotFound);
378 }
379 RtnlLinkRequest::InterfaceList => {
380 let mut interfaces = Vec::new();
381 let response = handle.get().execute();
382 futures::pin_mut!(response);
383 while let Ok(Some(response)) = response.try_next().await {
384 let if_index = response.header.index;
385 let mut if_name = None;
386 for link in response.attributes.iter() {
387 match link {
388 netlink_packet_route::link::LinkAttribute::IfName(name) => {
389 if_name = Some(name.clone());
390 }
391 _ => {}
392 }
393 }
394
395 if if_index == 0 || if_name.is_none() {
396 continue;
397 }
398
399 interfaces.push(Interface {
400 if_id: if_index,
401 if_name: if_name.unwrap(),
402 });
403 }
404 respond(RtnlLinkResponse::InterfaceList(interfaces));
405 }
406 RtnlLinkRequest::MacAddrSet { if_id, mac_addr } => {
407 if if_id == 0 {
408 respond(RtnlLinkResponse::NotFound);
409 continue 'reqloop;
410 }
411
412 let mac_bytes = mac_addr.inner.to_vec();
413 let result =
414 apply_link_set(&handle, if_id, |builder| builder.address(mac_bytes)).await;
415 respond(map_link_result(result, "set MAC address", if_id));
416 }
417 RtnlLinkRequest::InterfaceSetAdmin { if_id, up } => {
418 if if_id == 0 {
419 respond(RtnlLinkResponse::NotFound);
420 continue 'reqloop;
421 }
422
423 let op_desc = if up {
424 "set interface up"
425 } else {
426 "set interface down"
427 };
428 let result = apply_link_set(&handle, if_id, |builder| {
429 if up { builder.up() } else { builder.down() }
430 })
431 .await;
432
433 respond(map_link_result(result, op_desc, if_id));
434 }
435 RtnlLinkRequest::InterfaceSetPromisc { if_id, enable } => {
436 if if_id == 0 {
437 respond(RtnlLinkResponse::NotFound);
438 continue 'reqloop;
439 }
440
441 let op_desc = if enable {
442 "enable promiscuous mode"
443 } else {
444 "disable promiscuous mode"
445 };
446 let result =
447 apply_link_set(&handle, if_id, |builder| builder.promiscuous(enable)).await;
448
449 respond(map_link_result(result, op_desc, if_id));
450 }
451 RtnlLinkRequest::InterfaceSetArp { if_id, enable } => {
452 if if_id == 0 {
453 respond(RtnlLinkResponse::NotFound);
454 continue 'reqloop;
455 }
456
457 let op_desc = if enable { "enable ARP" } else { "disable ARP" };
458 let result = apply_link_set(&handle, if_id, |builder| builder.arp(enable)).await;
459
460 respond(map_link_result(result, op_desc, if_id));
461 }
462 RtnlLinkRequest::InterfaceSetMtu { if_id, mtu } => {
463 if if_id == 0 {
464 respond(RtnlLinkResponse::NotFound);
465 continue 'reqloop;
466 }
467
468 let result = apply_link_set(&handle, if_id, |builder| builder.mtu(mtu)).await;
469 respond(map_link_result(result, "set MTU", if_id));
470 }
471 RtnlLinkRequest::InterfaceRename { if_id, if_name } => {
472 if if_id == 0 {
473 respond(RtnlLinkResponse::NotFound);
474 continue 'reqloop;
475 }
476
477 let new_name = if_name.clone();
478 let result = apply_link_set(&handle, if_id, |builder| builder.name(new_name)).await;
479 let op_desc = format!("rename interface to {}", if_name);
480 respond(map_link_result(result, &op_desc, if_id));
481 }
482 RtnlLinkRequest::InterfaceSetAllMulticast { if_id, enable } => {
483 if if_id == 0 {
484 respond(RtnlLinkResponse::NotFound);
485 continue 'reqloop;
486 }
487
488 let op_desc = if enable {
489 "enable all-multicast mode"
490 } else {
491 "disable all-multicast mode"
492 };
493
494 let mut message = LinkMessageBuilder::<LinkUnspec>::new().index(if_id).build();
495 if enable {
496 message.header.flags |= LinkFlags::Allmulti;
497 } else {
498 message.header.flags.remove(LinkFlags::Allmulti);
499 }
500 message.header.change_mask |= LinkFlags::Allmulti;
501
502 let result = handle.set(message).execute().await;
503
504 respond(map_link_result(result, op_desc, if_id));
505 }
506 _ => respond(RtnlLinkResponse::NotImplemented),
507 }
508 }
509}