Skip to content

Commit

Permalink
Deauthentication frame parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
szykam committed Oct 19, 2024
1 parent bb8584b commit 402daba
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
8 changes: 8 additions & 0 deletions libwifi/src/frame/management/deauthentication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::frame::components::{MacAddress, ManagementHeader};
use libwifi_macros::AddressHeader;

#[derive(Clone, Debug, AddressHeader)]
pub struct Deauthentication {
pub header: ManagementHeader,
pub reason_code: u16,
}
2 changes: 2 additions & 0 deletions libwifi/src/frame/management/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod association;
mod beacon;
mod probe;
mod deauthentication;

pub use association::{AssociationRequest, AssociationResponse};
pub use deauthentication::{Deauthentication};
pub use beacon::Beacon;
pub use probe::{ProbeRequest, ProbeResponse};
1 change: 1 addition & 0 deletions libwifi/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum Frame {
ProbeResponse(ProbeResponse),
AssociationRequest(AssociationRequest),
AssociationResponse(AssociationResponse),
Deauthentication(Deauthentication),

// Control Frames
Rts(Rts),
Expand Down
1 change: 1 addition & 0 deletions libwifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn parse_frame(input: &[u8]) -> Result<Frame, Error> {
FrameSubType::ProbeResponse => parse_probe_response(frame_control, input),
FrameSubType::AssociationRequest => parse_association_request(frame_control, input),
FrameSubType::AssociationResponse => parse_association_response(frame_control, input),
FrameSubType::Deauthentication => parse_deauthentication(frame_control, input),

// Control
FrameSubType::Rts => parse_rts(frame_control, input),
Expand Down
18 changes: 17 additions & 1 deletion libwifi/src/parsers/frame_types/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nom::sequence::tuple;
use crate::error::Error;
use crate::frame::components::FrameControl;
use crate::frame::*;
use crate::parsers::{parse_management_header, parse_station_info};
use crate::parsers::{clone_slice, parse_management_header, parse_station_info};

/// Parse an [AssociationRequest] frame.
///
Expand Down Expand Up @@ -110,3 +110,19 @@ pub fn parse_probe_response(frame_control: FrameControl, input: &[u8]) -> Result
station_info,
}))
}

/// Parse a [Deauthentication] frame.
///
/// The general structure is:
/// - ManagementHeader
/// - Reason code
pub fn parse_deauthentication(frame_control: FrameControl, input: &[u8]) -> Result<Frame, Error> {
let (input, header) = parse_management_header(frame_control, input)?;
let reason_code_bytes = clone_slice::<2>(input);
let reason_code = u16::from_le_bytes(reason_code_bytes);

Ok(Frame::Deauthentication(Deauthentication {
header,
reason_code,
}))
}
10 changes: 9 additions & 1 deletion libwifi/tests/management_frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,20 @@ fn test_authentication() {

#[test]
fn test_deauthentication() {
let _payload = [
let payload = [
192, 0, // FrameControl
58, 1, // Duration id
248, 50, 228, 173, 71, 184, // First Address
192, 238, 251, 75, 207, 58, // Second Address
248, 50, 228, 173, 71, 184, // Third Address
224, 146, 3, 0,
];

let frame = parse_frame(&payload).expect("Payload should be valid");
println!("{frame:?}");
assert!(matches!(frame, Frame::Deauthentication(_)));

if let Frame::Deauthentication(deauthentication) = frame {
assert_eq!(3, deauthentication.reason_code);
}
}

0 comments on commit 402daba

Please sign in to comment.