From 6178e15abe1d13899baaefa7c5252792dc2b01b8 Mon Sep 17 00:00:00 2001 From: HimbeerserverDE Date: Sat, 29 Jul 2023 14:44:08 +0200 Subject: high level termination logic --- src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/state.rs | 2 ++ 2 files changed, 63 insertions(+) diff --git a/src/main.rs b/src/main.rs index ad5c94e..acc788e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use socket2::Socket; const PPPOE_UPLINK: &str = "eth1"; const MAX_ATTEMPTS: usize = 10; +const MAX_STATUS_ATTEMPTS: usize = 2; static PPPOE_XMIT_INTERVAL: Duration = Duration::from_secs(3); static SESSION_INIT_GRACE_PERIOD: Duration = Duration::from_secs(1); @@ -359,6 +360,50 @@ fn session( } Ppp::Auth(_) => {} Ppp::Active => {} + Ppp::Terminate(ref reason, attempt) => { + if attempt >= MAX_ATTEMPTS { + *ppp_state = Ppp::Terminate2( + String::from_utf8(reason.clone()).unwrap_or(String::new()), + ); + continue; + } + + PppPkt::new_lcp(LcpPkt::new_terminate_request( + rand::random(), + reason.clone(), + )) + .serialize(&mut ctl_w)?; + ctl_w.flush()?; + + let reason_pretty = + String::from_utf8(reason.clone()).unwrap_or(format!("{:?}", reason)); + + println!( + " -> lcp terminate-request {}/{}, reason: {}", + attempt, MAX_STATUS_ATTEMPTS, reason_pretty + ); + + *ppp_state = Ppp::Terminate(reason.clone(), attempt + 1); + } + Ppp::Terminate2(ref reason) => { + PppoePkt::new_padt( + remote_mac, + local_mac, + session_id, + vec![PppoeVal::GenericError(reason.clone()).into()], + ) + .serialize(&mut sock_disc_w)?; + sock_disc_w.flush()?; + + println!( + " -> [{}] padt, session id: {}, reason: {}", + remote_mac, session_id, reason + ); + + *ppp_state = Ppp::Terminated; + *pppoe_state.lock().expect("pppoe state mutex is poisoned") = Pppoe::Init; + break; + } Ppp::Terminated => { break; } @@ -596,6 +641,22 @@ fn handle_lcp(lcp: LcpPkt, ctl_w: &mut BufWriter, state: Arc>) Ok(()) } + LcpData::TerminateAck(..) => { + let mut state = state.lock().expect("ppp state mutex is poisoned"); + match *state { + Ppp::Terminate(ref reason, ..) => { + *state = + Ppp::Terminate2(String::from_utf8(reason.clone()).unwrap_or(String::new())) + } + _ => { + println!(" <- unexpected lcp terminate-ack {}", lcp.identifier); + return Ok(()); + } + } + + println!(" <- lcp terminate-ack {}", lcp.identifier); + Ok(()) + } _ => Ok(()), } } diff --git a/src/state.rs b/src/state.rs index a88f2c9..8f41d9c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -21,6 +21,8 @@ pub enum Ppp { SyncAcked(usize), Auth(Option), Active, + Terminate(Vec, usize), + Terminate2(String), Terminated, Err, } -- cgit v1.2.3