mirror of
https://github.com/threefoldtech/mycelium.git
synced 2026-06-06 07:41:41 +00:00
further ipv6 impl
This commit is contained in:
@@ -37,6 +37,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// Generate the node's IPv6 address from its public key
|
||||
let node_addr = x25519::generate_addr_from_pubkey(&node_keypair.1);
|
||||
println!("Node address: {}", node_addr);
|
||||
|
||||
// Create TUN interface and add static route
|
||||
let node_tun = match node_setup::setup_node(node_addr).await {
|
||||
@@ -77,6 +78,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// Read packets from the TUN interface (originating from the kernel) and send them to the router
|
||||
// Note: we will never receive control packets from the kernel, only data packets
|
||||
// filter out packets that are not destined for 200::/7
|
||||
{
|
||||
let router = router.clone();
|
||||
let node_tun = node_tun.clone();
|
||||
|
||||
+19
-9
@@ -37,32 +37,30 @@ pub async fn retrieve_tun_link_index(handle: Handle) -> Result<u32, Box<dyn std:
|
||||
}
|
||||
|
||||
// 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?;
|
||||
pub async fn add_address(handle: Handle, addr: Ipv6Addr, link_index: u32) -> Result<u32, Box<dyn std::error::Error>> {
|
||||
// add address to tun interface
|
||||
handle
|
||||
.address()
|
||||
.add(
|
||||
link_index,
|
||||
IpAddr::V6(addr),
|
||||
7,
|
||||
64,
|
||||
)
|
||||
.execute()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
Ok(link_index)
|
||||
}
|
||||
|
||||
|
||||
// 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?;
|
||||
pub async fn add_route(handle: Handle, link_index: u32) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// 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)
|
||||
.destination_prefix(TUN_ROUTE_DEST, TUN_ROUTE_PREFIX)
|
||||
.output_interface(link_index)
|
||||
.execute()
|
||||
.await?;
|
||||
@@ -83,10 +81,21 @@ pub async fn setup_node(addr: Ipv6Addr) -> Result<Arc<Tun>, Box<dyn std::error::
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let (conn, handle, _) = rtnetlink::new_connection()?;
|
||||
tokio::spawn(conn);
|
||||
|
||||
match add_address(handle.clone(), addr).await {
|
||||
let tun_link_index = match retrieve_tun_link_index(handle.clone()).await {
|
||||
Ok(link_index) => {
|
||||
println!("TUN interface link index retrieved");
|
||||
link_index
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("Error retrieving TUN interface link index: {}", e);
|
||||
}
|
||||
};
|
||||
|
||||
match add_address(handle.clone(), addr, tun_link_index).await {
|
||||
Ok(_) => {
|
||||
println!("Address added to TUN interface");
|
||||
}
|
||||
@@ -95,7 +104,8 @@ pub async fn setup_node(addr: Ipv6Addr) -> Result<Arc<Tun>, Box<dyn std::error::
|
||||
}
|
||||
};
|
||||
|
||||
match add_route(handle.clone()).await {
|
||||
// add route for /7 (global scope for the overlay)
|
||||
match add_route(handle.clone(), tun_link_index).await {
|
||||
Ok(_) => {
|
||||
println!("Route added to TUN interface");
|
||||
}
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ pub fn generate_addr_from_pubkey(pubkey: &PublicKey) -> Ipv6Addr {
|
||||
hasher.finalize_variable(&mut buf).unwrap();
|
||||
|
||||
let ipv6_bytes: [u8; 16] = [
|
||||
0x20, 0x00, // This prefix ensures the address falls into the 200::/7 range
|
||||
0x02, 0x00, // This prefix ensures the address falls into the 200::/7 range
|
||||
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10],
|
||||
buf[11], buf[12], buf[13],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user