1 /* -*- c -*- */ 2 /* 3 * Copyright 2007 - 2013 Dominic Spill, Michael Ossmann, Will Code 4 * 5 * This file is part of libbtbb 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with libbtbb; see the file COPYING. If not, write to 19 * the Free Software Foundation, Inc., 51 Franklin Street, 20 * Boston, MA 02110-1301, USA. 21 */ 22 #ifndef INCLUDED_BTBB_H 23 #define INCLUDED_BTBB_H 24 25 #include <stdint.h> 26 27 #define BTBB_WHITENED 0 28 #define BTBB_NAP_VALID 1 29 #define BTBB_UAP_VALID 2 30 #define BTBB_LAP_VALID 3 31 #define BTBB_CLK6_VALID 4 32 #define BTBB_CLK27_VALID 5 33 #define BTBB_CRC_CORRECT 6 34 #define BTBB_HAS_PAYLOAD 7 35 #define BTBB_IS_EDR 8 36 37 #define BTBB_HOP_REVERSAL_INIT 9 38 #define BTBB_GOT_FIRST_PACKET 10 39 #define BTBB_IS_AFH 11 40 #define BTBB_LOOKS_LIKE_AFH 12 41 #define BTBB_IS_ALIASED 13 42 #define BTBB_FOLLOWING 14 43 44 #ifdef __cplusplus 45 extern "C" 46 { 47 #endif 48 49 /* BT BR/EDR support */ 50 51 typedef struct btbb_packet btbb_packet; 52 53 /* Initialize the library. Compute the syndrome. Return 0 on success, 54 * negative on error. 55 * 56 * The library limits max_ac_errors to 5. Using a larger value will 57 * take up a lot of memory (several GB), without decoding many useful 58 * packets. Even a limit of 5 results in a syndrome table of several 59 * hundred MB and lots of noise. For embedded targets, a value of 2 is 60 * reasonable. */ 61 int btbb_init(int max_ac_errors); 62 63 char *btbb_get_release(void); 64 char *btbb_get_version(void); 65 66 btbb_packet *btbb_packet_new(void); 67 void btbb_packet_ref(btbb_packet *pkt); 68 void btbb_packet_unref(btbb_packet *pkt); 69 70 /* Search for a packet with specified LAP (or LAP_ANY). The stream 71 * must be at least of length serch_length + 72. Limit to 72 * 'max_ac_errors' bit errors. 73 * 74 * Returns offset into 'stream' at which packet was found. If no 75 * packet was found, returns a negative number. If LAP_ANY was 76 * specified, fills lap. 'ac_errors' must be set as an input, replaced 77 * by actual number of errors on output. */ 78 int btbb_find_ac(char *stream, 79 int search_length, 80 uint32_t lap, 81 int max_ac_errors, 82 btbb_packet **pkt); 83 #define LAP_ANY 0xffffffffUL 84 #define UAP_ANY 0xff 85 86 void btbb_packet_set_flag(btbb_packet *pkt, int flag, int val); 87 int btbb_packet_get_flag(const btbb_packet *pkt, int flag); 88 89 uint32_t btbb_packet_get_lap(const btbb_packet *pkt); 90 void btbb_packet_set_uap(btbb_packet *pkt, uint8_t uap); 91 uint8_t btbb_packet_get_uap(const btbb_packet *pkt); 92 uint16_t btbb_packet_get_nap(const btbb_packet *pkt); 93 94 uint8_t btbb_packet_get_channel(const btbb_packet *pkt); 95 uint8_t btbb_packet_get_ac_errors(const btbb_packet *pkt); 96 uint32_t btbb_packet_get_clkn(const btbb_packet *pkt); 97 uint32_t btbb_packet_get_header_packed(const btbb_packet* pkt); 98 99 void btbb_packet_set_data(btbb_packet *pkt, 100 char *syms, // Symbol data 101 int length, // Number of symbols 102 uint8_t channel, // Bluetooth channel 0-79 103 uint32_t clkn); // 312.5us clock (CLK27-0) 104 105 /* Get a pointer to packet symbols. */ 106 const char *btbb_get_symbols(const btbb_packet* pkt); 107 108 int btbb_packet_get_payload_length(const btbb_packet* pkt); 109 110 /* Get a pointer to payload. */ 111 const char *btbb_get_payload(const btbb_packet* pkt); 112 113 /* Pack the payload in to bytes */ 114 int btbb_get_payload_packed(const btbb_packet* pkt, char *dst); 115 116 uint8_t btbb_packet_get_type(const btbb_packet* pkt); 117 uint8_t btbb_packet_get_lt_addr(const btbb_packet* pkt); 118 uint8_t btbb_packet_get_header_flags(const btbb_packet* pkt); 119 uint8_t btbb_packet_get_hec(const btbb_packet *pkt); 120 121 /* Generate Sync Word from an LAP */ 122 uint64_t btbb_gen_syncword(const int LAP); 123 124 /* decode the packet header */ 125 int btbb_decode_header(btbb_packet* pkt); 126 127 /* decode the packet header */ 128 int btbb_decode_payload(btbb_packet* pkt); 129 130 /* print packet information */ 131 void btbb_print_packet(const btbb_packet* pkt); 132 133 /* check to see if the packet has a header */ 134 int btbb_header_present(const btbb_packet* pkt); 135 136 /* Packet queue (linked list) */ 137 typedef struct pkt_queue { 138 btbb_packet *pkt; 139 140 struct pkt_queue *next; 141 142 } pkt_queue; 143 144 typedef struct btbb_piconet btbb_piconet; 145 146 btbb_piconet *btbb_piconet_new(void); 147 void btbb_piconet_ref(btbb_piconet *pn); 148 void btbb_piconet_unref(btbb_piconet *pn); 149 150 /* initialize the piconet struct */ 151 void btbb_init_piconet(btbb_piconet *pn, uint32_t lap); 152 153 void btbb_piconet_set_uap(btbb_piconet *pn, uint8_t uap); 154 uint8_t btbb_piconet_get_uap(const btbb_piconet *pn); 155 uint32_t btbb_piconet_get_lap(const btbb_piconet *pn); 156 uint16_t btbb_piconet_get_nap(const btbb_piconet *pn); 157 uint64_t btbb_piconet_get_bdaddr(const btbb_piconet *pn); 158 159 int btbb_piconet_get_clk_offset(const btbb_piconet *pn); 160 void btbb_piconet_set_clk_offset(btbb_piconet *pn, int clk_offset); 161 162 void btbb_piconet_set_flag(btbb_piconet *pn, int flag, int val); 163 int btbb_piconet_get_flag(const btbb_piconet *pn, int flag); 164 165 void btbb_piconet_set_channel_seen(btbb_piconet *pn, uint8_t channel); 166 void btbb_piconet_set_afh_map(btbb_piconet *pn, uint8_t *afh_map); 167 uint8_t *btbb_piconet_get_afh_map(btbb_piconet *pn); 168 169 /* Extract as much information (LAP/UAP/CLK) as possible from received packet */ 170 int btbb_process_packet(btbb_packet *pkt, btbb_piconet *pn); 171 172 /* use packet headers to determine UAP */ 173 int btbb_uap_from_header(btbb_packet *pkt, btbb_piconet *pn); 174 175 /* Print hexadecimal representation of the derived AFH map */ 176 void btbb_print_afh_map(btbb_piconet *pn); 177 178 /* decode a whole packet from the given piconet */ 179 int btbb_decode(btbb_packet* pkt, btbb_piconet *pn); 180 181 182 /* initialize the hop reversal process */ 183 /* returns number of initial candidates for CLK1-27 */ 184 int btbb_init_hop_reversal(int aliased, btbb_piconet *pn); 185 186 /* narrow a list of candidate clock values based on all observed hops */ 187 int btbb_winnow(btbb_piconet *pn); 188 189 int btbb_init_survey(void); 190 /* Destructively iterate over survey results - optionally remove elements */ 191 btbb_piconet *btbb_next_survey_result(void); 192 193 typedef struct btbb_pcapng_handle btbb_pcapng_handle; 194 /* create a PCAPNG file for BREDR captures */ 195 int btbb_pcapng_create_file(const char *filename, const char *interface_desc, btbb_pcapng_handle ** ph); 196 /* save a BREDR packet to PCAPNG capture file */ 197 int btbb_pcapng_append_packet(btbb_pcapng_handle * h, const uint64_t ns, 198 const int8_t sigdbm, const int8_t noisedbm, 199 const uint32_t reflap, const uint8_t refuap, 200 const btbb_packet *pkt); 201 /* record a BDADDR to PCAPNG capture file */ 202 int btbb_pcapng_record_bdaddr(btbb_pcapng_handle * h, const uint64_t bdaddr, 203 const uint8_t uapmask, const uint8_t napvalid); 204 /* record BT CLOCK to PCAPNG capture file */ 205 int btbb_pcapng_record_btclock(btbb_pcapng_handle * h, const uint64_t bdaddr, 206 const uint64_t ns, const uint32_t clk, const uint32_t clkmask); 207 int btbb_pcapng_close(btbb_pcapng_handle * h); 208 209 #if defined(USE_PCAP) 210 typedef struct btbb_pcap_handle btbb_pcap_handle; 211 /* create a PCAP file for BREDR captures with LINKTYPE_BLUETOOTH_BREDR_BB */ 212 int btbb_pcap_create_file(const char *filename, btbb_pcap_handle ** ph); 213 /* write a BREDR packet to PCAP file */ 214 int btbb_pcap_append_packet(btbb_pcap_handle * h, const uint64_t ns, 215 const int8_t sigdbm, const int8_t noisedbm, 216 const uint32_t reflap, const uint8_t refuap, 217 const btbb_packet *pkt); 218 int btbb_pcap_close(btbb_pcap_handle * h); 219 #endif 220 221 /* BTLE support */ 222 223 typedef struct lell_packet lell_packet; 224 /* decode and allocate LE packet */ 225 void lell_allocate_and_decode(const uint8_t *stream, uint16_t phys_channel, uint32_t clk100ns, lell_packet **pkt); 226 lell_packet *lell_packet_new(void); 227 void lell_packet_ref(lell_packet *pkt); 228 void lell_packet_unref(lell_packet *pkt); 229 uint32_t lell_get_access_address(const lell_packet *pkt); 230 unsigned lell_get_access_address_offenses(const lell_packet *pkt); 231 unsigned lell_packet_is_data(const lell_packet *pkt); 232 unsigned lell_get_channel_index(const lell_packet *pkt); 233 unsigned lell_get_channel_k(const lell_packet *pkt); 234 const char * lell_get_adv_type_str(const lell_packet *pkt); 235 void lell_print(const lell_packet *pkt); 236 237 typedef struct lell_pcapng_handle lell_pcapng_handle; 238 /* create a PCAPNG file for LE captures */ 239 int lell_pcapng_create_file(const char *filename, const char *interface_desc, lell_pcapng_handle ** ph); 240 /* save an LE packet to PCAPNG capture file */ 241 int lell_pcapng_append_packet(lell_pcapng_handle * h, const uint64_t ns, 242 const int8_t sigdbm, const int8_t noisedbm, 243 const uint32_t refAA, const lell_packet *pkt); 244 /* record LE CONNECT_REQ parameters to PCAPNG capture file */ 245 int lell_pcapng_record_connect_req(lell_pcapng_handle * h, const uint64_t ns, const uint8_t * pdu); 246 int lell_pcapng_close(lell_pcapng_handle *h); 247 248 #if defined(USE_PCAP) 249 typedef struct lell_pcap_handle lell_pcap_handle; 250 /* create a PCAP file for LE captures using LINKTYPE_BLUETOOTH_LE_LL_WITH_PHDR */ 251 int lell_pcap_create_file(const char *filename, lell_pcap_handle ** ph); 252 /* create a PCAP file for LE captures using LINKTYPE_PPI */ 253 int lell_pcap_ppi_create_file(const char *filename, int btle_ppi_version, lell_pcap_handle ** ph); 254 /* save an LE packet to PCAP capture file */ 255 int lell_pcap_append_packet(lell_pcap_handle * h, const uint64_t ns, 256 const int8_t sigdbm, const int8_t noisedbm, 257 const uint32_t refAA, const lell_packet *pkt); 258 int lell_pcap_append_ppi_packet(lell_pcap_handle * h, const uint64_t ns, 259 const uint8_t clkn_high, 260 const int8_t rssi_min, const int8_t rssi_max, 261 const int8_t rssi_avg, const uint8_t rssi_count, 262 const lell_packet *pkt); 263 int lell_pcap_close(lell_pcap_handle *h); 264 #endif 265 266 #ifdef __cplusplus 267 } // __cplusplus defined. 268 #endif 269 270 #endif /* INCLUDED_BTBB_H */ 271