1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <array> 20 #include <cstddef> 21 #include <cstdint> 22 #include <utility> 23 #include <vector> 24 25 #include "packets/hci_packets.h" 26 27 namespace rootcanal { 28 29 // Filter to remove user information from packets added to a PCAP trace. 30 // This is necessary in order to ensure no identifyiable information 31 // remains in traces uploaded in debug traces. 32 // 33 // The packets are transformed using the following rules: 34 // 35 // - HCI command / event packets: 36 // + Re-map device names to random names of the same length. 37 // The device addresses are already provided by RootCanal. 38 // 39 // - HCI ACL / SCO / ISO packets: 40 // + Wipe the packet payload with zeros. 41 // The ACL data is usually of no consequence for debugging 42 // RootCanal issues, and can be safely removed. 43 44 class PcapFilter final { 45 public: 46 PcapFilter() = default; 47 48 // Main function to filter out user data in HCI packets. 49 std::vector<uint8_t> FilterHciPacket(std::vector<uint8_t> const& packet, uint8_t idc); 50 51 std::vector<uint8_t> FilterHciCommand(std::vector<uint8_t> const& packet); 52 std::vector<uint8_t> FilterHciEvent(std::vector<uint8_t> const& packet); 53 54 // Specific filters for HCI commands. 55 std::vector<uint8_t> FilterWriteLocalName(bluetooth::hci::CommandView& command); 56 std::vector<uint8_t> FilterWriteExtendedInquiryResponse(bluetooth::hci::CommandView& command); 57 std::vector<uint8_t> FilterLeSetAdvertisingData(bluetooth::hci::CommandView& command); 58 std::vector<uint8_t> FilterLeSetScanResponseData(bluetooth::hci::CommandView& command); 59 std::vector<uint8_t> FilterLeSetExtendedAdvertisingData(bluetooth::hci::CommandView& command); 60 std::vector<uint8_t> FilterLeSetExtendedScanResponseData(bluetooth::hci::CommandView& command); 61 std::vector<uint8_t> FilterLeSetPeriodicAdvertisingData(bluetooth::hci::CommandView& command); 62 63 // Specific filters for HCI events. 64 std::vector<uint8_t> FilterReadLocalNameComplete( 65 bluetooth::hci::CommandCompleteView& command_complete); 66 std::vector<uint8_t> FilterReadExtendedInquiryResponseComplete( 67 bluetooth::hci::CommandCompleteView& command_complete); 68 std::vector<uint8_t> FilterRemoteNameRequestComplete(bluetooth::hci::EventView& event); 69 std::vector<uint8_t> FilterExtendedInquiryResult(bluetooth::hci::EventView& event); 70 std::vector<uint8_t> FilterLeAdvertisingReport(bluetooth::hci::LeMetaEventView& event); 71 std::vector<uint8_t> FilterLeExtendedAdvertisingReport(bluetooth::hci::LeMetaEventView& event); 72 73 // Specific filter for any Gap data array. 74 // The Gap data entries are modified in place. 75 void FilterGapData(uint8_t* gap_data, size_t gap_data_len); 76 void FilterGapData(std::vector<uint8_t>& gap_data); 77 78 // Helpers to replace local names. 79 std::array<uint8_t, 248> ChangeDeviceName(std::array<uint8_t, 248> const& device_name); 80 std::vector<uint8_t> ChangeDeviceName(std::vector<uint8_t> const& device_name); 81 82 private: 83 // Map device names to anonymous replacements. 84 std::vector<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>> device_name_map{}; 85 }; 86 87 } // namespace rootcanal 88