18f3e7eeaSChristopher Kilgour /* -*- c -*- */ 28f3e7eeaSChristopher Kilgour /* 38f3e7eeaSChristopher Kilgour * Copyright 2014 Christopher D. Kilgour techie AT whiterocker.com 48f3e7eeaSChristopher Kilgour * 58f3e7eeaSChristopher Kilgour * This file is part of libbtbb 68f3e7eeaSChristopher Kilgour * 78f3e7eeaSChristopher Kilgour * This program is free software; you can redistribute it and/or modify 88f3e7eeaSChristopher Kilgour * it under the terms of the GNU General Public License as published by 98f3e7eeaSChristopher Kilgour * the Free Software Foundation; either version 2, or (at your option) 108f3e7eeaSChristopher Kilgour * any later version. 118f3e7eeaSChristopher Kilgour * 128f3e7eeaSChristopher Kilgour * This program is distributed in the hope that it will be useful, 138f3e7eeaSChristopher Kilgour * but WITHOUT ANY WARRANTY; without even the implied warranty of 148f3e7eeaSChristopher Kilgour * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158f3e7eeaSChristopher Kilgour * GNU General Public License for more details. 168f3e7eeaSChristopher Kilgour * 178f3e7eeaSChristopher Kilgour * You should have received a copy of the GNU General Public License 188f3e7eeaSChristopher Kilgour * along with libbtbb; see the file COPYING. If not, write to 198f3e7eeaSChristopher Kilgour * the Free Software Foundation, Inc., 51 Franklin Street, 208f3e7eeaSChristopher Kilgour * Boston, MA 02110-1301, USA. 218f3e7eeaSChristopher Kilgour */ 228f3e7eeaSChristopher Kilgour 238f3e7eeaSChristopher Kilgour #include "btbb.h" 248f3e7eeaSChristopher Kilgour #include "bluetooth_le_packet.h" 25*f83b85cfSDominic Spill #include "bluetooth_packet.h" 268f3e7eeaSChristopher Kilgour #include "pcapng-bt.h" 278f3e7eeaSChristopher Kilgour #include <stdlib.h> 288f3e7eeaSChristopher Kilgour #include <string.h> 298f3e7eeaSChristopher Kilgour #include <time.h> 308f3e7eeaSChristopher Kilgour #include <unistd.h> 310e05cc66SDominic Spill #include <stdio.h> 328f3e7eeaSChristopher Kilgour 338f3e7eeaSChristopher Kilgour /* generic section options indicating libbtbb */ 348f3e7eeaSChristopher Kilgour const struct { 358f3e7eeaSChristopher Kilgour struct { 368f3e7eeaSChristopher Kilgour option_header hdr; 378f3e7eeaSChristopher Kilgour char libname[8]; 388f3e7eeaSChristopher Kilgour } libopt; 398f3e7eeaSChristopher Kilgour struct { 408f3e7eeaSChristopher Kilgour option_header hdr; 418f3e7eeaSChristopher Kilgour } termopt; 428f3e7eeaSChristopher Kilgour } libbtbb_section_options = { 438f3e7eeaSChristopher Kilgour .libopt = { 448f3e7eeaSChristopher Kilgour .hdr = { 458f3e7eeaSChristopher Kilgour .option_code = SHB_USERAPPL, 468f3e7eeaSChristopher Kilgour .option_length = 7 }, 478f3e7eeaSChristopher Kilgour .libname = "libbtbb" 488f3e7eeaSChristopher Kilgour }, 498f3e7eeaSChristopher Kilgour .termopt = { 508f3e7eeaSChristopher Kilgour .hdr = { 518f3e7eeaSChristopher Kilgour .option_code = OPT_ENDOFOPT, 528f3e7eeaSChristopher Kilgour .option_length = 0 538f3e7eeaSChristopher Kilgour } 548f3e7eeaSChristopher Kilgour } 558f3e7eeaSChristopher Kilgour }; 568f3e7eeaSChristopher Kilgour 578f3e7eeaSChristopher Kilgour static PCAPNG_RESULT 588f3e7eeaSChristopher Kilgour check_and_fix_tsresol( PCAPNG_HANDLE * handle, 598f3e7eeaSChristopher Kilgour const option_header * interface_options ) 608f3e7eeaSChristopher Kilgour { 618f3e7eeaSChristopher Kilgour PCAPNG_RESULT retval = PCAPNG_OK; 628f3e7eeaSChristopher Kilgour int got_tsresol = 0; 638f3e7eeaSChristopher Kilgour 648f3e7eeaSChristopher Kilgour while( !got_tsresol && 658f3e7eeaSChristopher Kilgour interface_options && 668f3e7eeaSChristopher Kilgour interface_options->option_code && 678f3e7eeaSChristopher Kilgour interface_options->option_length) { 688f3e7eeaSChristopher Kilgour if (interface_options->option_code == IF_TSRESOL) { 698f3e7eeaSChristopher Kilgour got_tsresol = 1; 708f3e7eeaSChristopher Kilgour } 718f3e7eeaSChristopher Kilgour else { 728f3e7eeaSChristopher Kilgour size_t step = 4+4*((interface_options->option_length+3)/4); 738f3e7eeaSChristopher Kilgour uint8_t * next = &((uint8_t *)interface_options)[step]; 748f3e7eeaSChristopher Kilgour interface_options = (const option_header *) next; 758f3e7eeaSChristopher Kilgour } 768f3e7eeaSChristopher Kilgour } 778f3e7eeaSChristopher Kilgour 788f3e7eeaSChristopher Kilgour if (!got_tsresol) { 798f3e7eeaSChristopher Kilgour const struct { 808f3e7eeaSChristopher Kilgour option_header hdr; 818f3e7eeaSChristopher Kilgour uint8_t resol; 828f3e7eeaSChristopher Kilgour } tsresol = { 838f3e7eeaSChristopher Kilgour .hdr = { 848f3e7eeaSChristopher Kilgour .option_code = IF_TSRESOL, 858f3e7eeaSChristopher Kilgour .option_length = 1, 868f3e7eeaSChristopher Kilgour }, 878f3e7eeaSChristopher Kilgour .resol = 9 /* 10^-9 is nanoseconds */ 888f3e7eeaSChristopher Kilgour }; 898f3e7eeaSChristopher Kilgour 908f3e7eeaSChristopher Kilgour retval = pcapng_append_interface_option( handle, 918f3e7eeaSChristopher Kilgour (const option_header *) &tsresol ); 928f3e7eeaSChristopher Kilgour } 938f3e7eeaSChristopher Kilgour 948f3e7eeaSChristopher Kilgour return retval; 958f3e7eeaSChristopher Kilgour } 968f3e7eeaSChristopher Kilgour 978f3e7eeaSChristopher Kilgour /* --------------------------------- BR/EDR ----------------------------- */ 988f3e7eeaSChristopher Kilgour 998f3e7eeaSChristopher Kilgour static PCAPNG_RESULT 1008f3e7eeaSChristopher Kilgour create_bredr_capture_file_single_interface( PCAPNG_HANDLE * handle, 1018f3e7eeaSChristopher Kilgour const char * filename, 1028f3e7eeaSChristopher Kilgour const option_header * interface_options ) 1038f3e7eeaSChristopher Kilgour { 1048f3e7eeaSChristopher Kilgour PCAPNG_RESULT retval = PCAPNG_OK; 1058f3e7eeaSChristopher Kilgour 1068f3e7eeaSChristopher Kilgour retval = pcapng_create( handle, 1078f3e7eeaSChristopher Kilgour filename, 1088f3e7eeaSChristopher Kilgour (const option_header *) &libbtbb_section_options, 1098f3e7eeaSChristopher Kilgour (size_t) getpagesize( ), 1108f3e7eeaSChristopher Kilgour DLT_BLUETOOTH_BREDR_BB, 1110e05cc66SDominic Spill BREDR_MAX_PAYLOAD, 1128f3e7eeaSChristopher Kilgour interface_options, 1138f3e7eeaSChristopher Kilgour (size_t) getpagesize( ) ); 1148f3e7eeaSChristopher Kilgour 1158f3e7eeaSChristopher Kilgour if (retval == PCAPNG_OK) { 1168f3e7eeaSChristopher Kilgour /* if there is no timestamp resolution alread in the 1178f3e7eeaSChristopher Kilgour interface options, record nanosecond resolution */ 1188f3e7eeaSChristopher Kilgour retval = check_and_fix_tsresol( handle, interface_options ); 1198f3e7eeaSChristopher Kilgour 1208f3e7eeaSChristopher Kilgour if (retval != PCAPNG_OK) { 1218f3e7eeaSChristopher Kilgour (void) pcapng_close( handle ); 1228f3e7eeaSChristopher Kilgour } 1238f3e7eeaSChristopher Kilgour } 1248f3e7eeaSChristopher Kilgour 1258f3e7eeaSChristopher Kilgour return retval; 1268f3e7eeaSChristopher Kilgour } 1278f3e7eeaSChristopher Kilgour 1288f3e7eeaSChristopher Kilgour int btbb_pcapng_create_file( const char *filename, 1298f3e7eeaSChristopher Kilgour const char *interface_desc, 1308f3e7eeaSChristopher Kilgour btbb_pcapng_handle ** ph ) 1318f3e7eeaSChristopher Kilgour { 1328f3e7eeaSChristopher Kilgour int retval = PCAPNG_OK; 1338f3e7eeaSChristopher Kilgour PCAPNG_HANDLE * handle = malloc( sizeof(PCAPNG_HANDLE) ); 1348f3e7eeaSChristopher Kilgour if (handle) { 1358f3e7eeaSChristopher Kilgour const option_header * popt = NULL; 1368f3e7eeaSChristopher Kilgour struct { 1378f3e7eeaSChristopher Kilgour option_header header; 1388f3e7eeaSChristopher Kilgour char desc[256]; 1398f3e7eeaSChristopher Kilgour } ifopt = { 1408f3e7eeaSChristopher Kilgour .header = { 1418f3e7eeaSChristopher Kilgour .option_code = IF_DESCRIPTION, 1428f3e7eeaSChristopher Kilgour } 1438f3e7eeaSChristopher Kilgour }; 1448f3e7eeaSChristopher Kilgour if (interface_desc) { 1458f3e7eeaSChristopher Kilgour (void) strncpy( &ifopt.desc[0], interface_desc, 256 ); 1468f3e7eeaSChristopher Kilgour ifopt.desc[255] = '\0'; 1478f3e7eeaSChristopher Kilgour ifopt.header.option_length = strlen( ifopt.desc ); 1488f3e7eeaSChristopher Kilgour popt = (const option_header *) &ifopt; 1498f3e7eeaSChristopher Kilgour } 1508f3e7eeaSChristopher Kilgour 1518f3e7eeaSChristopher Kilgour retval = -create_bredr_capture_file_single_interface( handle, 1528f3e7eeaSChristopher Kilgour filename, 1538f3e7eeaSChristopher Kilgour popt ); 1548f3e7eeaSChristopher Kilgour if (retval == PCAPNG_OK) { 1558f3e7eeaSChristopher Kilgour *ph = (btbb_pcapng_handle *) handle; 1568f3e7eeaSChristopher Kilgour } 1578f3e7eeaSChristopher Kilgour else { 1588f3e7eeaSChristopher Kilgour free( handle ); 1598f3e7eeaSChristopher Kilgour } 1608f3e7eeaSChristopher Kilgour } 1618f3e7eeaSChristopher Kilgour else { 1628f3e7eeaSChristopher Kilgour retval = -PCAPNG_NO_MEMORY; 1638f3e7eeaSChristopher Kilgour } 1648f3e7eeaSChristopher Kilgour return retval; 1658f3e7eeaSChristopher Kilgour } 1668f3e7eeaSChristopher Kilgour 1678f3e7eeaSChristopher Kilgour static PCAPNG_RESULT 1688f3e7eeaSChristopher Kilgour append_bredr_packet( PCAPNG_HANDLE * handle, 1698f3e7eeaSChristopher Kilgour pcapng_bredr_packet * pkt ) 1708f3e7eeaSChristopher Kilgour { 1718f3e7eeaSChristopher Kilgour return pcapng_append_packet( handle, ( const enhanced_packet_block *) pkt ); 1728f3e7eeaSChristopher Kilgour } 1738f3e7eeaSChristopher Kilgour 1748f3e7eeaSChristopher Kilgour static void 1758f3e7eeaSChristopher Kilgour assemble_pcapng_bredr_packet( pcapng_bredr_packet * pkt, 1768f3e7eeaSChristopher Kilgour const uint32_t interface_id, 1778f3e7eeaSChristopher Kilgour const uint64_t ns, 1788f3e7eeaSChristopher Kilgour const uint32_t caplen, 1798f3e7eeaSChristopher Kilgour const uint8_t rf_channel, 1808f3e7eeaSChristopher Kilgour const int8_t signal_power, 1818f3e7eeaSChristopher Kilgour const int8_t noise_power, 1828f3e7eeaSChristopher Kilgour const uint8_t access_code_offenses, 1838f3e7eeaSChristopher Kilgour const uint8_t payload_transport, 1848f3e7eeaSChristopher Kilgour const uint8_t payload_rate, 1858f3e7eeaSChristopher Kilgour const uint8_t corrected_header_bits, 1868f3e7eeaSChristopher Kilgour const int16_t corrected_payload_bits, 1878f3e7eeaSChristopher Kilgour const uint32_t lap, 1888f3e7eeaSChristopher Kilgour const uint32_t ref_lap, 1898f3e7eeaSChristopher Kilgour const uint8_t ref_uap, 1908f3e7eeaSChristopher Kilgour const uint32_t bt_header, 1918f3e7eeaSChristopher Kilgour const uint16_t flags, 1920e05cc66SDominic Spill const char * payload ) 1938f3e7eeaSChristopher Kilgour { 1948f3e7eeaSChristopher Kilgour uint32_t pcapng_caplen = sizeof(pcap_bluetooth_bredr_bb_header)+caplen; 1958f3e7eeaSChristopher Kilgour uint32_t block_length = 4*((36+pcapng_caplen+3)/4); 1968f3e7eeaSChristopher Kilgour uint32_t reflapuap = (ref_lap&0xffffff) | (ref_uap<<24); 1978f3e7eeaSChristopher Kilgour 1988f3e7eeaSChristopher Kilgour pkt->blk_header.block_type = BLOCK_TYPE_ENHANCED_PACKET; 1998f3e7eeaSChristopher Kilgour pkt->blk_header.block_total_length = block_length; 2008f3e7eeaSChristopher Kilgour pkt->blk_header.interface_id = interface_id; 2018f3e7eeaSChristopher Kilgour pkt->blk_header.timestamp_high = (uint32_t) (ns >> 32); 2028f3e7eeaSChristopher Kilgour pkt->blk_header.timestamp_low = (uint32_t) (ns & 0x0ffffffffull); 2038f3e7eeaSChristopher Kilgour pkt->blk_header.captured_len = pcapng_caplen; 2048f3e7eeaSChristopher Kilgour pkt->blk_header.packet_len = pcapng_caplen; 2058f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.rf_channel = rf_channel; 2068f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.signal_power = signal_power; 2078f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.noise_power = noise_power; 2088f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.access_code_offenses = access_code_offenses; 2098f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.payload_transport_rate = 2108f3e7eeaSChristopher Kilgour (payload_transport << 4) | payload_rate; 2118f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.corrected_header_bits = corrected_header_bits; 2128f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.corrected_payload_bits = htole16( corrected_payload_bits ); 2138f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.lap = htole32( lap ); 2148f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.ref_lap_uap = htole32( reflapuap ); 2158f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.bt_header = htole16( bt_header ); 2168f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.flags = htole16( flags ); 2178f3e7eeaSChristopher Kilgour if (caplen) { 2188f3e7eeaSChristopher Kilgour (void) memcpy( &pkt->bredr_payload[0], payload, caplen ); 2198f3e7eeaSChristopher Kilgour } 2208f3e7eeaSChristopher Kilgour else { 2218f3e7eeaSChristopher Kilgour pkt->bredr_bb_header.flags &= htole16( ~BREDR_PAYLOAD_PRESENT ); 2228f3e7eeaSChristopher Kilgour } 2238f3e7eeaSChristopher Kilgour ((uint32_t *)pkt)[block_length/4-2] = 0x00000000; /* no-options */ 2248f3e7eeaSChristopher Kilgour ((uint32_t *)pkt)[block_length/4-1] = block_length; 2258f3e7eeaSChristopher Kilgour } 2268f3e7eeaSChristopher Kilgour 2278f3e7eeaSChristopher Kilgour int btbb_pcapng_append_packet(btbb_pcapng_handle * h, const uint64_t ns, 2288f3e7eeaSChristopher Kilgour const int8_t sigdbm, const int8_t noisedbm, 2298f3e7eeaSChristopher Kilgour const uint32_t reflap, const uint8_t refuap, 2308f3e7eeaSChristopher Kilgour const btbb_packet *pkt) 2318f3e7eeaSChristopher Kilgour { 2328f3e7eeaSChristopher Kilgour uint16_t flags = BREDR_DEWHITENED | BREDR_SIGPOWER_VALID | 2338f3e7eeaSChristopher Kilgour ((noisedbm < sigdbm) ? BREDR_NOISEPOWER_VALID : 0) | 2348f3e7eeaSChristopher Kilgour ((reflap != LAP_ANY) ? BREDR_REFLAP_VALID : 0) | 2358f3e7eeaSChristopher Kilgour ((refuap != UAP_ANY) ? BREDR_REFUAP_VALID : 0); 2360e05cc66SDominic Spill int caplen = btbb_packet_get_payload_length(pkt); 2370e05cc66SDominic Spill char payload_bytes[caplen]; 2380e05cc66SDominic Spill btbb_get_payload_packed( pkt, &payload_bytes[0] ); 2390e05cc66SDominic Spill caplen = MIN(BREDR_MAX_PAYLOAD, caplen); 2408f3e7eeaSChristopher Kilgour pcapng_bredr_packet pcapng_pkt; 2418f3e7eeaSChristopher Kilgour assemble_pcapng_bredr_packet( &pcapng_pkt, 2428f3e7eeaSChristopher Kilgour 0, 2438f3e7eeaSChristopher Kilgour ns, 2448f3e7eeaSChristopher Kilgour caplen, 2458f3e7eeaSChristopher Kilgour btbb_packet_get_channel(pkt), 2468f3e7eeaSChristopher Kilgour sigdbm, 2478f3e7eeaSChristopher Kilgour noisedbm, 2488f3e7eeaSChristopher Kilgour btbb_packet_get_ac_errors(pkt), 249*f83b85cfSDominic Spill btbb_packet_get_transport(pkt), 250*f83b85cfSDominic Spill btbb_packet_get_modulation(pkt), 2518f3e7eeaSChristopher Kilgour 0, /* TODO: corrected header bits */ 2528f3e7eeaSChristopher Kilgour 0, /* TODO: corrected payload bits */ 2538f3e7eeaSChristopher Kilgour btbb_packet_get_lap(pkt), 2548f3e7eeaSChristopher Kilgour reflap, 2558f3e7eeaSChristopher Kilgour refuap, 2568f3e7eeaSChristopher Kilgour btbb_packet_get_header_packed(pkt), 2578f3e7eeaSChristopher Kilgour flags, 2588f3e7eeaSChristopher Kilgour payload_bytes ); 2598f3e7eeaSChristopher Kilgour return -append_bredr_packet( (PCAPNG_HANDLE *)h, &pcapng_pkt ); 2608f3e7eeaSChristopher Kilgour } 2618f3e7eeaSChristopher Kilgour 2628f3e7eeaSChristopher Kilgour static PCAPNG_RESULT 2638f3e7eeaSChristopher Kilgour record_bd_addr_info( PCAPNG_HANDLE * handle, 2648f3e7eeaSChristopher Kilgour const uint64_t bd_addr, 2658f3e7eeaSChristopher Kilgour const uint8_t uap_mask, 2668f3e7eeaSChristopher Kilgour const uint8_t nap_valid ) 2678f3e7eeaSChristopher Kilgour { 2688f3e7eeaSChristopher Kilgour const bredr_br_addr_option bdopt = { 2698f3e7eeaSChristopher Kilgour .header = { 2708f3e7eeaSChristopher Kilgour .option_code = PCAPNG_BREDR_OPTION_BD_ADDR, 2718f3e7eeaSChristopher Kilgour .option_length = sizeof(bredr_br_addr_option), 2728f3e7eeaSChristopher Kilgour }, 2738f3e7eeaSChristopher Kilgour .bd_addr_info = { 2748f3e7eeaSChristopher Kilgour .bd_addr = { 2758f3e7eeaSChristopher Kilgour ((bd_addr>>0) & 0xff), 2768f3e7eeaSChristopher Kilgour ((bd_addr>>8) & 0xff), 2778f3e7eeaSChristopher Kilgour ((bd_addr>>16) & 0xff), 2788f3e7eeaSChristopher Kilgour ((bd_addr>>24) & 0xff), 2798f3e7eeaSChristopher Kilgour ((bd_addr>>32) & 0xff), 2808f3e7eeaSChristopher Kilgour ((bd_addr>>40) & 0xff) 2818f3e7eeaSChristopher Kilgour }, 2828f3e7eeaSChristopher Kilgour .uap_mask = uap_mask, 2838f3e7eeaSChristopher Kilgour .nap_valid = nap_valid, 2848f3e7eeaSChristopher Kilgour } 2858f3e7eeaSChristopher Kilgour }; 2868f3e7eeaSChristopher Kilgour return pcapng_append_interface_option( handle, 2878f3e7eeaSChristopher Kilgour (const option_header *) &bdopt ); 2888f3e7eeaSChristopher Kilgour } 2898f3e7eeaSChristopher Kilgour 2908f3e7eeaSChristopher Kilgour int btbb_pcapng_record_bdaddr(btbb_pcapng_handle * h, const uint64_t bdaddr, 2918f3e7eeaSChristopher Kilgour const uint8_t uapmask, const uint8_t napvalid) 2928f3e7eeaSChristopher Kilgour { 2938f3e7eeaSChristopher Kilgour return -record_bd_addr_info( (PCAPNG_HANDLE *) h, 2948f3e7eeaSChristopher Kilgour bdaddr, uapmask, napvalid ); 2958f3e7eeaSChristopher Kilgour } 2968f3e7eeaSChristopher Kilgour 2978f3e7eeaSChristopher Kilgour static PCAPNG_RESULT 2988f3e7eeaSChristopher Kilgour record_bredr_master_clock_info( PCAPNG_HANDLE * handle, 2998f3e7eeaSChristopher Kilgour const uint64_t bd_addr, 3008f3e7eeaSChristopher Kilgour const uint64_t ns, 3018f3e7eeaSChristopher Kilgour const uint32_t clk, 3028f3e7eeaSChristopher Kilgour const uint32_t clk_mask) 3038f3e7eeaSChristopher Kilgour { 3048f3e7eeaSChristopher Kilgour const bredr_clk_option mcopt = { 3058f3e7eeaSChristopher Kilgour .header = { 3068f3e7eeaSChristopher Kilgour .option_code = PCAPNG_BREDR_OPTION_MASTER_CLOCK_INFO, 3078f3e7eeaSChristopher Kilgour .option_length = sizeof(bredr_clk_option) 3088f3e7eeaSChristopher Kilgour }, 3098f3e7eeaSChristopher Kilgour .clock_info = { 3108f3e7eeaSChristopher Kilgour .ts = ns, 3118f3e7eeaSChristopher Kilgour .lap_uap = htole32(bd_addr & 0xffffffff), 3128f3e7eeaSChristopher Kilgour .clk = clk, 3138f3e7eeaSChristopher Kilgour .clk_mask = clk_mask 3148f3e7eeaSChristopher Kilgour } 3158f3e7eeaSChristopher Kilgour }; 3168f3e7eeaSChristopher Kilgour return pcapng_append_interface_option( handle, 3178f3e7eeaSChristopher Kilgour (const option_header *) &mcopt ); 3188f3e7eeaSChristopher Kilgour } 3198f3e7eeaSChristopher Kilgour 3208f3e7eeaSChristopher Kilgour int btbb_pcapng_record_btclock(btbb_pcapng_handle * h, const uint64_t bdaddr, 3218f3e7eeaSChristopher Kilgour const uint64_t ns, const uint32_t clk, 3228f3e7eeaSChristopher Kilgour const uint32_t clkmask) 3238f3e7eeaSChristopher Kilgour { 3248f3e7eeaSChristopher Kilgour return -record_bredr_master_clock_info( (PCAPNG_HANDLE *) h, 3258f3e7eeaSChristopher Kilgour bdaddr, ns, clk, clkmask ); 3268f3e7eeaSChristopher Kilgour } 3278f3e7eeaSChristopher Kilgour 3288f3e7eeaSChristopher Kilgour int btbb_pcapng_close(btbb_pcapng_handle * h) 3298f3e7eeaSChristopher Kilgour { 3308f3e7eeaSChristopher Kilgour pcapng_close( (PCAPNG_HANDLE *) h ); 3318f3e7eeaSChristopher Kilgour if (h) { 3328f3e7eeaSChristopher Kilgour free( h ); 3338f3e7eeaSChristopher Kilgour } 3348f3e7eeaSChristopher Kilgour return -PCAPNG_INVALID_HANDLE; 3358f3e7eeaSChristopher Kilgour } 3368f3e7eeaSChristopher Kilgour 3378f3e7eeaSChristopher Kilgour /* --------------------------------- Low Energy ---------------------------- */ 3388f3e7eeaSChristopher Kilgour 3398f3e7eeaSChristopher Kilgour static PCAPNG_RESULT 3408f3e7eeaSChristopher Kilgour create_le_capture_file_single_interface( PCAPNG_HANDLE * handle, 3418f3e7eeaSChristopher Kilgour const char * filename, 3428f3e7eeaSChristopher Kilgour const option_header * interface_options ) 3438f3e7eeaSChristopher Kilgour { 3448f3e7eeaSChristopher Kilgour PCAPNG_RESULT retval = PCAPNG_OK; 3458f3e7eeaSChristopher Kilgour 3468f3e7eeaSChristopher Kilgour retval = pcapng_create( handle, 3478f3e7eeaSChristopher Kilgour filename, 3488f3e7eeaSChristopher Kilgour (const option_header *) &libbtbb_section_options, 3498f3e7eeaSChristopher Kilgour (size_t) getpagesize( ), 3508f3e7eeaSChristopher Kilgour DLT_BLUETOOTH_LE_LL_WITH_PHDR, 3518f3e7eeaSChristopher Kilgour 64, 3528f3e7eeaSChristopher Kilgour interface_options, 3538f3e7eeaSChristopher Kilgour (size_t) getpagesize( ) ); 3548f3e7eeaSChristopher Kilgour 3558f3e7eeaSChristopher Kilgour if (retval == PCAPNG_OK) { 3568f3e7eeaSChristopher Kilgour /* if there is no timestamp resolution alread in the 3578f3e7eeaSChristopher Kilgour interface options, record nanosecond resolution */ 3588f3e7eeaSChristopher Kilgour retval = check_and_fix_tsresol( handle, interface_options ); 3598f3e7eeaSChristopher Kilgour 3608f3e7eeaSChristopher Kilgour if (retval != PCAPNG_OK) { 3618f3e7eeaSChristopher Kilgour (void) pcapng_close( handle ); 3628f3e7eeaSChristopher Kilgour } 3638f3e7eeaSChristopher Kilgour } 3648f3e7eeaSChristopher Kilgour 3658f3e7eeaSChristopher Kilgour return retval; 3668f3e7eeaSChristopher Kilgour } 3678f3e7eeaSChristopher Kilgour 3688f3e7eeaSChristopher Kilgour int 3698f3e7eeaSChristopher Kilgour lell_pcapng_create_file(const char *filename, const char *interface_desc, 3708f3e7eeaSChristopher Kilgour lell_pcapng_handle ** ph) 3718f3e7eeaSChristopher Kilgour { 3728f3e7eeaSChristopher Kilgour int retval = PCAPNG_OK; 3738f3e7eeaSChristopher Kilgour PCAPNG_HANDLE * handle = malloc( sizeof(PCAPNG_HANDLE) ); 3748f3e7eeaSChristopher Kilgour if (handle) { 3758f3e7eeaSChristopher Kilgour const option_header * popt = NULL; 3768f3e7eeaSChristopher Kilgour struct { 3778f3e7eeaSChristopher Kilgour option_header header; 3788f3e7eeaSChristopher Kilgour char desc[256]; 3798f3e7eeaSChristopher Kilgour } ifopt = { 3808f3e7eeaSChristopher Kilgour .header = { 3818f3e7eeaSChristopher Kilgour .option_code = IF_DESCRIPTION, 3828f3e7eeaSChristopher Kilgour } 3838f3e7eeaSChristopher Kilgour }; 3848f3e7eeaSChristopher Kilgour if (interface_desc) { 3858f3e7eeaSChristopher Kilgour (void) strncpy( &ifopt.desc[0], interface_desc, 256 ); 3868f3e7eeaSChristopher Kilgour ifopt.desc[255] = '\0'; 3878f3e7eeaSChristopher Kilgour ifopt.header.option_length = strlen( ifopt.desc ); 3888f3e7eeaSChristopher Kilgour popt = (const option_header *) &ifopt; 3898f3e7eeaSChristopher Kilgour } 3908f3e7eeaSChristopher Kilgour 3918f3e7eeaSChristopher Kilgour retval = -create_le_capture_file_single_interface( handle, 3928f3e7eeaSChristopher Kilgour filename, 3938f3e7eeaSChristopher Kilgour popt ); 3948f3e7eeaSChristopher Kilgour if (retval == PCAPNG_OK) { 3958f3e7eeaSChristopher Kilgour *ph = (lell_pcapng_handle *) handle; 3968f3e7eeaSChristopher Kilgour } 3978f3e7eeaSChristopher Kilgour else { 3988f3e7eeaSChristopher Kilgour free( handle ); 3998f3e7eeaSChristopher Kilgour } 4008f3e7eeaSChristopher Kilgour } 4018f3e7eeaSChristopher Kilgour else { 4028f3e7eeaSChristopher Kilgour retval = -PCAPNG_NO_MEMORY; 4038f3e7eeaSChristopher Kilgour } 4048f3e7eeaSChristopher Kilgour return retval; 4058f3e7eeaSChristopher Kilgour } 4068f3e7eeaSChristopher Kilgour 4078f3e7eeaSChristopher Kilgour static PCAPNG_RESULT 4088f3e7eeaSChristopher Kilgour append_le_packet( PCAPNG_HANDLE * handle, 4098f3e7eeaSChristopher Kilgour pcapng_le_packet * pkt ) 4108f3e7eeaSChristopher Kilgour { 4118f3e7eeaSChristopher Kilgour return pcapng_append_packet( handle, ( const enhanced_packet_block *) pkt ); 4128f3e7eeaSChristopher Kilgour } 4138f3e7eeaSChristopher Kilgour 4148f3e7eeaSChristopher Kilgour static void 4158f3e7eeaSChristopher Kilgour assemble_pcapng_le_packet( pcapng_le_packet * pkt, 4168f3e7eeaSChristopher Kilgour const uint32_t interface_id, 4178f3e7eeaSChristopher Kilgour const uint64_t ns, 4188f3e7eeaSChristopher Kilgour const uint32_t caplen, 4198f3e7eeaSChristopher Kilgour const uint8_t rf_channel, 4208f3e7eeaSChristopher Kilgour const int8_t signal_power, 4218f3e7eeaSChristopher Kilgour const int8_t noise_power, 4228f3e7eeaSChristopher Kilgour const uint8_t access_address_offenses, 4238f3e7eeaSChristopher Kilgour const uint32_t ref_access_address, 4248f3e7eeaSChristopher Kilgour const uint16_t flags, 4258f3e7eeaSChristopher Kilgour const uint8_t * lepkt ) 4268f3e7eeaSChristopher Kilgour { 4278f3e7eeaSChristopher Kilgour uint32_t pcapng_caplen = sizeof(pcap_bluetooth_le_ll_header)+caplen; 4288f3e7eeaSChristopher Kilgour uint32_t block_length = 4*((36+pcapng_caplen+3)/4); 4298f3e7eeaSChristopher Kilgour 4308f3e7eeaSChristopher Kilgour pkt->blk_header.block_type = BLOCK_TYPE_ENHANCED_PACKET; 4318f3e7eeaSChristopher Kilgour pkt->blk_header.block_total_length = block_length; 4328f3e7eeaSChristopher Kilgour pkt->blk_header.interface_id = interface_id; 4338f3e7eeaSChristopher Kilgour pkt->blk_header.timestamp_high = (uint32_t) (ns >> 32); 4348f3e7eeaSChristopher Kilgour pkt->blk_header.timestamp_low = (uint32_t) (ns & 0x0ffffffffull); 4358f3e7eeaSChristopher Kilgour pkt->blk_header.captured_len = pcapng_caplen; 4368f3e7eeaSChristopher Kilgour pkt->blk_header.packet_len = pcapng_caplen; 4378f3e7eeaSChristopher Kilgour pkt->le_ll_header.rf_channel = rf_channel; 4388f3e7eeaSChristopher Kilgour pkt->le_ll_header.signal_power = signal_power; 4398f3e7eeaSChristopher Kilgour pkt->le_ll_header.noise_power = noise_power; 4408f3e7eeaSChristopher Kilgour pkt->le_ll_header.access_address_offenses = access_address_offenses; 4418f3e7eeaSChristopher Kilgour pkt->le_ll_header.ref_access_address = htole32( ref_access_address ); 4428f3e7eeaSChristopher Kilgour pkt->le_ll_header.flags = htole16( flags ); 4438f3e7eeaSChristopher Kilgour (void) memcpy( &pkt->le_packet[0], lepkt, caplen ); 4448f3e7eeaSChristopher Kilgour ((uint32_t *)pkt)[block_length/4-2] = 0x00000000; /* no-options */ 4458f3e7eeaSChristopher Kilgour ((uint32_t *)pkt)[block_length/4-1] = block_length; 4468f3e7eeaSChristopher Kilgour } 4478f3e7eeaSChristopher Kilgour 4488f3e7eeaSChristopher Kilgour int 4498f3e7eeaSChristopher Kilgour lell_pcapng_append_packet(lell_pcapng_handle * h, const uint64_t ns, 4508f3e7eeaSChristopher Kilgour const int8_t sigdbm, const int8_t noisedbm, 4518f3e7eeaSChristopher Kilgour const uint32_t refAA, const lell_packet *pkt) 4528f3e7eeaSChristopher Kilgour { 4538f3e7eeaSChristopher Kilgour uint16_t flags = LE_DEWHITENED | LE_AA_OFFENSES_VALID | 4548f3e7eeaSChristopher Kilgour LE_SIGPOWER_VALID | 4558f3e7eeaSChristopher Kilgour ((noisedbm < sigdbm) ? LE_NOISEPOWER_VALID : 0) | 4568f3e7eeaSChristopher Kilgour (lell_packet_is_data(pkt) ? 0 : LE_REF_AA_VALID); 4578f3e7eeaSChristopher Kilgour pcapng_le_packet pcapng_pkt; 4588f3e7eeaSChristopher Kilgour assemble_pcapng_le_packet( &pcapng_pkt, 4598f3e7eeaSChristopher Kilgour 0, 4608f3e7eeaSChristopher Kilgour ns, 4618f3e7eeaSChristopher Kilgour 9+pkt->length, 4628f3e7eeaSChristopher Kilgour pkt->channel_k, 4638f3e7eeaSChristopher Kilgour sigdbm, 4648f3e7eeaSChristopher Kilgour noisedbm, 4658f3e7eeaSChristopher Kilgour pkt->access_address_offenses, 4668f3e7eeaSChristopher Kilgour refAA, 4678f3e7eeaSChristopher Kilgour flags, 4688f3e7eeaSChristopher Kilgour &pkt->symbols[0] ); 4698f3e7eeaSChristopher Kilgour int retval = -append_le_packet( (PCAPNG_HANDLE *) h, &pcapng_pkt ); 4708f3e7eeaSChristopher Kilgour if ((retval == 0) && !lell_packet_is_data(pkt) && (pkt->adv_type == CONNECT_REQ)) { 4718f3e7eeaSChristopher Kilgour (void) lell_pcapng_record_connect_req(h, ns, &pkt->symbols[0]); 4728f3e7eeaSChristopher Kilgour } 4738f3e7eeaSChristopher Kilgour return retval; 4748f3e7eeaSChristopher Kilgour } 4758f3e7eeaSChristopher Kilgour 4768f3e7eeaSChristopher Kilgour static PCAPNG_RESULT 4778f3e7eeaSChristopher Kilgour record_le_connect_req_info( PCAPNG_HANDLE * handle, 4788f3e7eeaSChristopher Kilgour const uint64_t ns, 4798f3e7eeaSChristopher Kilgour const uint8_t * pdu ) 4808f3e7eeaSChristopher Kilgour { 4818f3e7eeaSChristopher Kilgour le_ll_connection_info_option cropt = { 4828f3e7eeaSChristopher Kilgour .header = { 4838f3e7eeaSChristopher Kilgour .option_code = PCAPNG_LE_LL_CONNECTION_INFO, 4848f3e7eeaSChristopher Kilgour .option_length = sizeof(le_ll_connection_info_option) 4858f3e7eeaSChristopher Kilgour }, 4868f3e7eeaSChristopher Kilgour .connection_info = { 4878f3e7eeaSChristopher Kilgour .ns = ns 4888f3e7eeaSChristopher Kilgour } 4898f3e7eeaSChristopher Kilgour }; 4908f3e7eeaSChristopher Kilgour (void) memcpy( &cropt.connection_info.pdu.bytes[0], pdu, 34 ); 4918f3e7eeaSChristopher Kilgour return pcapng_append_interface_option( handle, 4928f3e7eeaSChristopher Kilgour (const option_header *) &cropt ); 4938f3e7eeaSChristopher Kilgour } 4948f3e7eeaSChristopher Kilgour 4958f3e7eeaSChristopher Kilgour int 4968f3e7eeaSChristopher Kilgour lell_pcapng_record_connect_req(lell_pcapng_handle * h, const uint64_t ns, 4978f3e7eeaSChristopher Kilgour const uint8_t * pdu) 4988f3e7eeaSChristopher Kilgour { 4998f3e7eeaSChristopher Kilgour return -record_le_connect_req_info( (PCAPNG_HANDLE *) h, ns, pdu ); 5008f3e7eeaSChristopher Kilgour } 5018f3e7eeaSChristopher Kilgour 5028f3e7eeaSChristopher Kilgour int lell_pcapng_close(lell_pcapng_handle *h) 5038f3e7eeaSChristopher Kilgour { 5048f3e7eeaSChristopher Kilgour pcapng_close( (PCAPNG_HANDLE *) h ); 5058f3e7eeaSChristopher Kilgour if (h) { 5068f3e7eeaSChristopher Kilgour free( h ); 5078f3e7eeaSChristopher Kilgour } 5088f3e7eeaSChristopher Kilgour return -PCAPNG_INVALID_HANDLE; 5098f3e7eeaSChristopher Kilgour } 510