forked from sxjack/unix_rid_capture
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nrf_sniffer.c
627 lines (460 loc) · 12.4 KB
/
nrf_sniffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/* -*- tab-width: 2; mode: c; -*-
*
* A program for capturing opendroneid / ASTM F3411 / ASD-STAN 4709-002
* WiFi beacon direct remote identification signals.
*
* This file has code handle the interface with an NRF Sniffer.
*
* Copyright (c) 2022-2023 Steve Jack
*
* MIT licence
*
* Notes
*
* nRF Sniffer data format from -
* https://github.com/wireshark/wireshark/blob/master/epan/dissectors/packet-nordic_ble.c
*
* My nRF Beacon is running version 4.1.1 of nRF's nRF52840 dongle sniffer firmware.
*
* Be aware that the sniffer records when we don't have it open.
*
* The specification has a board ID at the start of the packet, but it doesn't seem to
* be there?
* Need to investigate why it only works if we send single bytes down the pipe.
*
*/
#pragma GCC diagnostic warning "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <termios.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/ioctl.h>
#include "rid_capture.h"
#define BLE_BUFFER_SIZE 256
#define SLIP_START 0xAB
#define SLIP_END 0xBC
#define SLIP_ESC 0xCD
#define SLIP_ESC_START 0xAC
#define SLIP_ESC_END 0xBD
#define SLIP_ESC_ESC 0xCE
#define REQ_SCAN_CONT 0x07
#define PING_REQ 0x0d
#define PING_RESP 0x0e
#define GO_IDLE 0xfe
#define BOARD_ID 0
static int sniffer = -1, pipe_in = -1, pipe_out = -1;
static volatile int end_main = 0, end_child = 0;
static uint16_t counter = 1;
#if defined(BOARD_ID)
static const uint8_t nrf_board_id = BOARD_ID;
#endif
static speed_t baud_rates[] = {B460800, B1000000, 0, 0};
static struct termios tty;
static uint8_t *nrf_sniffer_v3_encode(uint16_t *,uint8_t,uint8_t *,uint16_t);
static int sniffer_encode_byte(uint8_t *,int,uint8_t);
static int decode_sniffer_packet(uint8_t *,int);
static void child_signal_handler(int);
#if STANDALONE
static void main_signal_handler(int);
/*
*
*/
int main(int argc, char *argv[]) {
int i, sniffer, bytes, wstatus, len2 = 23, len3;
uint8_t buffer[256], *buffer2;
uint16_t length;
pid_t child;
static uint8_t dummy_payload[] = {0x1e, 0x16, 0xfa, 0xff, 0x0d, 0x0d, 0x12, 0x00, 0x5e, 0x00,
SLIP_START, SLIP_END, SLIP_ESC, 0x00,
SLIP_ESC_START, SLIP_ESC_END, SLIP_ESC_ESC, 0x00,
SLIP_START, SLIP_END, SLIP_ESC};
fprintf(stderr,"\nChecking encode/decode.\nPayload: %25s","");
memset(buffer,0,sizeof(buffer));
buffer[0] = 0x0a;
buffer[1] = 0x01;
for (i = 0; i < 6; ++i) {
buffer[len2 - 6 + i] = i + 17;
}
memcpy(&buffer[len2],dummy_payload,sizeof(dummy_payload));
len3 = len2 + sizeof(dummy_payload);
for (i = 0; i < len3; ++i) {
if ((i == 0)||(i == 10)||(i == 23)) {
fprintf(stderr,"| ");
}
fprintf(stderr,"%02x ",buffer[i]);
}
fprintf(stderr,"\n");
buffer2 = nrf_sniffer_v3_encode(&length,0x06,buffer,len3);
buffer[80] = SLIP_END;
fprintf(stderr,"Encoded: ");
for (i = 0; i < length; ++i) {
if ((i == 1)||(i == 7)||(i == 17)||(i == 30)) {
fprintf(stderr,"| ");
}
#if defined(BOARD_ID)
fprintf(stderr,"%02x ",buffer2[i + 1]);
#else
fprintf(stderr,"%02x ",buffer2[i]);
#endif
}
fprintf(stderr,"\nDecoded: ");
#if defined(BOARD_ID)
buffer2[1] = SLIP_START;
parse_nrf_sniffer(&buffer2[1],length - 1);
#else
parse_nrf_sniffer(buffer2,length);
#endif
parse_nrf_sniffer(&buffer[80],1);
fprintf(stderr,"\nChecks complete.\n");
/*
*/
signal(SIGINT,main_signal_handler);
if ((child = start_nrf_sniffer("/dev/ttyACM0",&sniffer)) > 0) {
while (!end_main) {
if ((bytes = read(pipe_out,buffer,16)) > 0) {
parse_nrf_sniffer(buffer,bytes);
#if 0
} else {
fprintf(stderr,"%3d\n",bytes);
#endif
}
}
kill(child,SIGTERM);
wait(&wstatus);
}
fprintf(stderr,"\n");
exit(0);
}
#endif
/*
*
*/
pid_t start_nrf_sniffer(const char *device, int *_pipe) {
int status, pipefd[2], bytes, flags;
uint8_t *message, rx_buffer[16];
uint16_t length;
pid_t child = -1;
*_pipe = -1;
/*
* Open and setup the serial port to the nRF dongle/sniffer.
* Serial I/O on unix is very old school.
*
*/
if ((sniffer = open(device,O_RDWR | O_NOCTTY)) < 0) {
fprintf(stderr,"%s(): Unable to open \'%s\', %d, %s\n",
__func__,device,sniffer,strerror(sniffer));
return sniffer;
}
if (status = tcgetattr(sniffer,&tty)) {
fprintf(stderr,"%s(): tcgetattr() returned %d\n",__func__,status);
close(sniffer);
return -1;
}
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
tty.c_iflag &= ~IGNBRK;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;
cfsetospeed(&tty,baud_rates[1]);
cfsetispeed(&tty,baud_rates[1]);
if (status = tcsetattr(sniffer,TCSANOW,&tty)) {
fprintf(stderr,"%s(): tcgetattr() returned %d\n",__func__,status);
close(sniffer);
return -1;
}
message = nrf_sniffer_v3_encode(&length,REQ_SCAN_CONT,NULL,0);
write(sniffer,message,length);
/*
* Create a pipe and fork off a process to handle reading the serial link to the sniffer.
*/
if ((status = pipe(pipefd)) < 0) {
fprintf(stderr,"%s(): Unable to open pipe, %d, %s\n",
__func__,sniffer,strerror(status));
close(sniffer);
return -1;
}
*_pipe =
pipe_out = pipefd[0];
pipe_in = pipefd[1];
flags = fcntl(pipe_out,F_GETFL);
flags |= O_NONBLOCK;
if ((status = fcntl(pipe_out,F_SETFL,flags)) < 0) {
fprintf(stderr,"%s(): fcntl() returned %d, %s\n",
__func__,status,strerror(status));
}
if ((child = fork()) == 0) {
signal(SIGINT,child_signal_handler);
signal(SIGTERM,child_signal_handler);
while (!end_child) {
if (bytes = read(sniffer,rx_buffer,1) > 0) {
write(pipe_in,rx_buffer,bytes);
}
#if 0 /* Causes problems? */
message = nrf_sniffer_v3_encode(&length,GO_IDLE,NULL,0);
write(sniffer,message,length);
#endif
}
exit(0);
}
return child;
}
/*
*
*/
void parse_nrf_sniffer(uint8_t *buffer,int bytes) {
int i;
uint8_t c;
static int started = 0, escaped = 0;
static int rx_index = 0;
static uint8_t rx_buffer[BLE_BUFFER_SIZE];
for (i = 0; i < bytes; ++i) {
c = buffer[i];
if (started) {
if (escaped) {
switch (c) {
case SLIP_ESC_START:
rx_buffer[rx_index++] = SLIP_START;
break;
case SLIP_ESC_END:
rx_buffer[rx_index++] = SLIP_END;
break;
case SLIP_ESC_ESC:
rx_buffer[rx_index++] = SLIP_ESC;
break;
}
escaped = 0;
} else {
switch (c) {
case SLIP_ESC:
escaped = 1;
break;
case SLIP_END:
decode_sniffer_packet(rx_buffer,rx_index);
rx_index = 0;
started = 0;
escaped = 0;
break;
default:
rx_buffer[rx_index++] = c;
break;
}
}
} else if (c == SLIP_START) {
started = 1;
rx_index = 0;
}
if (rx_index > (BLE_BUFFER_SIZE - 4)) {
rx_index = 0;
started = 0;
escaped = 0;
}
}
return;
}
/*
*
*/
void stop_nrf_sniffer() {
end_child = 1;
return;
}
/*
*
*/
static uint8_t *nrf_sniffer_v3_encode(uint16_t *encoded_length,uint8_t packet_id,
uint8_t *payload,uint16_t payload_length) {
int i;
uint16_t j = 0;
static uint8_t buffer[BLE_BUFFER_SIZE + 2];
buffer[j++] = SLIP_START;
#if defined(BOARD_ID)
j = sniffer_encode_byte(buffer,j,nrf_board_id);
#endif
j = sniffer_encode_byte(buffer,j,(uint8_t) payload_length);
j = sniffer_encode_byte(buffer,j,(uint8_t) (payload_length >> 8));
buffer[j++] = 3;
j = sniffer_encode_byte(buffer,j,(uint8_t) counter);
j = sniffer_encode_byte(buffer,j,(uint8_t) (counter >> 8));
buffer[j++] = packet_id;
++counter;
if (payload) {
for (i = 0; (i < payload_length)&&(j < BLE_BUFFER_SIZE); ++i) {
j = sniffer_encode_byte(buffer,j,payload[i]);
}
}
buffer[j++] = SLIP_END;
*encoded_length = j;
return buffer;
}
/*
*
*/
static int sniffer_encode_byte(uint8_t *buffer,int index,uint8_t byte) {
switch (byte) {
case SLIP_START:
buffer[index++] = SLIP_ESC;
buffer[index++] = SLIP_ESC_START;
break;
case SLIP_END:
buffer[index++] = SLIP_ESC;
buffer[index++] = SLIP_ESC_END;
break;
case SLIP_ESC:
buffer[index++] = SLIP_ESC;
buffer[index++] = SLIP_ESC_ESC;
break;
default:
buffer[index++] = byte;
break;
}
return index;
}
/*
*
*/
int decode_sniffer_packet(uint8_t *message,int msg_len) {
int i, j, k, l, t, offset1, offset2 = 0, crc_ok = 0;
uint8_t packet_id, version, *adv_data, *payload = NULL, mac[6];
uint16_t payload_len, counter, data_len, uuid;
#if ! STANDALONE
int odid_len = 0, rssi = 0;
float voltage = 0.0;
uint8_t *odid_data = NULL;
#if BATT_VOLTS && __arm__
union {_Float16 f16;
uint16_t u16;} batt_volts;
#endif
#endif
if (msg_len < 6) {
return 0;
}
payload_len = message[0] | (message[1] << 8);
version = message[2];
counter = message[3] | (message[4] << 8);
packet_id = message[offset1 = 5];
payload = &message[offset1 + 1];
#if STANDALONE
fprintf(stderr,"%3d %3d %d %6d %02x ",
msg_len,payload_len,version,counter,packet_id);
#endif
if (version == 3) {
switch (packet_id) {
case 0x02:
case 0x06:
if ((payload_len > 22)&&
(payload_len < (BLE_BUFFER_SIZE - 10))) {
offset2 = payload[0];
crc_ok = payload[1] & 0x01;
adv_data = &payload[j = offset2 + 13];
data_len = payload_len - j;
for (i = 0; i < 6; ++i) {
mac[i] = payload[offset2 + 12 - i];
}
#if STANDALONE
fprintf(stderr,"%d |",crc_ok);
for (i = 0; (i < j); ++i) {
if ((i == offset2)||(i == (offset2 + 13))) {
fputs(" |",stderr);
}
fprintf(stderr," %02x",payload[i]);
}
#endif
for (j = 0, k = 0, l = 1; (k < 6)&&(j < data_len)&&(l); ++k) {
l = adv_data[j];
t = adv_data[j + 1];
uuid = (((uint16_t) adv_data[j + 3]) << 8) | adv_data[j + 2];
#if STANDALONE
fprintf(stderr," |");
for (i = 0; (i <= l)&&(i < 8); ++i) {
fprintf(stderr," %02x",adv_data[j + i]);
}
if (i < l) {
fputs(" ...",stderr);
}
#else
switch (t) {
case 0x16:
if (crc_ok) {
switch (uuid) {
case 0xfffa:
odid_data = &adv_data[j + 5];
odid_len = l - 5;
rssi = -payload[3];
break;
case 0x2bf0:
#if BATT_VOLTS && __arm__
batt_volts.u16 = adv_data[j + 6] | (adv_data[j + 7] << 8);
voltage = (float) batt_volts.f16;
#endif
break;
}
}
break;
case 0x01: // flags
case 0x03: // uuids
break;
}
#endif
j += l + 1;
}
}
break;
case 0x0e: /* ping */
break;
default:
break;
}
#if STANDALONE
} else {
fputs(" |",stderr);
for (i = 0; i < 16; ++i) {
if (i == (offset1 + 1)) {
fputs(" |",stderr);
}
fprintf(stderr," %02x",message[i]);
}
#endif
}
#if STANDALONE
fprintf(stderr,"\n");
#else
if (odid_data) {
parse_odid(mac,odid_data,odid_len,rssi,"Sniffer",
(voltage)? &voltage: NULL);
}
#endif
return 0;
}
/*
*
*/
static void child_signal_handler(int sig) {
end_child = 1;
return;
}
/*
*
*/
#if STANDALONE
static void main_signal_handler(int sig) {
end_main = 1;
return;
}
#endif
/*
*
*/