xref: /libbtbb/lib/src/pcap.c (revision b6703d5def81e2b86b3b30523f68ac469a87b403)
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 ts_sec;         /* timestamp seconds */
103 	uint32 ts_usec;        /* timestamp microseconds */
104 	uint32 incl_len;       /* number of octets of packet saved in file */
105 	uint32 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 	uint8_t bredr_payload[BREDR_MAX_PAYLOAD];
112 } pcap_bredr_packet;
113 
114 void btbb_pcap_dump(FILE *file, pcaprec_hdr_t *pcap_header, u_char *data) {
115 	fwrite(pcap_header, sizeof(*pcap_header), 1, file);
116 	fwrite(data, pcap_header->incl_len, 1, file);
117 	fflush(file);
118 }
119 
120 static void
121 assemble_pcapng_bredr_packet( pcap_bredr_packet * pkt,
122 			      const uint32_t interface_id __attribute__((unused)),
123 			      const uint64_t ns,
124 			      const uint32_t caplen,
125 			      const uint8_t rf_channel,
126 			      const int8_t signal_power,
127 			      const int8_t noise_power,
128 			      const uint8_t access_code_offenses,
129 			      const uint8_t payload_transport,
130 			      const uint8_t payload_rate,
131 			      const uint8_t corrected_header_bits,
132 			      const int16_t corrected_payload_bits,
133 			      const uint32_t lap,
134 			      const uint32_t ref_lap,
135 			      const uint8_t ref_uap,
136 			      const uint32_t bt_header,
137 			      const uint16_t flags,
138 			      const uint8_t * payload )
139 {
140 	uint32_t pcap_caplen = sizeof(pcap_bluetooth_bredr_bb_header)+caplen;
141 	uint32_t reflapuap = (ref_lap&0xffffff) | (ref_uap<<24);
142 
143 	pkt->pcap_header.ts_sec  = ns / 1000000000ull;
144 	pkt->pcap_header.ts_usec = ns % 1000000000ull;
145 	pkt->pcap_header.incl_len = pcap_caplen;
146 	pkt->pcap_header.orig_len = pcap_caplen;
147 
148 	pkt->bredr_bb_header.rf_channel = rf_channel;
149 	pkt->bredr_bb_header.signal_power = signal_power;
150 	pkt->bredr_bb_header.noise_power = noise_power;
151 	pkt->bredr_bb_header.access_code_offenses = access_code_offenses;
152 	pkt->bredr_bb_header.payload_transport_rate =
153 		(payload_transport << 4) | payload_rate;
154 	pkt->bredr_bb_header.corrected_header_bits = corrected_header_bits;
155 	pkt->bredr_bb_header.corrected_payload_bits = htole16( corrected_payload_bits );
156 	pkt->bredr_bb_header.lap = htole32( lap );
157 	pkt->bredr_bb_header.ref_lap_uap = htole32( reflapuap );
158 	pkt->bredr_bb_header.bt_header = htole16( bt_header );
159 	pkt->bredr_bb_header.flags = htole16( flags );
160 	if (caplen) {
161 		assert(caplen <= sizeof(pkt->bredr_payload)); // caller ensures this, but to be safe..
162 		(void) memcpy( &pkt->bredr_payload[0], payload, caplen );
163 	}
164 	else {
165 		pkt->bredr_bb_header.flags &= htole16( ~BREDR_PAYLOAD_PRESENT );
166 	}
167 }
168 
169 int
170 btbb_pcap_append_packet(btbb_pcap_handle * h, const uint64_t ns,
171 			const int8_t sigdbm, const int8_t noisedbm,
172 			const uint32_t reflap, const uint8_t refuap,
173 			const btbb_packet *pkt)
174 {
175 	if (h && h->pcap_file) {
176 		uint16_t flags = BREDR_DEWHITENED | BREDR_SIGPOWER_VALID |
177 			((noisedbm < sigdbm) ? BREDR_NOISEPOWER_VALID : 0) |
178 			((reflap != LAP_ANY) ? BREDR_REFLAP_VALID : 0) |
179 			((refuap != UAP_ANY) ? BREDR_REFUAP_VALID : 0);
180 		uint32_t caplen = (uint32_t) btbb_packet_get_payload_length(pkt);
181 		uint8_t payload_bytes[caplen];
182 		btbb_get_payload_packed( pkt, (char *) &payload_bytes[0] );
183 		caplen = MIN(BREDR_MAX_PAYLOAD, caplen);
184 		pcap_bredr_packet pcap_pkt;
185 		assemble_pcapng_bredr_packet( &pcap_pkt,
186 					      0,
187 					      ns,
188 					      caplen,
189 					      btbb_packet_get_channel(pkt),
190 					      sigdbm,
191 					      noisedbm,
192 					      btbb_packet_get_ac_errors(pkt),
193 						  btbb_packet_get_transport(pkt),
194 						  btbb_packet_get_modulation(pkt),
195 					      0, /* TODO: corrected header bits */
196 					      0, /* TODO: corrected payload bits */
197 					      btbb_packet_get_lap(pkt),
198 					      reflap,
199 					      refuap,
200 					      btbb_packet_get_header_packed(pkt),
201 					      flags,
202 					      payload_bytes );
203 		btbb_pcap_dump(h->pcap_file, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.bredr_bb_header);
204 		return 0;
205 	}
206 	return -PCAP_INVALID_HANDLE;
207 }
208 
209 int
210 btbb_pcap_close(btbb_pcap_handle * h)
211 {
212 	if (h && h->pcap_file) {
213 		fclose(h->pcap_file);
214 	}
215 	if (h) {
216 		free(h);
217 		return 0;
218 	}
219 	return -PCAP_INVALID_HANDLE;
220 }
221 
222 /* BTLE support */
223 
224 struct lell_pcap_handle {
225 	FILE *pcap_file;
226 	int dlt;
227 	uint8_t btle_ppi_version;
228 };
229 
230 static int
231 lell_pcap_create_file_dlt(const char *filename, int dlt, lell_pcap_handle ** ph)
232 {
233 	int retval = 0;
234 	lell_pcap_handle * handle = malloc( sizeof(lell_pcap_handle) );
235 	if (handle) {
236 		memset(handle, 0, sizeof(*handle));
237 		handle->pcap_file = btbb_pcap_open(filename, dlt, BREDR_MAX_PAYLOAD);
238 		if (handle->pcap_file) {
239 			handle->dlt = dlt;
240 			*ph = handle;
241 		}
242 		else {
243 			retval = -PCAP_FILE_NOT_ALLOWED;
244 			goto fail;
245 		}
246 	}
247 	else {
248 		retval = -PCAP_NO_MEMORY;
249 		goto fail;
250 	}
251 	return retval;
252 fail:
253 	(void) lell_pcap_close( handle );
254 	return retval;
255 }
256 
257 int
258 lell_pcap_create_file(const char *filename, lell_pcap_handle ** ph)
259 {
260 	return lell_pcap_create_file_dlt(filename, DLT_BLUETOOTH_LE_LL_WITH_PHDR, ph);
261 }
262 
263 int
264 lell_pcap_ppi_create_file(const char *filename, int btle_ppi_version,
265 			  lell_pcap_handle ** ph)
266 {
267 	int retval = lell_pcap_create_file_dlt(filename, DLT_PPI, ph);
268 	if (!retval) {
269 		(*ph)->btle_ppi_version = btle_ppi_version;
270 	}
271 	return retval;
272 }
273 
274 typedef struct {
275 	pcaprec_hdr_t pcap_header;
276 	pcap_bluetooth_le_ll_header le_ll_header;
277 	uint8_t le_packet[LE_MAX_PAYLOAD];
278 } pcap_le_packet;
279 
280 static void
281 assemble_pcapng_le_packet( pcap_le_packet * pkt,
282 			   const uint32_t interface_id __attribute__((unused)),
283 			   const uint64_t ns,
284 			   const uint32_t caplen,
285 			   const uint8_t rf_channel,
286 			   const int8_t signal_power,
287 			   const int8_t noise_power,
288 			   const uint8_t access_address_offenses,
289 			   const uint32_t ref_access_address,
290 			   const uint16_t flags,
291 			   const uint8_t * lepkt )
292 {
293 	uint32_t incl_len = MIN(LE_MAX_PAYLOAD, caplen);
294 
295 	pkt->pcap_header.ts_sec  = ns / 1000000000ull;
296 	pkt->pcap_header.ts_usec = ns % 1000000000ull;
297 	pkt->pcap_header.incl_len = sizeof(pcap_bluetooth_le_ll_header)+caplen;
298 	pkt->pcap_header.orig_len = sizeof(pcap_bluetooth_le_ll_header)+incl_len;
299 
300 	pkt->le_ll_header.rf_channel = rf_channel;
301 	pkt->le_ll_header.signal_power = signal_power;
302 	pkt->le_ll_header.noise_power = noise_power;
303 	pkt->le_ll_header.access_address_offenses = access_address_offenses;
304 	pkt->le_ll_header.ref_access_address = htole32( ref_access_address );
305 	pkt->le_ll_header.flags = htole16( flags );
306 	(void) memcpy( &pkt->le_packet[0], lepkt, incl_len );
307 }
308 
309 int
310 lell_pcap_append_packet(lell_pcap_handle * h, const uint64_t ns,
311 			const int8_t sigdbm, const int8_t noisedbm,
312 			const uint32_t refAA, const lell_packet *pkt)
313 {
314 	if (h && h->pcap_file &&
315 	    (h->dlt == DLT_BLUETOOTH_LE_LL_WITH_PHDR)) {
316 		uint16_t flags = LE_DEWHITENED | LE_AA_OFFENSES_VALID |
317 			LE_SIGPOWER_VALID |
318 			((noisedbm < sigdbm) ? LE_NOISEPOWER_VALID : 0) |
319 			(lell_packet_is_data(pkt) ? 0 : LE_REF_AA_VALID);
320 		pcap_le_packet pcap_pkt;
321 		assemble_pcapng_le_packet( &pcap_pkt,
322 					   0,
323 					   ns,
324 					   pkt->length + 4 + 2 + 3, // AA + header + CRC
325 					   pkt->channel_k,
326 					   sigdbm,
327 					   noisedbm,
328 					   pkt->access_address_offenses,
329 					   refAA,
330 					   flags,
331 					   &pkt->symbols[0] );
332 		btbb_pcap_dump(h->pcap_file, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.le_ll_header);
333 		return 0;
334 	}
335 	return -PCAP_INVALID_HANDLE;
336 }
337 
338 #define PPI_BTLE 30006
339 
340 typedef struct __attribute__((packed)) {
341 	uint8_t pph_version;
342 	uint8_t pph_flags;
343 	uint16_t pph_len;
344 	uint32_t pph_dlt;
345 } ppi_packet_header_t;
346 
347 typedef struct __attribute__((packed)) {
348 	uint16_t pfh_type;
349 	uint16_t pfh_datalen;
350 } ppi_fieldheader_t;
351 
352 typedef struct __attribute__((packed)) {
353 	uint8_t btle_version;
354 	uint16_t btle_channel;
355 	uint8_t btle_clkn_high;
356 	uint32_t btle_clk100ns;
357 	int8_t rssi_max;
358 	int8_t rssi_min;
359 	int8_t rssi_avg;
360 	uint8_t rssi_count;
361 } ppi_btle_t;
362 
363 typedef struct __attribute__((packed)) {
364 	pcaprec_hdr_t pcap_header;
365         ppi_packet_header_t ppi_packet_header;
366 	ppi_fieldheader_t ppi_fieldheader;
367 	ppi_btle_t le_ll_ppi_header;
368 	uint8_t le_packet[LE_MAX_PAYLOAD];
369 } pcap_ppi_le_packet;
370 
371 int
372 lell_pcap_append_ppi_packet(lell_pcap_handle * h, const uint64_t ns,
373 			    const uint8_t clkn_high,
374 			    const int8_t rssi_min, const int8_t rssi_max,
375 			    const int8_t rssi_avg, const uint8_t rssi_count,
376 			    const lell_packet *pkt)
377 {
378 	if (h && h->pcap_file &&
379 	    (h->dlt == DLT_PPI)) {
380 		pcap_ppi_le_packet pcap_pkt;
381 		const uint16_t pcap_headerlen =
382 			sizeof(ppi_packet_header_t) +
383 			sizeof(ppi_fieldheader_t) +
384 			sizeof(ppi_btle_t);
385 		uint16_t MHz = 2402 + 2*lell_get_channel_k(pkt);
386 		unsigned packet_len = pkt->length + 4 + 2 + 3; // AA + header + CRC
387 		unsigned incl_len   = MIN(LE_MAX_PAYLOAD, packet_len);
388 
389 		pcap_pkt.pcap_header.ts_sec  = ns / 1000000000ull;
390 		pcap_pkt.pcap_header.ts_usec = ns % 1000000000ull;
391 		pcap_pkt.pcap_header.incl_len = pcap_headerlen + incl_len;
392 		pcap_pkt.pcap_header.orig_len = pcap_headerlen + packet_len;
393 
394 		pcap_pkt.ppi_packet_header.pph_version = 0;
395 		pcap_pkt.ppi_packet_header.pph_flags = 0;
396 		pcap_pkt.ppi_packet_header.pph_len = htole16(pcap_headerlen);
397 		pcap_pkt.ppi_packet_header.pph_dlt = htole32(DLT_USER0);
398 
399 		pcap_pkt.ppi_fieldheader.pfh_type = htole16(PPI_BTLE);
400 		pcap_pkt.ppi_fieldheader.pfh_datalen = htole16(sizeof(ppi_btle_t));
401 
402 		pcap_pkt.le_ll_ppi_header.btle_version = h->btle_ppi_version;
403 		pcap_pkt.le_ll_ppi_header.btle_channel = htole16(MHz);
404 		pcap_pkt.le_ll_ppi_header.btle_clkn_high = clkn_high;
405 		pcap_pkt.le_ll_ppi_header.btle_clk100ns = htole32(pkt->clk100ns);
406 		pcap_pkt.le_ll_ppi_header.rssi_max = rssi_max;
407 		pcap_pkt.le_ll_ppi_header.rssi_min = rssi_min;
408 		pcap_pkt.le_ll_ppi_header.rssi_avg = rssi_avg;
409 		pcap_pkt.le_ll_ppi_header.rssi_count = rssi_count;
410 		(void) memcpy( &pcap_pkt.le_packet[0], &pkt->symbols[0], incl_len);
411 		btbb_pcap_dump(h->pcap_file, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.ppi_packet_header);
412 		return 0;
413 	}
414 	return -PCAP_INVALID_HANDLE;
415 }
416 
417 int
418 lell_pcap_close(lell_pcap_handle *h)
419 {
420 	if (h && h->pcap_file) {
421 		fclose(h->pcap_file);
422 	}
423 	if (h) {
424 		free(h);
425 		return 0;
426 	}
427 	return -PCAP_INVALID_HANDLE;
428 }
429