1 /*
2  * Copyright 2018 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 #include "model/devices/baseband_sniffer.h"
18 
19 #include <cstdint>
20 #include <ios>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "log.h"
27 #include "packets/link_layer_packets.h"
28 #include "pcap.h"
29 #include "phy.h"
30 
31 namespace rootcanal {
32 
33 #include "bredr_bb.h"
34 
BaseBandSniffer(const std::string & filename)35 BaseBandSniffer::BaseBandSniffer(const std::string& filename) {
36   output_.open(filename, std::ios::binary);
37 
38   uint32_t linktype = 255;  // http://www.tcpdump.org/linktypes.html
39                             // LINKTYPE_BLUETOOTH_BREDR_BB
40 
41   pcap::WriteHeader(output_, linktype);
42   output_.flush();
43 }
44 
AppendRecord(std::unique_ptr<bredr_bb::BaseBandPacketBuilder> packet)45 void BaseBandSniffer::AppendRecord(std::unique_ptr<bredr_bb::BaseBandPacketBuilder> packet) {
46   std::vector<uint8_t> bytes = packet->SerializeToBytes();
47   pcap::WriteRecordHeader(output_, bytes.size());
48   output_.write((char*)bytes.data(), bytes.size());
49   output_.flush();
50 }
51 
ReverseByte(uint8_t b)52 static uint8_t ReverseByte(uint8_t b) {
53   static uint8_t lookup[16] = {
54           [0b0000] = 0b0000, [0b0001] = 0b1000, [0b0010] = 0b0100, [0b0011] = 0b1100,
55           [0b0100] = 0b0010, [0b0101] = 0b1010, [0b0110] = 0b0110, [0b0111] = 0b1110,
56           [0b1000] = 0b0001, [0b1001] = 0b1001, [0b1010] = 0b0101, [0b1011] = 0b1101,
57           [0b1100] = 0b0011, [0b1101] = 0b1011, [0b1110] = 0b0111, [0b1111] = 0b1111,
58   };
59 
60   return (lookup[b & 0xF] << 4) | lookup[b >> 4];
61 }
62 
HeaderErrorCheck(uint8_t uap,uint32_t data)63 static uint8_t HeaderErrorCheck(uint8_t uap, uint32_t data) {
64   // See Bluetooth Core, Vol 2, Part B, 7.1.1
65 
66   uint8_t value = ReverseByte(uap);
67 
68   for (auto i = 0; i < 10; i++) {
69     bool bit = (value ^ data) & 1;
70     data >>= 1;
71     value >>= 1;
72     if (bit) {
73       value ^= 0xe5;
74     }
75   }
76 
77   return value;
78 }
79 
BuildBtPacketHeader(uint8_t uap,uint8_t lt_addr,uint8_t packet_type,bool flow,bool arqn,bool seqn)80 static uint32_t BuildBtPacketHeader(uint8_t uap, uint8_t lt_addr, uint8_t packet_type, bool flow,
81                                     bool arqn, bool seqn) {
82   // See Bluetooth Core, Vol2, Part B, 6.4
83 
84   uint32_t header =
85           (lt_addr & 0x7) | ((packet_type & 0xF) << 3) | (flow << 7) | (arqn << 8) | (seqn << 9);
86 
87   header |= (HeaderErrorCheck(uap, header) << 10);
88 
89   return header;
90 }
91 
ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView packet,Phy::Type,int8_t)92 void BaseBandSniffer::ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView packet,
93                                              Phy::Type /*type*/, int8_t /*rssi*/) {
94   auto packet_type = packet.GetType();
95   auto address = packet.GetSourceAddress();
96 
97   // Bluetooth Core, Vol2, Part B, 1.2, Figure 1.5
98   uint32_t lap = address.data()[0] | (address.data()[1] << 8) | (address.data()[2] << 16);
99   uint8_t uap = address.data()[3];
100   uint16_t nap = address.data()[4] | (address.data()[5] << 8);
101 
102   // http://www.whiterocker.com/bt/LINKTYPE_BLUETOOTH_BREDR_BB.html
103   uint16_t flags =
104           /* BT Packet Header and BR or EDR Payload are de-whitened */ 0x0001 |
105           /* BR or EDR Payload is decrypted */ 0x0008 |
106           /* Reference LAP is valid and led to this packet being captured */
107           0x0010 |
108           /* BR or EDR Payload is present and follows this field */ 0x0020 |
109           /* Reference UAP field is valid for HEC and CRC checking */ 0x0080 |
110           /* CRC portion of the BR or EDR Payload was checked */ 0x0400 |
111           /* CRC portion of the BR or EDR Payload passed its check */ 0x0800;
112 
113   uint8_t lt_addr = 0;
114 
115   uint8_t rf_channel = 0;
116   uint8_t signal_power = 0;
117   uint8_t noise_power = 0;
118   uint8_t access_code_offenses = 0;
119   uint8_t corrected_header_bits = 0;
120   uint16_t corrected_payload_bits = 0;
121   uint8_t lower_address_part = lap;
122   uint8_t reference_lap = lap;
123   uint8_t reference_uap = uap;
124 
125   if (packet_type == model::packets::PacketType::PAGE) {
126     auto page_view = model::packets::PageView::Create(packet);
127     ASSERT(page_view.IsValid());
128 
129     uint8_t bt_packet_type = 0b0010;  // FHS
130 
131     AppendRecord(bredr_bb::FHSAclPacketBuilder::Create(
132             rf_channel, signal_power, noise_power, access_code_offenses, corrected_header_bits,
133             corrected_payload_bits, lower_address_part, reference_lap, reference_uap,
134             BuildBtPacketHeader(uap, lt_addr, bt_packet_type, true, true, true), flags,
135             0,  // parity_bits
136             lap,
137             0,  // eir
138             0,  // sr
139             0,  // sp
140             uap, nap, page_view.GetClassOfDevice(),
141             1,  // lt_addr
142             0,  // clk
143             0,  // page_scan_mode
144             0   // crc
145             ));
146   } else if (packet_type == model::packets::PacketType::LMP) {
147     auto lmp_view = model::packets::LmpView::Create(packet);
148     ASSERT(lmp_view.IsValid());
149     auto lmp_bytes = lmp_view.GetPayload();
150     uint8_t bt_packet_type = 0b0011;  // DM1
151 
152     AppendRecord(bredr_bb::DM1AclPacketBuilder::Create(
153             rf_channel, signal_power, noise_power, access_code_offenses, corrected_header_bits,
154             corrected_payload_bits, lower_address_part, reference_lap, reference_uap,
155             BuildBtPacketHeader(uap, lt_addr, bt_packet_type, true, true, true), flags,
156             0x3,  // llid
157             1,    // flow
158             std::move(lmp_bytes),
159             0  // crc
160             ));
161   }
162 }
163 
164 }  // namespace rootcanal
165