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