1 /* 2 * Copyright 2019 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 <bluetooth/log.h> 20 21 #include <fstream> 22 #include <string> 23 #include <unordered_map> 24 #include <unordered_set> 25 #include <vector> 26 27 #include "common/circular_buffer.h" 28 #include "hal/hci_hal.h" 29 #include "hal/snoop_logger_socket_interface.h" 30 #include "hal/snoop_logger_socket_thread.h" 31 #include "hal/syscall_wrapper_impl.h" 32 #include "module.h" 33 #include "os/repeating_alarm.h" 34 35 namespace bluetooth { 36 namespace hal { 37 38 #ifdef USE_FAKE_TIMERS 39 static uint64_t file_creation_time; 40 #endif 41 42 class FilterTracker { 43 public: 44 // NOTE: 1 is used as a static CID for L2CAP signaling 45 std::unordered_set<uint16_t> l2c_local_cid = {1}; 46 std::unordered_set<uint16_t> l2c_remote_cid = {1}; 47 uint16_t rfcomm_local_cid = 0; 48 uint16_t rfcomm_remote_cid = 0; 49 std::unordered_set<uint16_t> rfcomm_channels = {0}; 50 51 // Adds L2C channel to acceptlist. 52 void AddL2capCid(uint16_t local_cid, uint16_t remote_cid); 53 54 // Sets L2CAP channel that RFCOMM uses. 55 void SetRfcommCid(uint16_t local_cid, uint16_t remote_cid); 56 57 // Remove L2C channel from acceptlist. 58 void RemoveL2capCid(uint16_t local_cid, uint16_t remote_cid); 59 60 void AddRfcommDlci(uint8_t channel); 61 62 bool IsAcceptlistedL2cap(bool local, uint16_t cid); 63 64 bool IsRfcommChannel(bool local, uint16_t cid); 65 66 bool IsAcceptlistedDlci(uint8_t dlci); 67 }; 68 69 typedef enum { 70 FILTER_PROFILE_NONE = -1, 71 FILTER_PROFILE_PBAP = 0, 72 FILTER_PROFILE_HFP_HS, 73 FILTER_PROFILE_HFP_HF, 74 FILTER_PROFILE_MAP, 75 FILTER_PROFILE_MAX, 76 } profile_type_t; 77 78 class ProfilesFilter { 79 public: 80 void SetupProfilesFilter(bool pbap_filtered, bool map_filtered); 81 82 bool IsHfpProfile(bool local, uint16_t cid, uint8_t dlci); 83 84 bool IsL2capMatch(bool local, uint16_t cid); 85 86 bool IsL2capFlowExt(bool local, uint16_t cid); 87 88 bool IsRfcommMatch(bool local, uint16_t cid, uint8_t dlci); 89 90 bool IsRfcommFlowExt(bool local, uint16_t cid, uint8_t dlci); 91 92 profile_type_t CidToProfile(bool local, uint16_t cid); 93 94 profile_type_t DlciToProfile(bool local, uint16_t cid, uint8_t dlci); 95 96 void ProfileL2capOpen( 97 profile_type_t profile, uint16_t lcid, uint16_t rcid, uint16_t psm, bool flow_ext); 98 99 void ProfileL2capClose(profile_type_t profile); 100 101 void ProfileRfcommOpen( 102 profile_type_t profile, uint16_t lcid, uint8_t dlci, uint16_t uuid, bool flow_ext); 103 104 void ProfileRfcommClose(profile_type_t profile); 105 106 bool IsRfcommChannel(bool local, uint16_t cid); 107 108 void PrintProfilesConfig(); 109 ProfileToString(profile_type_t profile)110 static inline std::string ProfileToString(profile_type_t profile) { 111 switch (profile) { 112 case FILTER_PROFILE_NONE: 113 return "FILTER_PROFILE_NONE"; 114 case FILTER_PROFILE_PBAP: 115 return "FILTER_PROFILE_PBAP"; 116 case FILTER_PROFILE_HFP_HS: 117 return "FILTER_PROFILE_HFP_HS"; 118 case FILTER_PROFILE_HFP_HF: 119 return "FILTER_PROFILE_HFP_HF"; 120 case FILTER_PROFILE_MAP: 121 return "FILTER_PROFILE_MAP"; 122 default: 123 return "[Unknown profile_type_t]"; 124 } 125 } 126 127 uint16_t ch_rfc_l, ch_rfc_r; // local & remote L2CAP channel for RFCOMM 128 uint16_t ch_last; // last channel seen for fragment packet 129 130 private: 131 bool setup_done_flag = false; 132 struct { 133 profile_type_t type; 134 bool enabled, l2cap_opened, rfcomm_opened; 135 bool flow_ext_l2cap, flow_ext_rfcomm; 136 uint16_t lcid, rcid, rfcomm_uuid, psm; 137 uint8_t scn; 138 } profiles[FILTER_PROFILE_MAX]; 139 profile_type_t current_profile; 140 }; 141 142 class SnoopLogger : public ::bluetooth::Module { 143 public: 144 static const ModuleFactory Factory; 145 146 static const std::string kBtSnoopMaxPacketsPerFileProperty; 147 static const std::string kIsDebuggableProperty; 148 static const std::string kBtSnoopLogModeProperty; 149 static const std::string kBtSnoopLogPersists; 150 static const std::string kBtSnoopDefaultLogModeProperty; 151 static const std::string kBtSnoopLogFilterHeadersProperty; 152 static const std::string kBtSnoopLogFilterProfileA2dpProperty; 153 static const std::string kBtSnoopLogFilterProfileMapModeProperty; 154 static const std::string kBtSnoopLogFilterProfilePbapModeProperty; 155 static const std::string kBtSnoopLogFilterProfileRfcommProperty; 156 static const std::string kSoCManufacturerProperty; 157 158 static const std::string kBtSnoopLogModeDisabled; 159 static const std::string kBtSnoopLogModeFiltered; 160 static const std::string kBtSnoopLogModeFull; 161 162 static const std::string kSoCManufacturerQualcomm; 163 164 static const std::string kBtSnoopLogFilterProfileModeFullfillter; 165 static const std::string kBtSnoopLogFilterProfileModeHeader; 166 static const std::string kBtSnoopLogFilterProfileModeMagic; 167 static const std::string kBtSnoopLogFilterProfileModeDisabled; 168 169 std::unordered_map<std::string, bool> kBtSnoopLogFilterState = { 170 {kBtSnoopLogFilterHeadersProperty, false}, 171 {kBtSnoopLogFilterProfileA2dpProperty, false}, 172 {kBtSnoopLogFilterProfileRfcommProperty, false}}; 173 174 std::unordered_map<std::string, std::string> kBtSnoopLogFilterMode = { 175 {kBtSnoopLogFilterProfilePbapModeProperty, kBtSnoopLogFilterProfileModeDisabled}, 176 {kBtSnoopLogFilterProfileMapModeProperty, kBtSnoopLogFilterProfileModeDisabled}}; 177 178 // Put in header for test 179 struct PacketHeaderType { 180 uint32_t length_original; 181 uint32_t length_captured; 182 uint32_t flags; 183 uint32_t dropped_packets; 184 uint64_t timestamp; 185 uint8_t type; 186 } __attribute__((__packed__)); 187 188 // Struct for caching info about L2CAP Media Channel 189 struct A2dpMediaChannel { 190 uint16_t conn_handle; 191 uint16_t local_cid; 192 uint16_t remote_cid; 193 }; 194 195 // Returns the maximum number of packets per file 196 // Changes to this value is only effective after restarting Bluetooth 197 static size_t GetMaxPacketsPerFile(); 198 199 static size_t GetMaxPacketsPerBuffer(); 200 201 // Get snoop logger mode based on current system setup 202 // Changes to this values is only effective after restarting Bluetooth 203 static std::string GetBtSnoopMode(); 204 205 // Returns whether the soc manufacturer is Qualcomm 206 // Changes to this value is only effective after restarting Bluetooth 207 static bool IsQualcommDebugLogEnabled(); 208 209 // Returns whether snoop log persists even after restarting Bluetooth 210 static bool IsBtSnoopLogPersisted(); 211 212 // Has to be defined from 1 to 4 per btsnoop format 213 enum PacketType { 214 CMD = 1, 215 ACL = 2, 216 SCO = 3, 217 EVT = 4, 218 ISO = 5, 219 }; 220 221 enum Direction { 222 INCOMING, 223 OUTGOING, 224 }; 225 226 void Capture(const HciPacket& packet, Direction direction, PacketType type); 227 228 // Set a L2CAP channel as acceptlisted, allowing packets with that L2CAP CID 229 // to show up in the snoop logs. 230 void AcceptlistL2capChannel(uint16_t conn_handle, uint16_t local_cid, uint16_t remote_cid); 231 232 // Set a RFCOMM dlci as acceptlisted, allowing packets with that RFCOMM CID 233 // to show up in the snoop logs. The local_cid is used to associate it with 234 // its corrisponding ACL connection. The dlci is the channel with direction 235 // so there is no chance of a collision if two services are using the same 236 // channel but in different directions. 237 void AcceptlistRfcommDlci(uint16_t conn_handle, uint16_t local_cid, uint8_t dlci); 238 239 // Indicate that the provided L2CAP channel is being used for RFCOMM. 240 // If packets with the provided L2CAP CID are encountered, they will be 241 // filtered on RFCOMM based on channels provided to |filter_rfcomm_channel|. 242 void AddRfcommL2capChannel(uint16_t conn_handle, uint16_t local_cid, uint16_t remote_cid); 243 244 // Clear an L2CAP channel from being filtered. 245 void ClearL2capAcceptlist(uint16_t conn_handle, uint16_t local_cid, uint16_t remote_cid); 246 247 // Cache A2DP Media Channel info for filtering media packets. 248 void AddA2dpMediaChannel(uint16_t conn_handle, uint16_t local_cid, uint16_t remote_cid); 249 250 // Remove A2DP Media Channel cache 251 void RemoveA2dpMediaChannel(uint16_t conn_handle, uint16_t local_cid); 252 253 // New RFCOMM port is opened. 254 void SetRfcommPortOpen( 255 uint16_t conn_handle, uint16_t local_cid, uint8_t dlci, uint16_t uuid, bool flow); 256 // RFCOMM port is closed. 257 void SetRfcommPortClose(uint16_t handle, uint16_t local_cid, uint8_t dlci, uint16_t uuid); 258 259 // New L2CAP channel is opened. 260 void SetL2capChannelOpen( 261 uint16_t handle, uint16_t local_cid, uint16_t remote_cid, uint16_t psm, bool flow); 262 // L2CAP channel is closed. 263 void SetL2capChannelClose(uint16_t handle, uint16_t local_cid, uint16_t remote_cid); 264 265 void RegisterSocket(SnoopLoggerSocketInterface* socket); 266 267 protected: 268 // Packet type length 269 static const size_t PACKET_TYPE_LENGTH; 270 // The size of the L2CAP header. All information past this point is removed from 271 // a filtered packet. 272 static const uint32_t L2CAP_HEADER_SIZE; 273 // Max packet data size when headersfiltered option enabled 274 static const size_t MAX_HCI_ACL_LEN; 275 276 void ListDependencies(ModuleList* list) const override; 277 void Start() override; 278 void Stop() override; 279 DumpsysDataFinisher GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const override; ToString()280 std::string ToString() const override { 281 return std::string("SnoopLogger"); 282 } 283 284 SnoopLogger( 285 std::string snoop_log_path, 286 std::string snooz_log_path, 287 size_t max_packets_per_file, 288 size_t max_packets_per_buffer, 289 const std::string& btsnoop_mode, 290 bool qualcomm_debug_log_enabled, 291 const std::chrono::milliseconds snooz_log_life_time, 292 const std::chrono::milliseconds snooz_log_delete_alarm_interval, 293 bool snoop_log_persists); 294 void CloseCurrentSnoopLogFile(); 295 void OpenNextSnoopLogFile(); 296 void DumpSnoozLogToFile(const std::vector<std::string>& data) const; 297 // Enable filters according to their sysprops 298 void EnableFilters(); 299 // Disable all filters 300 void DisableFilters(); 301 // Check if the filter is enabled. Pass filter name as a string. 302 bool IsFilterEnabled(std::string filter_name); 303 // Check if packet should be filtered (rfcommchannelfiltered mode) 304 bool ShouldFilterLog(bool is_received, uint8_t* packet); 305 // Calculate packet length (snoopheadersfiltered mode) 306 void CalculateAclPacketLength(uint32_t& length, uint8_t* packet, bool is_received); 307 // Strip packet's payload (profilesfiltered mode) 308 uint32_t PayloadStrip( 309 profile_type_t current_profile, uint8_t* packet, uint32_t hdr_len, uint32_t pl_len); 310 // Filter profile packet according to its filtering mode 311 uint32_t FilterProfiles(bool is_received, uint8_t* packet); 312 // Check if packet is A2DP media packet (a2dppktsfiltered mode) 313 bool IsA2dpMediaPacket(bool is_received, uint8_t* packet); 314 // Chec if channel is cached in snoop logger for filtering (a2dppktsfiltered mode) 315 bool IsA2dpMediaChannel(uint16_t conn_handle, uint16_t cid, bool is_local_cid); 316 // Handle HFP filtering while profilesfiltered enabled 317 uint32_t FilterProfilesHandleHfp( 318 uint8_t* packet, uint32_t length, uint32_t totlen, uint32_t offset); 319 void FilterProfilesRfcommChannel( 320 uint8_t* packet, 321 uint8_t& current_offset, 322 uint32_t& length, 323 profile_type_t& current_profile, 324 bluetooth::hal::ProfilesFilter& filters, 325 bool is_received, 326 uint16_t l2cap_channel, 327 uint32_t& offset, 328 uint32_t total_length); 329 void FilterCapturedPacket( 330 HciPacket& packet, 331 Direction direction, 332 PacketType type, 333 uint32_t& length, 334 PacketHeaderType header); 335 336 std::unique_ptr<SnoopLoggerSocketThread> snoop_logger_socket_thread_; 337 338 private: 339 static std::string btsnoop_mode_; 340 std::string snoop_log_path_; 341 std::string snooz_log_path_; 342 std::ofstream btsnoop_ostream_; 343 size_t max_packets_per_file_; 344 common::CircularBuffer<std::string> btsnooz_buffer_; 345 bool qualcomm_debug_log_enabled_ = false; 346 size_t packet_counter_ = 0; 347 mutable std::recursive_mutex file_mutex_; 348 std::unique_ptr<os::RepeatingAlarm> alarm_; 349 std::chrono::milliseconds snooz_log_life_time_; 350 std::chrono::milliseconds snooz_log_delete_alarm_interval_; 351 SnoopLoggerSocketInterface* socket_; 352 SyscallWrapperImpl syscall_if; 353 bool snoop_log_persists = false; 354 }; 355 356 } // namespace hal 357 } // namespace bluetooth 358 359 namespace fmt { 360 template <> 361 struct formatter<bluetooth::hal::profile_type_t> : enum_formatter<bluetooth::hal::profile_type_t> { 362 }; 363 } // namespace fmt 364