xref: /libbtbb/lib/src/pcap.c (revision 4de3c54fb3f2a85ca6030783e4c0cc0ac45dfa47)
1 /* -*- c -*- */
2 /*
3  * Copyright 2014 Christopher D. Kilgour techie AT whiterocker.com
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 #include "bluetooth_le_packet.h"
23 #include "bluetooth_packet.h"
24 #include "btbb.h"
25 #include "pcap-common.h"
26 
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 
31 typedef enum {
32 	PCAP_OK = 0,
33 	PCAP_INVALID_HANDLE,
34 	PCAP_FILE_NOT_ALLOWED,
35 	PCAP_NO_MEMORY,
36 } PCAP_RESULT;
37 
38 typedef struct __attribute__((packed)) pcap_hdr_s {
39 	uint32_t magic_number;   /* magic number */
40 	uint16_t version_major;  /* major version number */
41 	uint16_t version_minor;  /* minor version number */
42 	int32_t  thiszone;       /* GMT to local correction */
43 	uint32_t sigfigs;        /* accuracy of timestamps */
44 	uint32_t snaplen;        /* max length of captured packets, in octets */
45 	uint32_t network;        /* data link type */
46 } pcap_hdr_t;
47 
48 FILE *btbb_pcap_open(const char *filename, uint32_t dlt, uint32_t snaplen) {
49 	pcap_hdr_t pcap_header = {
50 		.magic_number = 0xa1b2c3d4,
51 		.version_major = 2,
52 		.version_minor = 4,
53 		.thiszone = 0,
54 		.sigfigs = 0,
55 		.snaplen = snaplen,
56 		.network = dlt,
57 	};
58 
59 	FILE *pcap_file = fopen(filename, "w");
60 	if (pcap_file == NULL) return NULL;
61 
62 	fwrite(&pcap_header, sizeof(pcap_header), 1, pcap_file);
63 
64 	return pcap_file;
65 }
66 
67 /* BT BR/EDR support */
68 
69 struct btbb_pcap_handle {
70 	FILE *pcap_file;
71 };
72 
73 int
74 btbb_pcap_create_file(const char *filename, btbb_pcap_handle ** ph)
75 {
76 	int retval = 0;
77 	btbb_pcap_handle * handle = malloc( sizeof(btbb_pcap_handle) );
78 	if (handle) {
79 		memset(handle, 0, sizeof(*handle));
80 		handle->pcap_file = btbb_pcap_open(filename, DLT_BLUETOOTH_BREDR_BB,
81 											BREDR_MAX_PAYLOAD);
82 		if (handle->pcap_file) {
83 			*ph = handle;
84 		}
85 		else {
86 			perror("PCAP error:");
87 			retval = -PCAP_FILE_NOT_ALLOWED;
88 			goto fail;
89 		}
90 	}
91 	else {
92 		retval = -PCAP_NO_MEMORY;
93 		goto fail;
94 	}
95 	return retval;
96 fail:
97 	(void) btbb_pcap_close( handle );
98 	return retval;
99 }
100 
101 typedef struct __attribute__((packed)) pcaprec_hdr_s {
102 	uint32_t ts_sec;         /* timestamp seconds */
103 	uint32_t ts_usec;        /* timestamp microseconds */
104 	uint32_t incl_len;       /* number of octets of packet saved in file */
105 	uint32_t orig_len;       /* actual length of packet */
106 } pcaprec_hdr_t;
107 
108 typedef struct {
109 	pcaprec_hdr_t pcap_header;
110 	pcap_bluetooth_bredr_bb_header bredr_bb_header;
111 } pcap_bredr_packet;
112 
113 void btbb_pcap_dump(FILE *file, pcaprec_hdr_t *pcap_header, u_char *data) {
114 	fwrite(pcap_header, sizeof(*pcap_header), 1, file);
115 	fwrite(data, pcap_header->incl_len, 1, file);
116 	fflush(file);
117 }
118 
119 static void
120 assemble_pcapng_bredr_packet( pcap_bredr_packet * pkt,
121 			      const uint32_t interface_id __attribute__((unused)),
122 			      const uint64_t ns,
123 			      const uint32_t caplen,
124 			      const uint8_t rf_channel,
125 			      const int8_t signal_power,
126 			      const int8_t noise_power,
127 			      const uint8_t access_code_offenses,
128 			      const uint8_t payload_transport,
129 			      const uint8_t payload_rate,
130 			      const uint8_t corrected_header_bits,
131 			      const int16_t corrected_payload_bits,
132 			      const uint32_t lap,
133 			      const uint32_t ref_lap,
134 			      const uint8_t ref_uap,
135 			      const uint32_t bt_header,
136 			      const uint16_t flags,
137 			      const uint8_t * payload )
138 {
139 	uint32_t pcap_caplen = sizeof(pcap_bluetooth_bredr_bb_header) -
140 				sizeof(pkt->bredr_bb_header.bredr_payload)
141 				+ caplen;
142 	uint32_t reflapuap = (ref_lap&0xffffff) | (ref_uap<<24);
143 
144 	pkt->pcap_header.ts_sec  = ns / 1000000000ull;
145 	pkt->pcap_header.ts_usec = ns % 1000000000ull;
146 	pkt->pcap_header.incl_len = pcap_caplen;
147 	pkt->pcap_header.orig_len = pcap_caplen;
148 
149 	pkt->bredr_bb_header.rf_channel = rf_channel;
150 	pkt->bredr_bb_header.signal_power = signal_power;
151 	pkt->bredr_bb_header.noise_power = noise_power;
152 	pkt->bredr_bb_header.access_code_offenses = access_code_offenses;
153 	pkt->bredr_bb_header.payload_transport_rate =
154 		(payload_transport << 4) | payload_rate;
155 	pkt->bredr_bb_header.corrected_header_bits = corrected_header_bits;
156 	pkt->bredr_bb_header.corrected_payload_bits = htole16( corrected_payload_bits );
157 	pkt->bredr_bb_header.lap = htole32( lap );
158 	pkt->bredr_bb_header.ref_lap_uap = htole32( reflapuap );
159 	pkt->bredr_bb_header.bt_header = htole32( bt_header );
160 	pkt->bredr_bb_header.flags = htole16( flags );
161 	if (caplen) {
162 		assert(caplen <= sizeof(pkt->bredr_bb_header.bredr_payload)); // caller ensures this, but to be safe..
163 		(void) memcpy( &pkt->bredr_bb_header.bredr_payload[0], payload, caplen );
164 	}
165 	else {
166 		pkt->bredr_bb_header.flags &= htole16( ~BREDR_PAYLOAD_PRESENT );
167 	}
168 }
169 
170 int
171 btbb_pcap_append_packet(btbb_pcap_handle * h, const uint64_t ns,
172 			const int8_t sigdbm, const int8_t noisedbm,
173 			const uint32_t reflap, const uint8_t refuap,
174 			const btbb_packet *pkt)
175 {
176 	if (h && h->pcap_file) {
177 		uint16_t flags = BREDR_DEWHITENED | BREDR_SIGPOWER_VALID |
178 			((noisedbm < sigdbm) ? BREDR_NOISEPOWER_VALID : 0) |
179 			((reflap != LAP_ANY) ? BREDR_REFLAP_VALID : 0) |
180 			((refuap != UAP_ANY) ? BREDR_REFUAP_VALID : 0);
181 		uint32_t caplen = (uint32_t) btbb_packet_get_payload_length(pkt);
182 		uint8_t payload_bytes[caplen];
183 		btbb_get_payload_packed( pkt, (char *) &payload_bytes[0] );
184 		caplen = MIN(BREDR_MAX_PAYLOAD, caplen);
185 		pcap_bredr_packet pcap_pkt;
186 		assemble_pcapng_bredr_packet( &pcap_pkt,
187 					      0,
188 					      ns,
189 					      caplen,
190 					      btbb_packet_get_channel(pkt),
191 					      sigdbm,
192 					      noisedbm,
193 					      btbb_packet_get_ac_errors(pkt),
194 						  btbb_packet_get_transport(pkt),
195 						  btbb_packet_get_modulation(pkt),
196 					      0, /* TODO: corrected header bits */
197 					      0, /* TODO: corrected payload bits */
198 					      btbb_packet_get_lap(pkt),
199 					      reflap,
200 					      refuap,
201 					      btbb_packet_get_header_packed(pkt),
202 					      flags,
203 					      payload_bytes );
204 		btbb_pcap_dump(h->pcap_file, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.bredr_bb_header);
205 		return 0;
206 	}
207 	return -PCAP_INVALID_HANDLE;
208 }
209 
210 int
211 btbb_pcap_close(btbb_pcap_handle * h)
212 {
213 	if (h && h->pcap_file) {
214 		fclose(h->pcap_file);
215 	}
216 	if (h) {
217 		free(h);
218 		return 0;
219 	}
220 	return -PCAP_INVALID_HANDLE;
221 }
222 
223 /* BTLE support */
224 
225 struct lell_pcap_handle {
226 	FILE *pcap_file;
227 	int dlt;
228 	uint8_t btle_ppi_version;
229 };
230 
231 static int
232 lell_pcap_create_file_dlt(const char *filename, int dlt, lell_pcap_handle ** ph)
233 {
234 	int retval = 0;
235 	lell_pcap_handle * handle = malloc( sizeof(lell_pcap_handle) );
236 	if (handle) {
237 		memset(handle, 0, sizeof(*handle));
238 		handle->pcap_file = btbb_pcap_open(filename, dlt, BREDR_MAX_PAYLOAD);
239 		if (handle->pcap_file) {
240 			handle->dlt = dlt;
241 			*ph = handle;
242 		}
243 		else {
244 			retval = -PCAP_FILE_NOT_ALLOWED;
245 			goto fail;
246 		}
247 	}
248 	else {
249 		retval = -PCAP_NO_MEMORY;
250 		goto fail;
251 	}
252 	return retval;
253 fail:
254 	(void) lell_pcap_close( handle );
255 	return retval;
256 }
257 
258 int
259 lell_pcap_create_file(const char *filename, lell_pcap_handle ** ph)
260 {
261 	return lell_pcap_create_file_dlt(filename, DLT_BLUETOOTH_LE_LL_WITH_PHDR, ph);
262 }
263 
264 int
265 lell_pcap_ppi_create_file(const char *filename, int btle_ppi_version,
266 			  lell_pcap_handle ** ph)
267 {
268 	int retval = lell_pcap_create_file_dlt(filename, DLT_PPI, ph);
269 	if (!retval) {
270 		(*ph)->btle_ppi_version = btle_ppi_version;
271 	}
272 	return retval;
273 }
274 
275 typedef struct {
276 	pcaprec_hdr_t pcap_header;
277 	pcap_bluetooth_le_ll_header le_ll_header;
278 	uint8_t le_packet[LE_MAX_PAYLOAD];
279 } pcap_le_packet;
280 
281 static void
282 assemble_pcapng_le_packet( pcap_le_packet * pkt,
283 			   const uint32_t interface_id __attribute__((unused)),
284 			   const uint64_t ns,
285 			   const uint32_t caplen,
286 			   const uint8_t rf_channel,
287 			   const int8_t signal_power,
288 			   const int8_t noise_power,
289 			   const uint8_t access_address_offenses,
290 			   const uint32_t ref_access_address,
291 			   const uint16_t flags,
292 			   const uint8_t * lepkt )
293 {
294 	uint32_t incl_len = MIN(LE_MAX_PAYLOAD, caplen);
295 
296 	pkt->pcap_header.ts_sec  = ns / 1000000000ull;
297 	pkt->pcap_header.ts_usec = ns % 1000000000ull;
298 	pkt->pcap_header.incl_len = sizeof(pcap_bluetooth_le_ll_header)+caplen;
299 	pkt->pcap_header.orig_len = sizeof(pcap_bluetooth_le_ll_header)+incl_len;
300 
301 	pkt->le_ll_header.rf_channel = rf_channel;
302 	pkt->le_ll_header.signal_power = signal_power;
303 	pkt->le_ll_header.noise_power = noise_power;
304 	pkt->le_ll_header.access_address_offenses = access_address_offenses;
305 	pkt->le_ll_header.ref_access_address = htole32( ref_access_address );
306 	pkt->le_ll_header.flags = htole16( flags );
307 	(void) memcpy( &pkt->le_packet[0], lepkt, incl_len );
308 }
309 
310 int
311 lell_pcap_append_packet(lell_pcap_handle * h, const uint64_t ns,
312 			const int8_t sigdbm, const int8_t noisedbm,
313 			const uint32_t refAA, const lell_packet *pkt)
314 {
315 	if (h && h->pcap_file &&
316 	    (h->dlt == DLT_BLUETOOTH_LE_LL_WITH_PHDR)) {
317 		uint16_t flags = LE_DEWHITENED | LE_AA_OFFENSES_VALID |
318 			LE_SIGPOWER_VALID |
319 			((noisedbm < sigdbm) ? LE_NOISEPOWER_VALID : 0) |
320 			(lell_packet_is_data(pkt) ? 0 : LE_REF_AA_VALID);
321 		pcap_le_packet pcap_pkt;
322 		assemble_pcapng_le_packet( &pcap_pkt,
323 					   0,
324 					   ns,
325 					   pkt->length + 4 + 2 + 3, // AA + header + CRC
326 					   pkt->channel_k,
327 					   sigdbm,
328 					   noisedbm,
329 					   pkt->access_address_offenses,
330 					   refAA,
331 					   flags,
332 					   &pkt->symbols[0] );
333 		btbb_pcap_dump(h->pcap_file, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.le_ll_header);
334 		return 0;
335 	}
336 	return -PCAP_INVALID_HANDLE;
337 }
338 
339 #define PPI_BTLE 30006
340 
341 typedef struct __attribute__((packed)) {
342 	uint8_t pph_version;
343 	uint8_t pph_flags;
344 	uint16_t pph_len;
345 	uint32_t pph_dlt;
346 } ppi_packet_header_t;
347 
348 typedef struct __attribute__((packed)) {
349 	uint16_t pfh_type;
350 	uint16_t pfh_datalen;
351 } ppi_fieldheader_t;
352 
353 typedef struct __attribute__((packed)) {
354 	uint8_t btle_version;
355 	uint16_t btle_channel;
356 	uint8_t btle_clkn_high;
357 	uint32_t btle_clk100ns;
358 	int8_t rssi_max;
359 	int8_t rssi_min;
360 	int8_t rssi_avg;
361 	uint8_t rssi_count;
362 } ppi_btle_t;
363 
364 typedef struct __attribute__((packed)) {
365 	pcaprec_hdr_t pcap_header;
366         ppi_packet_header_t ppi_packet_header;
367 	ppi_fieldheader_t ppi_fieldheader;
368 	ppi_btle_t le_ll_ppi_header;
369 	uint8_t le_packet[LE_MAX_PAYLOAD];
370 } pcap_ppi_le_packet;
371 
372 int
373 lell_pcap_append_ppi_packet(lell_pcap_handle * h, const uint64_t ns,
374 			    const uint8_t clkn_high,
375 			    const int8_t rssi_min, const int8_t rssi_max,
376 			    const int8_t rssi_avg, const uint8_t rssi_count,
377 			    const lell_packet *pkt)
378 {
379 	if (h && h->pcap_file &&
380 	    (h->dlt == DLT_PPI)) {
381 		pcap_ppi_le_packet pcap_pkt;
382 		const uint16_t pcap_headerlen =
383 			sizeof(ppi_packet_header_t) +
384 			sizeof(ppi_fieldheader_t) +
385 			sizeof(ppi_btle_t);
386 		uint16_t MHz = 2402 + 2*lell_get_channel_k(pkt);
387 		unsigned packet_len = pkt->length + 4 + 2 + 3; // AA + header + CRC
388 		unsigned incl_len   = MIN(LE_MAX_PAYLOAD, packet_len);
389 
390 		pcap_pkt.pcap_header.ts_sec  = ns / 1000000000ull;
391 		pcap_pkt.pcap_header.ts_usec = ns % 1000000000ull;
392 		pcap_pkt.pcap_header.incl_len = pcap_headerlen + incl_len;
393 		pcap_pkt.pcap_header.orig_len = pcap_headerlen + packet_len;
394 
395 		pcap_pkt.ppi_packet_header.pph_version = 0;
396 		pcap_pkt.ppi_packet_header.pph_flags = 0;
397 		pcap_pkt.ppi_packet_header.pph_len = htole16(pcap_headerlen);
398 		pcap_pkt.ppi_packet_header.pph_dlt = htole32(DLT_USER0);
399 
400 		pcap_pkt.ppi_fieldheader.pfh_type = htole16(PPI_BTLE);
401 		pcap_pkt.ppi_fieldheader.pfh_datalen = htole16(sizeof(ppi_btle_t));
402 
403 		pcap_pkt.le_ll_ppi_header.btle_version = h->btle_ppi_version;
404 		pcap_pkt.le_ll_ppi_header.btle_channel = htole16(MHz);
405 		pcap_pkt.le_ll_ppi_header.btle_clkn_high = clkn_high;
406 		pcap_pkt.le_ll_ppi_header.btle_clk100ns = htole32(pkt->clk100ns);
407 		pcap_pkt.le_ll_ppi_header.rssi_max = rssi_max;
408 		pcap_pkt.le_ll_ppi_header.rssi_min = rssi_min;
409 		pcap_pkt.le_ll_ppi_header.rssi_avg = rssi_avg;
410 		pcap_pkt.le_ll_ppi_header.rssi_count = rssi_count;
411 		(void) memcpy( &pcap_pkt.le_packet[0], &pkt->symbols[0], incl_len);
412 		btbb_pcap_dump(h->pcap_file, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.ppi_packet_header);
413 		return 0;
414 	}
415 	return -PCAP_INVALID_HANDLE;
416 }
417 
418 int
419 lell_pcap_close(lell_pcap_handle *h)
420 {
421 	if (h && h->pcap_file) {
422 		fclose(h->pcap_file);
423 	}
424 	if (h) {
425 		free(h);
426 		return 0;
427 	}
428 	return -PCAP_INVALID_HANDLE;
429 }
430