mirror of
https://github.com/threefoldtech/mycelium.git
synced 2026-06-07 00:01:40 +00:00
further ipv6 support + addr generation from pubkey
This commit is contained in:
+72
-26
@@ -1,16 +1,20 @@
|
||||
use futures::stream::TryStreamExt;
|
||||
use futures::TryStreamExt;
|
||||
use rtnetlink::Handle;
|
||||
use std::{error::Error, net::{Ipv4Addr, Ipv6Addr}, sync::Arc};
|
||||
use x25519_dalek::PublicKey;
|
||||
use std::{
|
||||
net::{IpAddr, Ipv6Addr},
|
||||
sync::Arc,
|
||||
};
|
||||
use tokio_tun::{Tun, TunBuilder};
|
||||
|
||||
pub const TUN_NAME: &str = "tun0";
|
||||
pub const TUN_ROUTE_DEST: Ipv6Addr = Ipv6Addr::new(0xfd, 0x00, 0, 0, 0, 0, 0, 0);
|
||||
pub const TUN_ROUTE_PREFIX: u8 = 16;
|
||||
pub const TUN_ROUTE_DEST: Ipv6Addr = Ipv6Addr::new(0x200, 0, 0, 0, 0, 0, 0, 0);
|
||||
pub const TUN_ROUTE_PREFIX: u8 = 7;
|
||||
|
||||
// Create a TUN interface
|
||||
pub fn create_tun_interface() -> Result<Arc<Tun>, Box<dyn Error>> {
|
||||
pub fn create_tun_interface() -> Result<Arc<Tun>, Box<dyn std::error::Error>> {
|
||||
let tun = TunBuilder::new()
|
||||
.name(TUN_NAME)
|
||||
.name("tun0")
|
||||
.tap(false)
|
||||
.mtu(1420)
|
||||
.packet_info(false)
|
||||
@@ -20,43 +24,85 @@ pub fn create_tun_interface() -> Result<Arc<Tun>, Box<dyn Error>> {
|
||||
Ok(Arc::new(tun))
|
||||
}
|
||||
|
||||
// Add a route to the TUN interface
|
||||
pub async fn add_route(handle: Handle) -> Result<(), Box<dyn Error>> {
|
||||
let mut link_request = handle
|
||||
.link()
|
||||
.get()
|
||||
.match_name(String::from(TUN_NAME))
|
||||
.execute();
|
||||
|
||||
let link_idx = if let Some(link) = link_request.try_next().await? {
|
||||
pub async fn retrieve_tun_link_index(handle: Handle) -> Result<u32, Box<dyn std::error::Error>> {
|
||||
let mut link_req = handle.link().get().match_name(TUN_NAME.to_string()).execute();
|
||||
let link_index = if let Some(link) = link_req.try_next().await? {
|
||||
link.header.index
|
||||
} else {
|
||||
eprintln!("link not found");
|
||||
panic!("link not found");
|
||||
};
|
||||
|
||||
let route = handle.route();
|
||||
route
|
||||
.add()
|
||||
.v4()
|
||||
.destination_prefix(TUN_ROUTE_DEST, TUN_ROUTE_PREFIX)
|
||||
.output_interface(link_idx)
|
||||
Ok(link_index)
|
||||
}
|
||||
|
||||
// Add address to TUN interface
|
||||
pub async fn add_address(handle: Handle, addr: Ipv6Addr) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let link_index = retrieve_tun_link_index(handle.clone()).await?;
|
||||
// add address to tun interface
|
||||
handle
|
||||
.address()
|
||||
.add(
|
||||
link_index,
|
||||
IpAddr::V6(addr),
|
||||
7,
|
||||
)
|
||||
.execute()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn setup_node(tun_addr: Ipv6Addr) -> Result<Arc<Tun>, Box<dyn Error>> {
|
||||
let tun = create_tun_interface(tun_addr)?;
|
||||
println!("Interface '{}' ({}) created", TUN_NAME, tun_addr);
|
||||
|
||||
// Adding route to TUN interface
|
||||
pub async fn add_route(handle: Handle) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let link_index = retrieve_tun_link_index(handle.clone()).await?;
|
||||
// add route to tun interface
|
||||
let route = handle.route();
|
||||
route
|
||||
.add()
|
||||
.v6()
|
||||
.destination_prefix(Ipv6Addr::new(0x200, 0, 0, 0, 0, 0, 0, 0), 7)
|
||||
.output_interface(link_index)
|
||||
.execute()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub async fn setup_node(addr: Ipv6Addr) -> Result<Arc<Tun>, Box<dyn std::error::Error>> {
|
||||
|
||||
let tun = match create_tun_interface() {
|
||||
Ok(tun) => {
|
||||
println!("TUN interface created");
|
||||
tun
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("Error creating TUN interface: {}", e);
|
||||
}
|
||||
};
|
||||
|
||||
let (conn, handle, _) = rtnetlink::new_connection()?;
|
||||
tokio::spawn(conn);
|
||||
|
||||
add_route(handle.clone()).await?;
|
||||
match add_address(handle.clone(), addr).await {
|
||||
Ok(_) => {
|
||||
println!("Address added to TUN interface");
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("Error adding address to TUN interface: {}", e);
|
||||
}
|
||||
};
|
||||
|
||||
println!("Static route created");
|
||||
match add_route(handle.clone()).await {
|
||||
Ok(_) => {
|
||||
println!("Route added to TUN interface");
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("Error adding route to TUN interface: {}", e);
|
||||
}
|
||||
};
|
||||
|
||||
Ok(tun)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user