From be92fc9d6fd534265a1efdcffdc68b371629629b Mon Sep 17 00:00:00 2001 From: Maxime Van Hees Date: Tue, 23 May 2023 09:51:46 +0000 Subject: [PATCH] further ipv6 impl --- src/main.rs | 2 ++ src/node_setup.rs | 28 +++++++++++++++++++--------- src/x25519.rs | 2 +- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 71b07d8..ef803c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ async fn main() -> Result<(), Box> { // 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> { // 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(); diff --git a/src/node_setup.rs b/src/node_setup.rs index 3180f13..4b65a9f 100644 --- a/src/node_setup.rs +++ b/src/node_setup.rs @@ -37,32 +37,30 @@ pub async fn retrieve_tun_link_index(handle: Handle) -> Result Result<(), Box> { - let link_index = retrieve_tun_link_index(handle.clone()).await?; +pub async fn add_address(handle: Handle, addr: Ipv6Addr, link_index: u32) -> Result> { // 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> { - let link_index = retrieve_tun_link_index(handle.clone()).await?; +pub async fn add_route(handle: Handle, link_index: u32) -> Result<(), Box> { // 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, Box { + 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, Box { println!("Route added to TUN interface"); } diff --git a/src/x25519.rs b/src/x25519.rs index d9d1dd3..3c66c30 100644 --- a/src/x25519.rs +++ b/src/x25519.rs @@ -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], ];