aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBailey Kuo <formosa@google.com>2024-01-03 14:31:56 -0800
committerBailey Kuo <formosa@google.com>2024-01-03 14:31:56 -0800
commit7265d2ed3e7b58cc70a2b4fb7c4f86f52ba8ce7c (patch)
tree07bdd1557063fd0c5d3d8ba3f085adb0fde2da1c
parentb65f5bd907d189a90fd1aa355ae8e5820c1bb3d9 (diff)
downloadnetsim-7265d2ed3e7b58cc70a2b4fb7c4f86f52ba8ce7c.tar.gz
Support identifying mdns packet
- Use pdl to check the destination address Bug: 308815804 Test: ninja Change-Id: Ic5b6606fed44a2c892c83b7c98d58378b1a93c1b
-rw-r--r--rust/daemon/src/wifi/ieee80211.rs21
-rw-r--r--rust/daemon/src/wifi/medium.rs24
-rw-r--r--rust/daemon/src/wifi/test_packets/hwsim_cmd_frame_mdns.csv1
3 files changed, 36 insertions, 10 deletions
diff --git a/rust/daemon/src/wifi/ieee80211.rs b/rust/daemon/src/wifi/ieee80211.rs
index 54f5301..64bc61b 100644
--- a/rust/daemon/src/wifi/ieee80211.rs
+++ b/rust/daemon/src/wifi/ieee80211.rs
@@ -20,6 +20,7 @@ include!(concat!(env!("OUT_DIR"), "/ieee80211_packets.rs"));
/// A Ieee80211 MAC address
+// TODO: Add unit tests.
impl fmt::Display for MacAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bytes = u64::to_le_bytes(self.0);
@@ -56,6 +57,13 @@ impl From<MacAddress> for [u8; 6] {
}
}
+impl MacAddress {
+ pub fn is_multicast(&self) -> bool {
+ let addr = u64::to_le_bytes(self.0);
+ addr[0] == 0x1
+ }
+}
+
impl Ieee80211 {
// Frame has addr4 field
pub fn has_a4(&self) -> bool {
@@ -99,6 +107,9 @@ impl Ieee80211 {
}
}
+ /// Ieee80211 packets have 3-4 addresses in different positions based
+ /// on the FromDS and ToDS flags. This function gets the destination
+ /// address depending on the FromDS+ToDS packet subtypes.
pub fn get_destination(&self) -> MacAddress {
match self.specialize() {
Ieee80211Child::Ieee80211ToAp(hdr) => hdr.get_destination(),
@@ -149,4 +160,14 @@ mod tests {
let b = format!("{}", parse_mac_address("00:0b:85:71:20:ce").unwrap());
assert_eq!(a, b);
}
+
+ #[test]
+ fn test_is_multicast() {
+ // Multicast MAC address: 01:00:5E:00:00:FB
+ let mdns_mac_address = parse_mac_address("01:00:5e:00:00:fb").unwrap();
+ assert!(mdns_mac_address.is_multicast());
+ // Source address: Cisco_71:20:ce (00:0b:85:71:20:ce)
+ let non_mdns_mac_address = parse_mac_address("00:0b:85:71:20:ce").unwrap();
+ assert!(!non_mdns_mac_address.is_multicast());
+ }
}
diff --git a/rust/daemon/src/wifi/medium.rs b/rust/daemon/src/wifi/medium.rs
index 300291e..74511fc 100644
--- a/rust/daemon/src/wifi/medium.rs
+++ b/rust/daemon/src/wifi/medium.rs
@@ -90,16 +90,7 @@ pub fn parse_hwsim_cmd(packet: &[u8]) -> anyhow::Result<HwsimCmdEnum> {
}
pub fn test_parse_hwsim_cmd() {
- let packet: Vec<u8> = vec![
- 188, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 10, 0, 2, 0, 2, 21, 178, 0,
- 0, 0, 0, 0, 98, 0, 3, 0, 64, 0, 0, 0, 255, 255, 255, 255, 255, 255, 74, 129, 38, 251, 211,
- 154, 255, 255, 255, 255, 255, 255, 128, 12, 0, 0, 1, 8, 2, 4, 11, 22, 12, 18, 24, 36, 50,
- 4, 48, 72, 96, 108, 45, 26, 126, 16, 27, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 255, 22, 35, 1, 120, 200, 26, 64, 0, 0, 191, 206, 0, 0, 0, 0, 0, 0,
- 0, 0, 250, 255, 250, 255, 0, 0, 8, 0, 4, 0, 2, 0, 0, 0, 8, 0, 19, 0, 118, 9, 0, 0, 12, 0,
- 7, 0, 0, 1, 255, 0, 255, 0, 255, 0, 16, 0, 21, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0,
- 12, 0, 8, 0, 201, 0, 0, 0, 0, 0, 0, 0,
- ];
+ let packet: Vec<u8> = include!("test_packets/hwsim_cmd_frame.csv");
assert!(parse_hwsim_cmd(&packet).is_ok());
// missing transmitter attribute
@@ -151,4 +142,17 @@ mod tests {
fn test_netlink_attr() {
test_parse_hwsim_cmd();
}
+
+ #[test]
+ fn test_is_mdns_packet() {
+ let packet: Vec<u8> = include!("test_packets/hwsim_cmd_frame_mdns.csv");
+ let hwsim_msg = HwsimMsg::parse(&packet).unwrap();
+ let mdns_frame = Frame::parse(&hwsim_msg).unwrap();
+ assert!(mdns_frame.ieee80211.get_destination().is_multicast());
+
+ let packet: Vec<u8> = include!("test_packets/hwsim_cmd_frame.csv");
+ let hwsim_msg = HwsimMsg::parse(&packet).unwrap();
+ let non_mdns_frame = Frame::parse(&hwsim_msg).unwrap();
+ assert!(!non_mdns_frame.ieee80211.get_destination().is_multicast());
+ }
}
diff --git a/rust/daemon/src/wifi/test_packets/hwsim_cmd_frame_mdns.csv b/rust/daemon/src/wifi/test_packets/hwsim_cmd_frame_mdns.csv
new file mode 100644
index 0000000..76b8f4c
--- /dev/null
+++ b/rust/daemon/src/wifi/test_packets/hwsim_cmd_frame_mdns.csv
@@ -0,0 +1 @@
+vec![160, 2, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 10, 0, 2, 0, 2, 21, 178, 0, 0, 0, 0, 0, 71, 2, 3, 0, 8, 1, 58, 1, 0, 19, 16, 133, 254, 1, 2, 21, 178, 0, 0, 0, 1, 0, 94, 0, 0, 251, 160, 8, 170, 170, 3, 0, 0, 0, 8, 0, 69, 0, 2, 35, 114, 146, 64, 0, 255, 17, 26, 44, 10, 0, 2, 16, 224, 0, 0, 251, 20, 233, 20, 233, 2, 15, 158, 47, 0, 0, 132, 0, 0, 0, 0, 12, 0, 0, 0, 5, 21, 97, 100, 98, 45, 69, 77, 85, 76, 65, 84, 79, 82, 51, 52, 88, 49, 88, 49, 51, 88, 48, 4, 95, 97, 100, 98, 4, 95, 116, 99, 112, 5, 108, 111, 99, 97, 108, 0, 0, 16, 128, 1, 0, 0, 17, 148, 0, 1, 0, 9, 95, 115, 101, 114, 118, 105, 99, 101, 115, 7, 95, 100, 110, 115, 45, 115, 100, 4, 95, 117, 100, 112, 192, 44, 0, 12, 0, 1, 0, 0, 17, 148, 0, 2, 192, 34, 192, 34, 0, 12, 0, 1, 0, 0, 17, 148, 0, 2, 192, 12, 192, 12, 0, 33, 128, 1, 0, 0, 0, 120, 0, 16, 0, 0, 0, 0, 21, 179, 7, 65, 110, 100, 114, 111, 105, 100, 192, 44, 45, 87, 105, 70, 105, 84, 101, 115, 116, 45, 55, 49, 52, 102, 50, 54, 56, 52, 45, 101, 102, 101, 52, 45, 52, 55, 97, 100, 45, 97, 55, 53, 99, 45, 100, 55, 97, 101, 100, 101, 54, 55, 55, 56, 99, 102, 10, 95, 119, 105, 102, 105, 95, 116, 101, 115, 116, 192, 39, 0, 16, 128, 1, 0, 0, 17, 148, 0, 1, 0, 192, 62, 0, 12, 0, 1, 0, 0, 17, 148, 0, 2, 192, 187, 192, 187, 0, 12, 0, 1, 0, 0, 17, 148, 0, 2, 192, 141, 192, 141, 0, 33, 128, 1, 0, 0, 0, 120, 0, 8, 0, 0, 0, 0, 129, 135, 192, 131, 2, 49, 54, 1, 50, 1, 48, 2, 49, 48, 7, 105, 110, 45, 97, 100, 100, 114, 4, 97, 114, 112, 97, 0, 0, 12, 128, 1, 0, 0, 0, 120, 0, 2, 192, 131, 1, 55, 1, 50, 1, 53, 1, 49, 1, 57, 1, 52, 1, 51, 1, 65, 1, 56, 1, 52, 1, 56, 1, 55, 1, 55, 1, 53, 1, 55, 1, 53, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 56, 1, 69, 1, 70, 3, 105, 112, 54, 193, 21, 0, 12, 128, 1, 0, 0, 0, 120, 0, 2, 192, 131, 192, 131, 0, 1, 128, 1, 0, 0, 0, 120, 0, 4, 10, 0, 2, 16, 192, 131, 0, 28, 128, 1, 0, 0, 0, 120, 0, 16, 254, 128, 0, 0, 0, 0, 0, 0, 87, 87, 120, 72, 163, 73, 21, 39, 192, 12, 0, 47, 128, 1, 0, 0, 17, 148, 0, 9, 192, 12, 0, 5, 0, 0, 128, 0, 64, 192, 141, 0, 47, 128, 1, 0, 0, 17, 148, 0, 9, 192, 141, 0, 5, 0, 0, 128, 0, 64, 193, 3, 0, 47, 128, 1, 0, 0, 0, 120, 0, 6, 193, 3, 0, 2, 0, 8, 193, 39, 0, 47, 128, 1, 0, 0, 0, 120, 0, 6, 193, 39, 0, 2, 0, 8, 192, 131, 0, 47, 128, 1, 0, 0, 0, 120, 0, 8, 192, 131, 0, 4, 64, 0, 0, 8, 0, 8, 0, 4, 0, 0, 0, 0, 0, 8, 0, 19, 0, 143, 9, 0, 0, 12, 0, 7, 0, 0, 2, 0, 2, 0, 2, 255, 0, 16, 0, 21, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 255, 0, 0, 12, 0, 8, 0, 139, 0, 0, 0, 0, 0, 0, 0] \ No newline at end of file