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