diff options
Diffstat (limited to 'src/protocol/l2tp.rs')
| -rw-r--r-- | src/protocol/l2tp.rs | 340 |
1 files changed, 176 insertions, 164 deletions
diff --git a/src/protocol/l2tp.rs b/src/protocol/l2tp.rs index 0496f2b..90c5ea5 100644 --- a/src/protocol/l2tp.rs +++ b/src/protocol/l2tp.rs @@ -43,7 +43,7 @@ use nom::IResult; */ #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum AvpType { +pub enum L2TPAVPType { Message, Result, ProtocolVersion, @@ -138,26 +138,26 @@ pub enum AvpType { } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum L2tpType { +pub enum L2TPType { Data, Control, } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct AvpHeader { +pub struct L2TPAVPHeader { pub mandatory: bool, pub hidden: bool, pub reserved: u8, pub length: u16, pub vendor_id: u16, - pub attribute_type: AvpType, + pub attribute_type: L2TPAVPType, pub attribute_value: Vec<u8>, } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct L2tpHeader { +pub struct L2TPHeader { pub flags: u16, - pub flag_type: L2tpType, + pub flag_type: L2TPType, pub flag_length: bool, pub flag_sequence: bool, pub flag_offset: bool, @@ -171,121 +171,121 @@ pub struct L2tpHeader { pub nr: Option<u16>, pub offset_size: Option<u16>, pub offset_pad: Option<Vec<u8>>, - pub avps: Option<Vec<AvpHeader>>, + pub avps: Option<Vec<L2TPAVPHeader>>, } /****************************************************************************** * API ******************************************************************************/ -impl From<bool> for L2tpType { +impl From<bool> for L2TPType { fn from(raw: bool) -> Self { match raw { - false => L2tpType::Data, - true => L2tpType::Control, + false => L2TPType::Data, + true => L2TPType::Control, } } } -impl From<u16> for AvpType { +impl From<u16> for L2TPAVPType { fn from(raw: u16) -> Self { match raw { - 0 => AvpType::Message, - 1 => AvpType::Result, - 2 => AvpType::ProtocolVersion, - 3 => AvpType::FramingCapabilites, - 4 => AvpType::BearerCapabilites, - 5 => AvpType::TieBreaker, - 6 => AvpType::FirmwareRevision, - 7 => AvpType::HostName, - 8 => AvpType::VendorName, - 9 => AvpType::AssignedTunnelId, - 10 => AvpType::ReceiveWindowSize, - 11 => AvpType::Challenge, - 12 => AvpType::CauseCode, - 13 => AvpType::ChallengeResponse, - 14 => AvpType::AssignedSessionId, - 15 => AvpType::CallSerialNumber, - 16 => AvpType::MinimumBps, - 17 => AvpType::MaximumBps, - 18 => AvpType::BearerType, - 19 => AvpType::FramingType, - 20 => AvpType::CalledNumber, - 21 => AvpType::CallingNumber, - 22 => AvpType::SubAddress, - 23 => AvpType::TxConnectSpeed, - 24 => AvpType::PhysicalChannelId, - 25 => AvpType::InitialReceivedLcpConfReq, - 26 => AvpType::LastSentLcpConfReq, - 27 => AvpType::LastReceivedLcpConfReq, - 28 => AvpType::ProxyAuthenType, - 29 => AvpType::ProxyAuthenName, - 30 => AvpType::ProxyAuthenChallenge, - 31 => AvpType::ProxyAuthenID, - 32 => AvpType::ProxyAuthenResponse, - 33 => AvpType::CallErrors, - 34 => AvpType::AcctAuthenType, - 35 => AvpType::AcctAuthenName, - 36 => AvpType::AcctAuthenChallenge, - 37 => AvpType::AcctAuthenID, - 38 => AvpType::AcctAuthenResponse, - 39 => AvpType::ChallengeControl, - 40 => AvpType::AcctEapType, - 41 => AvpType::AcctEapId, - 42 => AvpType::AcctEapResponse, - 44 => AvpType::CalledStationId, - 45 => AvpType::CallingStationId, - 46 => AvpType::NasIdentifier, - 47 => AvpType::ProxyState, - 48 => AvpType::LoginLatService, - 49 => AvpType::LoginLatNode, - 50 => AvpType::LoginLatGroup, - 51 => AvpType::FramedAppleTalkLink, - 52 => AvpType::FramedAppleTalkNetwork, - 53 => AvpType::FramedAppleTalkZone, - 61 => AvpType::AcctStatusType, - 62 => AvpType::AcctDelayTime, - 63 => AvpType::AcctInputOctets, - 64 => AvpType::AcctOutputOctets, - 65 => AvpType::AcctSessionId, - 66 => AvpType::AcctAuthentic, - 67 => AvpType::AcctSessionTime, - 68 => AvpType::AcctInputPackets, - 69 => AvpType::AcctOutputPackets, - 70 => AvpType::AcctTerminateCause, - 71 => AvpType::AcctMultiSessionId, - 72 => AvpType::AcctLinkCount, - 73 => AvpType::AcctEventTimeStamp, - 74 => AvpType::AcctTunnelConnection, - 75 => AvpType::AcctTunnelPacketsLost, - 87 => AvpType::NasPortId, - 88 => AvpType::FramedInterfaceId, - 97 => AvpType::FramedIpv6Prefix, - 98 => AvpType::LoginIpv6Host, - 99 => AvpType::FramedIpv6Route, - 100 => AvpType::FramedIpv6Pool, - 101 => AvpType::ErrorCause, - 102 => AvpType::EapMessage, - 103 => AvpType::MessageAuthenticator, - 104 => AvpType::TunnelPassword, - 105 => AvpType::ArapPassword, - 106 => AvpType::ArapFeatures, - 107 => AvpType::ArapZoneAccess, - 108 => AvpType::ArapSecurity, - 109 => AvpType::ArapSecurityData, - 110 => AvpType::PasswordRetry, - 111 => AvpType::Prompt, - 112 => AvpType::ConnectInfo, - 113 => AvpType::ConfigurationToken, - 114 => AvpType::EapMessage2, - 115 => AvpType::MessageAuthenticator2, - 116 => AvpType::ArapChallenge, - other => AvpType::Other(other), + 0 => L2TPAVPType::Message, + 1 => L2TPAVPType::Result, + 2 => L2TPAVPType::ProtocolVersion, + 3 => L2TPAVPType::FramingCapabilites, + 4 => L2TPAVPType::BearerCapabilites, + 5 => L2TPAVPType::TieBreaker, + 6 => L2TPAVPType::FirmwareRevision, + 7 => L2TPAVPType::HostName, + 8 => L2TPAVPType::VendorName, + 9 => L2TPAVPType::AssignedTunnelId, + 10 => L2TPAVPType::ReceiveWindowSize, + 11 => L2TPAVPType::Challenge, + 12 => L2TPAVPType::CauseCode, + 13 => L2TPAVPType::ChallengeResponse, + 14 => L2TPAVPType::AssignedSessionId, + 15 => L2TPAVPType::CallSerialNumber, + 16 => L2TPAVPType::MinimumBps, + 17 => L2TPAVPType::MaximumBps, + 18 => L2TPAVPType::BearerType, + 19 => L2TPAVPType::FramingType, + 20 => L2TPAVPType::CalledNumber, + 21 => L2TPAVPType::CallingNumber, + 22 => L2TPAVPType::SubAddress, + 23 => L2TPAVPType::TxConnectSpeed, + 24 => L2TPAVPType::PhysicalChannelId, + 25 => L2TPAVPType::InitialReceivedLcpConfReq, + 26 => L2TPAVPType::LastSentLcpConfReq, + 27 => L2TPAVPType::LastReceivedLcpConfReq, + 28 => L2TPAVPType::ProxyAuthenType, + 29 => L2TPAVPType::ProxyAuthenName, + 30 => L2TPAVPType::ProxyAuthenChallenge, + 31 => L2TPAVPType::ProxyAuthenID, + 32 => L2TPAVPType::ProxyAuthenResponse, + 33 => L2TPAVPType::CallErrors, + 34 => L2TPAVPType::AcctAuthenType, + 35 => L2TPAVPType::AcctAuthenName, + 36 => L2TPAVPType::AcctAuthenChallenge, + 37 => L2TPAVPType::AcctAuthenID, + 38 => L2TPAVPType::AcctAuthenResponse, + 39 => L2TPAVPType::ChallengeControl, + 40 => L2TPAVPType::AcctEapType, + 41 => L2TPAVPType::AcctEapId, + 42 => L2TPAVPType::AcctEapResponse, + 44 => L2TPAVPType::CalledStationId, + 45 => L2TPAVPType::CallingStationId, + 46 => L2TPAVPType::NasIdentifier, + 47 => L2TPAVPType::ProxyState, + 48 => L2TPAVPType::LoginLatService, + 49 => L2TPAVPType::LoginLatNode, + 50 => L2TPAVPType::LoginLatGroup, + 51 => L2TPAVPType::FramedAppleTalkLink, + 52 => L2TPAVPType::FramedAppleTalkNetwork, + 53 => L2TPAVPType::FramedAppleTalkZone, + 61 => L2TPAVPType::AcctStatusType, + 62 => L2TPAVPType::AcctDelayTime, + 63 => L2TPAVPType::AcctInputOctets, + 64 => L2TPAVPType::AcctOutputOctets, + 65 => L2TPAVPType::AcctSessionId, + 66 => L2TPAVPType::AcctAuthentic, + 67 => L2TPAVPType::AcctSessionTime, + 68 => L2TPAVPType::AcctInputPackets, + 69 => L2TPAVPType::AcctOutputPackets, + 70 => L2TPAVPType::AcctTerminateCause, + 71 => L2TPAVPType::AcctMultiSessionId, + 72 => L2TPAVPType::AcctLinkCount, + 73 => L2TPAVPType::AcctEventTimeStamp, + 74 => L2TPAVPType::AcctTunnelConnection, + 75 => L2TPAVPType::AcctTunnelPacketsLost, + 87 => L2TPAVPType::NasPortId, + 88 => L2TPAVPType::FramedInterfaceId, + 97 => L2TPAVPType::FramedIpv6Prefix, + 98 => L2TPAVPType::LoginIpv6Host, + 99 => L2TPAVPType::FramedIpv6Route, + 100 => L2TPAVPType::FramedIpv6Pool, + 101 => L2TPAVPType::ErrorCause, + 102 => L2TPAVPType::EapMessage, + 103 => L2TPAVPType::MessageAuthenticator, + 104 => L2TPAVPType::TunnelPassword, + 105 => L2TPAVPType::ArapPassword, + 106 => L2TPAVPType::ArapFeatures, + 107 => L2TPAVPType::ArapZoneAccess, + 108 => L2TPAVPType::ArapSecurity, + 109 => L2TPAVPType::ArapSecurityData, + 110 => L2TPAVPType::PasswordRetry, + 111 => L2TPAVPType::Prompt, + 112 => L2TPAVPType::ConnectInfo, + 113 => L2TPAVPType::ConfigurationToken, + 114 => L2TPAVPType::EapMessage2, + 115 => L2TPAVPType::MessageAuthenticator2, + 116 => L2TPAVPType::ArapChallenge, + other => L2TPAVPType::Other(other), } } } -fn avp_decode(input: &[u8]) -> IResult<&[u8], AvpHeader> { +fn avp_decode(input: &[u8]) -> IResult<&[u8], L2TPAVPHeader> { let (input, (mandatory, hidden, reserved, length)): (&[u8], (u8, u8, u8, u16)) = bits::bits::<_, _, Error<_>, _, _>(sequence::tuple(( bits::streaming::take(1u8), @@ -310,7 +310,7 @@ fn avp_decode(input: &[u8]) -> IResult<&[u8], AvpHeader> { Ok(( input, - AvpHeader { + L2TPAVPHeader { mandatory: mandatory != 0, hidden: hidden != 0, reserved, @@ -322,9 +322,9 @@ fn avp_decode(input: &[u8]) -> IResult<&[u8], AvpHeader> { )) } -impl Decode for L2tpHeader { - type Iterm = L2tpHeader; - fn decode(input: &[u8]) -> IResult<&[u8], L2tpHeader> { +impl Decode for L2TPHeader { + type Iterm = L2TPHeader; + fn decode(input: &[u8]) -> IResult<&[u8], L2TPHeader> { let (input, (flags, ver)) = bits::bits::<_, _, Error<_>, _, _>(sequence::tuple(( bits::streaming::take(12u8), bits::streaming::take(4u8), @@ -367,8 +367,8 @@ impl Decode for L2tpHeader { false => (input, None), }; let (input, avps) = match flag_type { - L2tpType::Data => (input, None), - L2tpType::Control => { + L2TPType::Data => (input, None), + L2TPType::Control => { let mut avps = Vec::new(); let mut input = input; loop { @@ -385,7 +385,7 @@ impl Decode for L2tpHeader { Ok(( input, - L2tpHeader { + L2TPHeader { flags, flag_type, flag_length, @@ -412,10 +412,10 @@ impl Decode for L2tpHeader { #[cfg(test)] mod tests { - use super::AvpHeader; - use super::AvpType; - use super::L2tpHeader; - use super::L2tpType; + use super::L2TPAVPHeader; + use super::L2TPAVPType; + use super::L2TPHeader; + use super::L2TPType; use crate::protocol::codec::Decode; use std::vec; const EMPTY_SLICE: &'static [u8] = &[]; @@ -442,9 +442,9 @@ mod tests { 0x40, 0x02, 0x00, 0x4e, 0x71, 0x46, 0x00, 0x02, 0xff, /* Payload */ ]; - let expectation = L2tpHeader { + let expectation = L2TPHeader { flags: 0x4002 >> 4, - flag_type: L2tpType::Data, + flag_type: L2TPType::Data, flag_length: true, flag_sequence: false, flag_offset: false, @@ -460,14 +460,17 @@ mod tests { avps: None, }; - assert_eq!(L2tpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(L2TPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = L2tpHeader::decode(&bytes); - if let Ok((payload, header)) = result { - println!("return: {:?}, payload: {}", header, payload.len()); - } else { - println!("return: Incomplete data"); + let result = L2TPHeader::decode(&bytes); + match result { + Ok((payload, header)) => { + println!("OK: {:?}, payload: {}", header, payload.len()); + } + Err(e) => { + println!("ERR: {:?}", e); + } } // assert_eq!(1, 0); @@ -491,9 +494,9 @@ mod tests { let bytes = [0x00, 0x02, 0x00, 0x0d, 0x00, 0x01, 0xff /* Payload */]; - let expectation = L2tpHeader { + let expectation = L2TPHeader { flags: 0x0002 >> 4, - flag_type: L2tpType::Data, + flag_type: L2TPType::Data, flag_length: false, flag_sequence: false, flag_offset: false, @@ -509,14 +512,17 @@ mod tests { avps: None, }; - assert_eq!(L2tpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(L2TPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = L2tpHeader::decode(&bytes); - if let Ok((payload, header)) = result { - println!("return: {:?}, payload: {}", header, payload.len()); - } else { - println!("return: Incomplete data"); + let result = L2TPHeader::decode(&bytes); + match result { + Ok((payload, header)) => { + println!("OK: {:?}, payload: {}", header, payload.len()); + } + Err(e) => { + println!("ERR: {:?}", e); + } } // assert_eq!(1, 0); @@ -543,9 +549,9 @@ mod tests { 0x02, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = L2tpHeader { + let expectation = L2TPHeader { flags: 0x0202 >> 4, - flag_type: L2tpType::Data, + flag_type: L2TPType::Data, flag_length: false, flag_sequence: false, flag_offset: true, @@ -561,14 +567,17 @@ mod tests { avps: None, }; - assert_eq!(L2tpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(L2TPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = L2tpHeader::decode(&bytes); - if let Ok((payload, header)) = result { - println!("return: {:?}, payload: {}", header, payload.len()); - } else { - println!("return: Incomplete data"); + let result = L2TPHeader::decode(&bytes); + match result { + Ok((payload, header)) => { + println!("OK: {:?}, payload: {}", header, payload.len()); + } + Err(e) => { + println!("ERR: {:?}", e); + } } // assert_eq!(1, 0); @@ -670,9 +679,9 @@ mod tests { 0x08, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x08, ]; - let expectation = L2tpHeader { + let expectation = L2TPHeader { flags: 0xc802 >> 4, - flag_type: L2tpType::Control, + flag_type: L2TPType::Control, flag_length: true, flag_sequence: true, flag_offset: false, @@ -686,100 +695,103 @@ mod tests { offset_size: None, offset_pad: None, avps: Some(vec![ - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::Message, + attribute_type: L2TPAVPType::Message, attribute_value: vec![0x00, 0x01], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::ProtocolVersion, + attribute_type: L2TPAVPType::ProtocolVersion, attribute_value: vec![0x01, 0x00], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 10, vendor_id: 0, - attribute_type: AvpType::FramingCapabilites, + attribute_type: L2TPAVPType::FramingCapabilites, attribute_value: vec![0x00, 0x00, 0x00, 0x01], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 10, vendor_id: 0, - attribute_type: AvpType::BearerCapabilites, + attribute_type: L2TPAVPType::BearerCapabilites, attribute_value: vec![0x00, 0x00, 0x00, 0x00], }, - AvpHeader { + L2TPAVPHeader { mandatory: false, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::FirmwareRevision, + attribute_type: L2TPAVPType::FirmwareRevision, attribute_value: vec![0x06, 0x01], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 18, vendor_id: 0, - attribute_type: AvpType::HostName, + attribute_type: L2TPAVPType::HostName, attribute_value: vec![ 0x49, 0x49, 0x45, 0x2d, 0x53, 0x4d, 0x2d, 0x54, 0x48, 0x49, 0x4e, 0x4b, ], }, - AvpHeader { + L2TPAVPHeader { mandatory: false, hidden: false, reserved: 0, length: 15, vendor_id: 0, - attribute_type: AvpType::VendorName, + attribute_type: L2TPAVPType::VendorName, attribute_value: vec![0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::AssignedTunnelId, + attribute_type: L2TPAVPType::AssignedTunnelId, attribute_value: vec![0x00, 0x01], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::ReceiveWindowSize, + attribute_type: L2TPAVPType::ReceiveWindowSize, attribute_value: vec![0x00, 0x08], }, ]), }; - assert_eq!(L2tpHeader::decode(&bytes), Ok((EMPTY_SLICE, expectation))); + assert_eq!(L2TPHeader::decode(&bytes), Ok((EMPTY_SLICE, expectation))); // example - let result = L2tpHeader::decode(&bytes); - if let Ok((payload, header)) = result { - println!("return: {:?}, payload: {}", header, payload.len()); - } else { - println!("return: Incomplete data"); + let result = L2TPHeader::decode(&bytes); + match result { + Ok((payload, header)) => { + println!("OK: {:?}, payload: {}", header, payload.len()); + } + Err(e) => { + println!("ERR: {:?}", e); + } } // assert_eq!(1, 0); |
