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,
50                                        uint8_t idc);
51 
52   std::vector<uint8_t> FilterHciCommand(std::vector<uint8_t> const& packet);
53   std::vector<uint8_t> FilterHciEvent(std::vector<uint8_t> const& packet);
54 
55   // Specific filters for HCI commands.
56   std::vector<uint8_t> FilterWriteLocalName(
57       bluetooth::hci::CommandView& command);
58   std::vector<uint8_t> FilterWriteExtendedInquiryResponse(
59       bluetooth::hci::CommandView& command);
60   std::vector<uint8_t> FilterLeSetAdvertisingData(
61       bluetooth::hci::CommandView& command);
62   std::vector<uint8_t> FilterLeSetScanResponseData(
63       bluetooth::hci::CommandView& command);
64   std::vector<uint8_t> FilterLeSetExtendedAdvertisingData(
65       bluetooth::hci::CommandView& command);
66   std::vector<uint8_t> FilterLeSetExtendedScanResponseData(
67       bluetooth::hci::CommandView& command);
68   std::vector<uint8_t> FilterLeSetPeriodicAdvertisingData(
69       bluetooth::hci::CommandView& command);
70 
71   // Specific filters for HCI events.
72   std::vector<uint8_t> FilterReadLocalNameComplete(
73       bluetooth::hci::CommandCompleteView& command_complete);
74   std::vector<uint8_t> FilterReadExtendedInquiryResponseComplete(
75       bluetooth::hci::CommandCompleteView& command_complete);
76   std::vector<uint8_t> FilterRemoteNameRequestComplete(
77       bluetooth::hci::EventView& event);
78   std::vector<uint8_t> FilterExtendedInquiryResult(
79       bluetooth::hci::EventView& event);
80   std::vector<uint8_t> FilterLeAdvertisingReport(
81       bluetooth::hci::LeMetaEventView& event);
82   std::vector<uint8_t> FilterLeExtendedAdvertisingReport(
83       bluetooth::hci::LeMetaEventView& event);
84 
85   // Specific filter for any Gap data array.
86   // The Gap data entries are modified in place.
87   void FilterGapData(uint8_t* gap_data, size_t gap_data_len);
88   void FilterGapData(std::vector<uint8_t>& gap_data);
89 
90   // Helpers to replace local names.
91   std::array<uint8_t, 248> ChangeDeviceName(
92       std::array<uint8_t, 248> const& device_name);
93   std::vector<uint8_t> ChangeDeviceName(
94       std::vector<uint8_t> const& device_name);
95 
96  private:
97   // Map device names to anonymous replacements.
98   std::vector<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>>
99       device_name_map{};
100 };
101 
102 }  // namespace rootcanal
103