Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
indexds committed Jan 8, 2025
1 parent db1f158 commit ba74ffa
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ opt-level = "z"

[patch.crates-io]
esp-idf-svc = { git = "https://github.com/indexds/esp-idf-svc", branch = "napt" }
esp-idf-sys = { git = "https://github.com/esp-rs/esp-idf-sys", branch = "master" }
esp-idf-sys = { git = "https://github.com/indexds/esp-idf-sys", branch = "expose_binding" }
esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal", branch = "master" }
embedded-hal = { git = "https://github.com/rust-embedded/embedded-hal", branch = "master" }
embedded-svc = { git = "https://github.com/esp-rs/embedded-svc", branch = "master" }
Expand Down
58 changes: 58 additions & 0 deletions src/network/bridge.rs
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
use core::ptr;
use std::ffi::CString;
use std::sync::{Arc, Mutex};

use esp_idf_svc::eth::{EspEth, RmiiEth};
use esp_idf_svc::handle::RawHandle;
use esp_idf_svc::netif::EspNetif;
use esp_idf_svc::sys::{
_g_esp_netif_netstack_default_br,
bridgeif_config,
esp,
esp_netif_attach,
esp_netif_br_glue_add_port,
esp_netif_br_glue_new,
esp_netif_config_t,
esp_netif_flags_ESP_NETIF_FLAG_IS_BRIDGE,
esp_netif_inherent_config,
esp_netif_new,
};

fn start(eth: Arc<Mutex<EspEth<'static, RmiiEth>>>, wg: Arc<Mutex<EspNetif>>) -> anyhow::Result<()> {
let bridge_info = Box::new(bridgeif_config {
max_fdb_dyn_entries: 10,
max_fdb_sta_entries: 10,
max_ports: 2,
});

let base = Box::new(esp_netif_inherent_config {
flags: esp_netif_flags_ESP_NETIF_FLAG_IS_BRIDGE,
mac: [0x02, 0x00, 0x00, 0x00, 0x00, 0x10],
ip_info: ptr::null_mut(),
get_ip_event: 0,
lost_ip_event: 0,
if_key: CString::new("br0")?.into_raw(),
if_desc: CString::new("bridge")?.into_raw(),
route_prio: 30,
bridge_info: Box::into_raw(bridge_info),
});

let netif_conf = esp_netif_config_t {
base: Box::into_raw(base),
driver: ptr::null_mut(),
stack: unsafe { _g_esp_netif_netstack_default_br },
};

let bridge_netif = unsafe { esp_netif_new(&netif_conf) };

let glue = unsafe { esp_netif_br_glue_new() };

let eth_handle = eth.lock().unwrap().netif_mut().handle();
let wg_handle = wg.lock().unwrap().handle();

esp!(unsafe { esp_netif_br_glue_add_port(glue, eth_handle) })?;
esp!(unsafe { esp_netif_br_glue_add_port(glue, wg_handle) })?;

esp!(unsafe { esp_netif_attach(bridge_netif, glue as _) })?;

Ok(())
}
7 changes: 2 additions & 5 deletions src/wireguard/esp_wireguard/esp_wireguard/src/esp_wireguard.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ static esp_err_t esp_wireguard_netif_create(const wireguard_config_t *config)
return err;
}

esp_err_t esp_wireguard_init(wireguard_config_t *config, wireguard_ctx_t *ctx)
esp_err_t esp_wireguard_init(wireguard_ctx_t *ctx)
{
esp_err_t err = ESP_FAIL;

if (!config || !ctx) {
if (!ctx) {
err = ESP_ERR_INVALID_ARG;
goto fail;
}
Expand All @@ -228,9 +228,6 @@ esp_err_t esp_wireguard_init(wireguard_config_t *config, wireguard_ctx_t *ctx)
ESP_LOGE(TAG, "wireguard_platform_init: %s", esp_err_to_name(err));
goto fail;
}
ctx->config = config;
ctx->netif = NULL;
ctx->netif_default = netif_default;

err = ESP_OK;
fail:
Expand Down
63 changes: 42 additions & 21 deletions src/wireguard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use std::ffi::CString;
use std::net::Ipv4Addr;
use std::num::NonZeroU32;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use ctx::WG_CTX;
use esp_idf_svc::handle::RawHandle;
use esp_idf_svc::ipv4::{ClientSettings, Mask, Subnet};
use esp_idf_svc::netif::{EspNetif, NetifConfiguration};
use esp_idf_svc::nvs::{EspNvs, NvsDefault};
use esp_idf_svc::sntp::{EspSntp, SyncStatus};
use esp_idf_svc::sys::esp;
use esp_idf_svc::sys::{
esp,
esp_netif_flags_ESP_NETIF_FLAG_AUTOUP,
esp_netif_ip_event_type_ESP_NETIF_IP_EVENT_GOT_IP,
esp_netif_ip_event_type_ESP_NETIF_IP_EVENT_LOST_IP,
};

use crate::utils::nvs::WgConfig;

Expand All @@ -25,6 +34,8 @@ use esp_idf_svc::sys::wg::{
wireguard_config_t,
wireguard_ctx_t,
};
use esp_idf_svc::sys::esp_netif_get_netif_impl;
use esp_idf_svc::sys::wg::netif_default;

/// The maximum number of attempts to sync system time before declaring the call
/// to [sync_systime] a failure.
Expand Down Expand Up @@ -62,7 +73,7 @@ pub fn sync_systime() -> anyhow::Result<()> {
/// wrapping them in [`Box`].
fn create_ctx_conf(
nvs: Arc<Mutex<EspNvs<NvsDefault>>>,
) -> anyhow::Result<(*mut wireguard_ctx_t, *mut wireguard_config_t)> {
) -> anyhow::Result<(EspNetif, *mut wireguard_ctx_t)> {
let nvs_conf = WgConfig::get_config(nvs)?;

let config = Box::new(wireguard_config_t {
Expand All @@ -78,13 +89,35 @@ fn create_ctx_conf(
persistent_keepalive: 20,
});

let wg_netif = EspNetif::new_with_conf(&NetifConfiguration {
flags: esp_netif_flags_ESP_NETIF_FLAG_AUTOUP,
got_ip_event_id: NonZeroU32::new(esp_netif_ip_event_type_ESP_NETIF_IP_EVENT_GOT_IP),
lost_ip_event_id: NonZeroU32::new(esp_netif_ip_event_type_ESP_NETIF_IP_EVENT_LOST_IP),
key: "wg0".try_into().unwrap(),
description: "wg_if".try_into().unwrap(),
route_priority: 50,
ip_configuration: Some(esp_idf_svc::ipv4::Configuration::Client(
esp_idf_svc::ipv4::ClientConfiguration::Fixed(ClientSettings {
ip: Ipv4Addr::new(0, 0, 0, 0),
subnet: Subnet {
gateway: Ipv4Addr::new(0, 0, 0, 0),
mask: Mask(0),
},
dns: None,
secondary_dns: None,
}),
)),
stack: esp_idf_svc::netif::NetifStack::Eth,
custom_mac: None,
})?;

let ctx = Box::new(wireguard_ctx_t {
config: ptr::null_mut(),
netif: ptr::null_mut(),
netif_default: ptr::null_mut(),
config: Box::into_raw(config),
netif: unsafe { esp_netif_get_netif_impl(wg_netif.handle()) } as _,
netif_default: unsafe { netif_default },
});

Ok((Box::into_raw(ctx), Box::into_raw(config)))
Ok((wg_netif, Box::into_raw(ctx)))
}

/// Establishes a tunnel with the peer defined in the `nvs` configuration.
Expand All @@ -102,6 +135,8 @@ fn create_ctx_conf(
/// taken NEVER TO DROP this context as it would unvariably result in undefined
/// behavior or crash the program.
pub fn start_tunnel(nvs: Arc<Mutex<EspNvs<NvsDefault>>>) -> anyhow::Result<()> {


let mut guard = WG_CTX.lock().unwrap();

// Check if a tunnel is already in service, otherwise we will get either
Expand Down Expand Up @@ -204,18 +239,4 @@ pub fn end_tunnel() -> anyhow::Result<()> {
guard.reset();

Ok(())
}

#[allow(dead_code)]
pub fn netif_ip() -> anyhow::Result<Ipv4Addr> {
let guard = WG_CTX.lock().unwrap();

if !guard.is_set() {
log::error!("Attempted to get ip without prior connection!");
return Err(anyhow::anyhow!("No netif to get ip from."));
}

let raw_ip = unsafe { (*(*guard.0).netif).ip_addr.addr };

Ok(Ipv4Addr::from(raw_ip.to_be_bytes()))
}
}

0 comments on commit ba74ffa

Please sign in to comment.