Commit 5187576e authored by eonm's avatar eonm
Browse files

Add initial files

parents
/target
**/*.rs.bk
Cargo.lock
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at eonm.dev@gmail.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq
[package]
name = "nl80211"
version = "0.0.1"
authors = ["eonm <eon.mathis@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
buffering = { version = "0.3.2", features = ["copy"] }
neli = "0.4.3-r1"
hex = "*"
MIT License
Copyright (c) 2019 Eonm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# nl80211
This diff is collapsed.
use crate::attr::Nl80211Attr;
use crate::attr::Nl80211Bss;
use crate::nl80211traits::ParseNlAttr;
use crate::nl80211traits::PrettyFormat;
use crate::parse_attr::{parse_hex, parse_i32, parse_u16, parse_u32};
use neli::nlattr::AttrHandle;
/// A struct representing a BSS (Basic Service Set)
#[derive(Debug, Clone, PartialEq)]
pub struct Bss {
pub bssid: Option<Vec<u8>>,
/// Frequency in MHz (u32)
pub frequency: Option<Vec<u8>>,
/// Beacon interval of the (I)BSS (u16)
pub beacon_interval: Option<Vec<u8>>,
/// Age of this BSS entry in ms (u32)
pub seen_ms_ago: Option<Vec<u8>>,
/// Status, if this BSS is "used" (u8)
pub status: Option<Vec<u8>>,
/// Signal strength of probe response/beacon in mBm (100 * dBm) (i32)
pub signal: Option<Vec<u8>>,
}
impl Bss {
pub fn default() -> Bss {
Bss {
bssid: None,
frequency: None,
beacon_interval: None,
seen_ms_ago: None,
status: None,
signal: None,
}
}
}
impl PrettyFormat for Bss {
fn pretty_format(&self) -> String {
let mut result = Vec::new();
if let Some(bssid) = &self.bssid {
result.push(format!("bssid : {}", parse_hex(bssid)))
};
if let Some(frequency) = &self.frequency {
result.push(format!(
"frequency : {} Ghz",
parse_u32(frequency) as f32 / 1000.00
))
};
if let Some(beacon_interval) = &self.beacon_interval {
result.push(format!(
"beacon interval : {} TUs",
parse_u16(beacon_interval)
))
};
if let Some(seen_ms_ago) = &self.seen_ms_ago {
result.push(format!("last seen : {} ms", parse_u32(seen_ms_ago)))
};
if let Some(status) = &self.status {
result.push(format!("status : {}", parse_u32(status)))
};
if let Some(signal) = &self.signal {
result.push(format!(
"signal : {:?} dBm",
parse_i32(signal) as f32 / 100.00
))
};
result.join("\n")
}
}
impl ParseNlAttr for Bss {
/// Parse netlink messages returned by the nl80211 command CmdGetScan
fn parse(&mut self, handle: AttrHandle<Nl80211Attr>) -> Bss {
for attr in handle.iter() {
println!("{:?}", attr);
match attr.nla_type {
Nl80211Attr::AttrBss => {
let sub_handle = attr.get_nested_attributes::<Nl80211Bss>().unwrap();
for sub_attr in sub_handle.iter() {
match sub_attr.nla_type {
Nl80211Bss::BssBeaconInterval => {
self.beacon_interval = Some(sub_attr.payload.clone())
}
Nl80211Bss::BssFrequency => {
self.frequency = Some(sub_attr.payload.clone())
}
Nl80211Bss::BssSeenMsAgo => {
self.seen_ms_ago = Some(sub_attr.payload.clone())
}
Nl80211Bss::BssStatus => self.status = Some(sub_attr.payload.clone()),
Nl80211Bss::BssBssid => self.bssid = Some(sub_attr.payload.clone()),
Nl80211Bss::BssSignalMbm => {
self.signal = Some(sub_attr.payload.clone())
}
_ => (),
}
}
}
_ => (),
}
}
self.to_owned()
}
}
#[cfg(test)]
mod test_bss {
use super::*;
use crate::attr::Nl80211Attr::*;
use neli::nlattr::Nlattr;
#[test]
fn test_pretty_format() {
let bss = Bss {
bssid: Some(vec![255, 255, 255, 255, 255, 255]),
frequency: Some(vec![108, 9, 0, 0]),
beacon_interval: Some(vec![100, 0]),
seen_ms_ago: Some(vec![100, 0, 0, 0]),
status: Some(vec![1, 0, 0, 0]),
signal: Some(vec![76, 235, 255, 255]),
};
let expected_output = r#"bssid : FF:FF:FF:FF:FF:FF
frequency : 2.412 Ghz
beacon interval : 100 TUs
last seen : 100 ms
status : 1
signal : -53.0 dBm"#;
assert_eq!(
bss.pretty_format(),
expected_output.replace("\n ", "\n")
)
}
#[test]
fn test_parse() {
let handler = vec![
Nlattr {
nla_len: 8,
nla_type: AttrGeneration,
payload: vec![28, 4, 0, 0],
},
Nlattr {
nla_len: 8,
nla_type: AttrIfindex,
payload: vec![3, 0, 0, 0],
},
Nlattr {
nla_len: 12,
nla_type: AttrWdev,
payload: vec![1, 0, 0, 0, 0, 0, 0, 0],
},
Nlattr {
nla_len: 728,
nla_type: AttrBss,
payload: vec![
10, 0, 1, 0, 255, 255, 255, 255, 255, 255, 0, 0, 4, 0, 14, 0, 12, 0, 3, 0, 132,
12, 93, 163, 39, 0, 0, 0, 95, 1, 6, 0, 0, 8, 83, 70, 82, 45, 49, 99, 50, 56, 1,
8, 130, 132, 139, 150, 36, 48, 72, 108, 3, 1, 1, 7, 6, 68, 69, 32, 1, 13, 20,
32, 1, 0, 35, 2, 16, 0, 42, 1, 0, 50, 4, 12, 18, 24, 96, 48, 24, 1, 0, 0, 15,
172, 2, 2, 0, 0, 15, 172, 4, 0, 15, 172, 2, 1, 0, 0, 15, 172, 2, 12, 0, 11, 5,
1, 0, 80, 0, 0, 70, 5, 114, 8, 1, 0, 0, 45, 26, 188, 9, 27, 255, 255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61, 22, 1, 8, 4, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 8, 4, 0, 8, 0, 0, 0, 0,
64, 221, 131, 0, 80, 242, 4, 16, 74, 0, 1, 16, 16, 68, 0, 1, 2, 16, 59, 0, 1,
3, 16, 71, 0, 16, 65, 133, 194, 155, 156, 12, 135, 126, 154, 135, 125, 82, 84,
30, 42, 138, 16, 33, 0, 8, 83, 97, 103, 101, 109, 99, 111, 109, 16, 35, 0, 8,
83, 97, 103, 101, 109, 99, 111, 109, 16, 36, 0, 6, 49, 50, 51, 52, 53, 54, 16,
66, 0, 7, 48, 48, 48, 48, 48, 48, 49, 16, 84, 0, 8, 0, 6, 0, 80, 242, 4, 0, 1,
16, 17, 0, 10, 83, 97, 103, 101, 109, 99, 111, 109, 65, 80, 16, 8, 0, 2, 32, 8,
16, 60, 0, 1, 3, 16, 73, 0, 6, 0, 55, 42, 0, 1, 32, 221, 9, 0, 16, 24, 2, 1, 0,
12, 0, 0, 221, 26, 0, 80, 242, 1, 1, 0, 0, 80, 242, 2, 2, 0, 0, 80, 242, 4, 0,
80, 242, 2, 1, 0, 0, 80, 242, 2, 221, 24, 0, 80, 242, 2, 1, 1, 132, 0, 3, 164,
0, 0, 39, 164, 0, 0, 66, 67, 94, 0, 98, 50, 47, 0, 0, 12, 0, 13, 0, 187, 118,
116, 163, 39, 0, 0, 0, 19, 1, 11, 0, 0, 8, 83, 70, 82, 45, 49, 99, 50, 56, 1,
8, 130, 132, 139, 150, 36, 48, 72, 108, 3, 1, 1, 5, 4, 0, 1, 0, 0, 7, 6, 68,
69, 32, 1, 13, 20, 32, 1, 0, 35, 2, 16, 0, 42, 1, 0, 50, 4, 12, 18, 24, 96, 48,
24, 1, 0, 0, 15, 172, 2, 2, 0, 0, 15, 172, 4, 0, 15, 172, 2, 1, 0, 0, 15, 172,
2, 12, 0, 11, 5, 1, 0, 80, 0, 0, 70, 5, 114, 8, 1, 0, 0, 45, 26, 188, 9, 27,
255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61,
22, 1, 8, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 8,
4, 0, 8, 0, 0, 0, 0, 64, 221, 49, 0, 80, 242, 4, 16, 74, 0, 1, 16, 16, 68, 0,
1, 2, 16, 71, 0, 16, 65, 133, 194, 155, 156, 12, 135, 126, 154, 135, 125, 82,
84, 30, 42, 138, 16, 60, 0, 1, 3, 16, 73, 0, 6, 0, 55, 42, 0, 1, 32, 221, 9, 0,
16, 24, 2, 1, 0, 12, 0, 0, 221, 26, 0, 80, 242, 1, 1, 0, 0, 80, 242, 2, 2, 0,
0, 80, 242, 4, 0, 80, 242, 2, 1, 0, 0, 80, 242, 2, 221, 24, 0, 80, 242, 2, 1,
1, 132, 0, 3, 164, 0, 0, 39, 164, 0, 0, 66, 67, 94, 0, 98, 50, 47, 0, 0, 6, 0,
4, 0, 100, 0, 0, 0, 6, 0, 5, 0, 17, 21, 0, 0, 8, 0, 2, 0, 108, 9, 0, 0, 8, 0,
12, 0, 0, 0, 0, 0, 8, 0, 10, 0, 100, 0, 0, 0, 8, 0, 7, 0, 76, 235, 255, 255, 8,
0, 9, 0, 1, 0, 0, 0,
],
},
];
let bss = Bss::default().parse(neli::nlattr::AttrHandle::Owned(handler));
let expected_bss = Bss {
bssid: Some(vec![255, 255, 255, 255, 255, 255]),
frequency: Some(vec![108, 9, 0, 0]),
beacon_interval: Some(vec![100, 0]),
seen_ms_ago: Some(vec![100, 0, 0, 0]),
status: Some(vec![1, 0, 0, 0]),
signal: Some(vec![76, 235, 255, 255]),
};
assert_eq!(bss, expected_bss)
}
}
use neli::consts::Cmd;
use neli::{impl_var, impl_var_base, impl_var_trait};
// https://github.com/mdlayher/wifi/blob/b1436901ddee2ea3ee8782a440a084e457615766/internal/nl80211/const.go
impl_var_trait!(
/// nl80211Commands
///
/// Enumeration from nl80211/nl80211.h:880
Nl80211Cmd, u8, Cmd,
CmdUnspec => 0,
CmdGetWiphy => 1,
CmdSetWiphy => 2,
CmdNewWiphy => 3,
CmdDelWiphy => 4,
CmdGetInterface => 5,
CmdSetInterface => 6,
CmdNewInterface => 7,
CmdDelInterface => 8,
CmdGetKey => 9,
CmdSetKey => 10,
CmdNewKey => 11,
CmdDelKey => 12,
CmdGetBeacon => 13,
CmdSetBeacon => 14,
CmdStartAp => 15,
CmdNewBeacon => 15,//CmdStartAp,
CmdStopAp => 16,
CmdDelBeacon => 16,//CmdStopAp,
CmdGetStation => 17,
CmdSetStation => 18,
CmdNewStation => 19,
CmdDelStation => 20,
CmdGetMpath => 21,
CmdSetMpath => 22,
CmdNewMpath => 23,
CmdDelMpath => 24,
CmdSetBss => 25,
CmdSetReg => 26,
CmdReqSetReg => 27,
CmdGetMeshConfig => 28,
CmdSetMeshConfig => 29,
CmdSetMgmtExtraIe => 30,
CmdGetReg => 31,
CmdGetScan => 32,
CmdTriggerScan => 33,
CmdNewScanResults => 34,
CmdScanAborted => 35,
CmdRegChange => 36,
CmdAuthenticate => 37,
CmdAssociate => 38,
CmdDeauthenticate => 39,
CmdDisassociate => 40,
CmdMichaelMicFailure => 41,
CmdRegBeaconHint => 42,
CmdJoinIbss => 43,
CmdLeaveIbss => 44,
CmdTestmode => 45,
CmdConnect => 46,
CmdRoam => 47,
CmdDisconnect => 48,
CmdSetWiphyNetns => 49,
CmdGetSurvey => 50,
CmdNewSurveyResults => 51,
CmdSetPmksa => 52,
CmdDelPmksa => 53,
CmdFlushPmksa => 54,
CmdRemainOnChannel => 55,
CmdCancelRemainOnChannel => 56,
CmdSetTxBitrateMask => 57,
CmdRegisterFrame => 58,
CmdRegisterAction => 58,//CmdRegisterFrame,
CmdFrame => 59,
CmdAction => 59,//CmdFrame,
CmdFrameTxStatus => 60,
CmdActionTxStatus => 60,//CmdFrameTxStatus,
CmdSetPowerSave => 61,
CmdGetPowerSave => 62,
CmdSetCqm => 63,
CmdNotifyCqm => 64,
CmdSetChannel => 65,
CmdSetWdsPeer => 66,
CmdFrameWaitCancel => 67,
CmdJoinMesh => 68,
CmdLeaveMesh => 69,
CmdUnprotDeauthenticate => 70,
CmdUnprotDisassociate => 71,
CmdNewPeerCandidate => 72,
CmdGetWowlan => 73,
CmdSetWowlan => 74,
CmdStartSchedScan => 75,
CmdStopSchedScan => 76,
CmdSchedScanResults => 77,
CmdSchedScanStopped => 78,
CmdSetRekeyOffload => 79,
CmdPmksaCandidate => 80,
CmdTdlsOper => 81,
CmdTdlsMgmt => 82,
CmdUnexpectedFrame => 83,
CmdProbeClient => 84,
CmdRegisterBeacons => 85,
CmdUnexpected4addrFrame => 86,
CmdSetNoackMap => 87,
CmdChSwitchNotify => 88,
CmdStartP2pDevice => 89,
CmdStopP2pDevice => 90,
CmdConnFailed => 91,
CmdSetMcastRate => 92,
CmdSetMacAcl => 93,
CmdRadarDetect => 94,
CmdGetProtocolFeatures => 95,
CmdUpdateFtIes => 96,
CmdFtEvent => 97,
CmdCritProtocolStart => 98,
CmdCritProtocolStop => 99,
CmdGetCoalesce => 100,
CmdSetCoalesce => 101,
CmdChannelSwitch => 102,
CmdVendor => 103,
CmdSetQosMap => 104,
CmdAddTxTs => 105,
CmdDelTxTs => 106,
CmdGetMpp => 107,
CmdJoinOcb => 108,
CmdLeaveOcb => 109,
CmdChSwitchStartedNotify => 110,
CmdTdlsChannelSwitch => 111,
CmdTdlsCancelChannelSwitch => 112,
CmdWiphyRegChange => 113,
CmdAbortScan => 114,
CmdStartNan => 115,
CmdStopNan => 116,
CmdAddNanFunction => 117,
CmdDelNanFunction => 118,
CmdChangeNanConfig => 119,
CmdNanMatch => 120,
CmdAfterLast => 121,
CmdMax => 120
);
pub const NL_80211_GENL_NAME: &str = "nl80211";
pub const NL_80211_GENL_VERSION: u8 = 1;
use crate::attr::*;
use crate::nl80211traits::ParseNlAttr;
use crate::nl80211traits::PrettyFormat;
use crate::parse_attr::parse_u32;
use crate::parse_attr::parse_u64;
use crate::socket::Socket;
use crate::station::Station;
use neli::nlattr::AttrHandle;
use crate::parse_attr::{parse_hex, parse_string};
/// A struct representing a wifi interface
#[derive(Clone, Debug, PartialEq)]
pub struct Interface {
/// A netlink interface index. This index is used to fetch extra information with nl80211
pub index: Option<Vec<u8>>,
/// Interface essid
pub ssid: Option<Vec<u8>>,
/// Interface MAC address
pub mac: Option<Vec<u8>>,
/// Interface name (u8, String)
pub name: Option<Vec<u8>>,
/// Interface frequency of the selected channel (u32, MHz)
pub frequency: Option<Vec<u8>>,
/// Interface chanel
pub channel: Option<Vec<u8>>,
/// Interface transmit power level in signed mBm units.
pub power: Option<Vec<u8>>,
/// index of wiphy to operate on, cf. /sys/class/ieee80211/<phyname>/index
pub phy: Option<Vec<u8>>,
/// Wireless device identifier, used for pseudo-devices that don't have a netdev (u64)
pub device: Option<Vec<u8>>,
}
impl Interface {
pub fn default() -> Interface {
Interface {
index: None,
ssid: None,
mac: None,
name: None,
frequency: None,
channel: None,
power: None,
phy: None,
device: None,
}
}
/// Get station info for this interface
pub fn get_station_info(&self) -> Result<Station, neli::err::NlError> {
if let Some(index) = &self.index {
Socket::connect()?.get_station_info(index)
} else {
Err(neli::err::NlError::new("Invalid interface index {:?}"))
}
}
}
impl ParseNlAttr for Interface {
/// Parse netlink messages returned by the nl80211 command CmdGetInterface
fn parse(&mut self, handle: AttrHandle<Nl80211Attr>) -> Interface {
for attr in handle.iter() {
match attr.nla_type {
Nl80211Attr::AttrIfindex => {
self.index = Some(attr.payload.clone());
}
Nl80211Attr::AttrSsid => {
self.ssid = Some(attr.payload.clone());
}
Nl80211Attr::AttrMac => {
self.mac = Some(attr.payload.clone());
}
Nl80211Attr::AttrIfname => {
self.name = Some(attr.payload.clone());
}
Nl80211Attr::AttrWiphyFreq => self.frequency = Some(attr.payload.clone()),
Nl80211Attr::AttrChannelWidth => self.channel = Some(attr.payload.clone()),
Nl80211Attr::AttrWiphyTxPowerLevel => self.power = Some(attr.payload.clone()),
Nl80211Attr::AttrWiphy => self.phy = Some(attr.payload.clone()),
Nl80211Attr::AttrWdev => self.device = Some(attr.payload.clone()),
_ => (),
}
}
self.to_owned()
}
}
impl PrettyFormat for Interface {
fn pretty_format(&self) -> String {
let mut result = Vec::new();
if let Some(ssid) = &self.ssid {
result.push(format!("essid : {}", parse_string(&ssid)))
};
if let Some(mac) = &self.mac {
result.push(format!("mac : {}", parse_hex(&mac)))
};
if let Some(name) = &self.name {
result.push(format!("interface : {}", parse_string(&name)))
};
if let Some(frequency) = &self.frequency {
result.push(format!(
"frequency : {} Ghz",
parse_u32(frequency) as f64 / 1000.00
))
};
if let Some(chanel) = &self.channel {
result.push(format!("channel : {}", parse_u32(chanel)))
};
if let Some(power) = &self.power {
result.push(format!("power : {} dBm", parse_u32(power) / 100))
};
if let Some(phy) = &self.phy {
result.push(format!("phy : {}", parse_u32(phy)))