-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdiag_input.c
670 lines (568 loc) · 16 KB
/
diag_input.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <osmocom/gsm/rsl.h>
#include <osmocom/core/utils.h>
#include <osmocom/gsm/gsm_utils.h>
#include <osmocom/gsm/protocol/gsm_04_08.h>
#include <arpa/inet.h>
#include <assert.h>
#include "diag_input.h"
#include "session.h"
#include "diag_structs.h"
#include "l3_handler.h"
struct diag_packet {
uint16_t msg_class;
uint16_t len;
uint16_t inner_len;
uint16_t msg_protocol;
uint64_t timestamp;
uint8_t msg_type;
uint8_t msg_subtype;
uint8_t data_len;
uint8_t data[0];
} __attribute__ ((packed));
struct burst_info {
uint32_t fn;
uint16_t arfcn[4];
} last_burst;
struct radio_message *last_m = NULL;
void diag_init(unsigned start_sid, unsigned start_cid, const char *gsmtap_target, const char *pcap_target, char *filename, uint32_t appid)
{
int callback_type;
callback_type = CALLBACK_NONE;
#ifdef USE_AUTOTIME
auto_timestamp = 1;
#else
auto_timestamp = 0;
#endif
memset(&last_burst, 0, sizeof(last_burst));
session_init(start_sid, 0, gsmtap_target, pcap_target, callback_type);
diag_set_filename(filename);
diag_set_appid(appid);
}
void diag_set_filename(char *filename)
{
if (filename && (filename[0] != '-')) {
session_from_filename(filename, &_s[0]);
session_from_filename(filename, &_s[1]);
}
}
void diag_set_appid(uint32_t appid)
{
if (appid)
{
_s[0].appid = appid;
_s[1].appid = appid;
}
}
void diag_destroy(unsigned *last_sid, unsigned *last_cid)
{
session_destroy(last_sid, last_cid);
}
uint32_t get_fn(struct diag_packet *dp)
{
return (dp->timestamp/204800)%GSM_MAX_FN;
}
uint32_t get_epoch(uint8_t *qd_time)
{
double qd_ts;
qd_ts = qd_time[1];
qd_ts += ((uint32_t)qd_time[2]) << 8;
qd_ts += ((uint32_t)qd_time[3]) << 16;
qd_ts += ((uint32_t)qd_time[4]) << 24;
qd_ts *= 1.25*256.0/1000.0;
/* Sanity check on timestamp (year > 2011) */
if (auto_timestamp || qd_ts < 1000000000) {
/* Use current time */
int rv = -1;
struct timeval tv;
rv = gettimeofday(&tv, NULL);
if (0 == rv) {
return tv.tv_sec;
}
} else {
/* Adjust timestamp from GPS to UNIX */
qd_ts += 315964800.0;
}
return qd_ts;
}
void print_common(struct diag_packet *dp, unsigned len)
{
printf("%u [%02u] ", get_fn(dp), dp->len);
printf("%04x/%03u/%03u ", dp->msg_protocol, dp->msg_type, dp->msg_subtype);
printf("[%03u] %s\n", dp->data_len, osmo_hexdump_nospc(dp->data, len-2-sizeof(struct diag_packet)));
}
struct radio_message * handle_3G(struct diag_packet *dp, unsigned len)
{
unsigned payload_len;
struct radio_message *m;
if (len < 16) {
return 0;
}
payload_len = dp->len - 16;
if (payload_len > len - 16) {
return 0;
}
if (payload_len > sizeof(m->bb.data)) {
return 0;
}
m = (struct radio_message *) malloc(sizeof(struct radio_message));
memset(m, 0, sizeof(struct radio_message));
m->rat = RAT_UMTS;
m->bb.fn[0] = get_fn(dp);
switch (dp->msg_type) {
case 0: /* UL-CCCH */
m->flags = MSG_FACCH;
m->bb.arfcn[0] = ARFCN_UPLINK;
break;
case 1: /* UL-DCCH */
m->flags = MSG_SDCCH;
m->bb.arfcn[0] = ARFCN_UPLINK;
break;
case 2: /* DL-CCCH */
m->flags = MSG_FACCH;
m->bb.arfcn[0] = 0;
break;
case 3: /* DL-DCCH */
m->flags = MSG_SDCCH;
m->bb.arfcn[0] = 0;
break;
case 4: /* DL-BCCH */
m->flags = MSG_BCCH;
m->bb.arfcn[0] = 0;
if (dp->data_len < payload_len) {
payload_len = dp->data_len;
}
break;
default:
if (msg_verbose > 1) {
printf("Discarding 3G message type=%d data=%s\n", dp->msg_type, osmo_hexdump_nospc(dp->data, payload_len));
}
free(m);
return 0;
}
m->msg_len = payload_len;
memcpy(m->bb.data, &dp->data[1], payload_len);
return m;
}
struct radio_message * handle_4G(struct diag_packet *dp, unsigned len)
{
unsigned payload_len;
struct radio_message *m;
uint8_t *data = NULL;
if (len < 16) {
return 0;
}
payload_len = len - 7;
data = &dp->data[1];
m = (struct radio_message *) malloc(sizeof(struct radio_message));
memset(m, 0, sizeof(struct radio_message));
m->rat = RAT_LTE;
m->bb.fn[0] = get_fn(dp);
switch (dp->msg_protocol) {
case 0xb0c0: // LTE RRC
m->flags = MSG_BCCH; // it's not really BCCH, just indicates RRC
m->bb.arfcn[0] = ((uint16_t) dp->data[4]) << 8 | dp->data[3];
/* Qualcomm to wireshark conversion */
switch (dp->data[7]) {
case 2: // BCCH-DL-SCH
m->chan_nr = 5;
break;
case 3: // MCCH
m->chan_nr = 7;
break;
case 4: // PCCH
m->chan_nr = 6;
break;
case 5: // DL-CCCH
m->chan_nr = 0;
break;
case 6: // DL-DCCH
m->chan_nr = 1;
break;
case 7: // UL-CCCH
m->chan_nr = 2;
m->bb.arfcn[0] |= ARFCN_UPLINK;
break;
case 8: // UL-DCCH
m->chan_nr = 3;
m->bb.arfcn[0] |= ARFCN_UPLINK;
break;
default:
// Unhandled
return NULL;
}
payload_len=len-14;
data = &dp->data[14];
break;
case 0xb0e0: // LTE NAS ESM DL (protected)
case 0xb0ea: // LTE NAS EMM DL (protected)
m->flags = MSG_SDCCH | MSG_CIPHERED;
m->bb.arfcn[0] = 0;
break;
case 0xb0e1: // LTE NAS ESM UL (protected)
case 0xb0eb: // LTE NAS EMM UL (protected)
m->flags = MSG_SDCCH | MSG_CIPHERED;
m->bb.arfcn[0] = ARFCN_UPLINK;
break;
case 0xb0e2: // LTE NAS ESM DL
case 0xb0ec: // LTE NAS EMM DL
m->flags = MSG_SDCCH;
m->bb.arfcn[0] = 0;
break;
case 0xb0e3: // LTE NAS ESM UL
case 0xb0ed: // LTE NAS EMM UL
m->flags = MSG_SDCCH;
m->bb.arfcn[0] = ARFCN_UPLINK;
break;
case 0xb0f3: // EMM ciphering and integrity keys
default:
if (msg_verbose > 1) {
printf("Discarding 4G message type=%d data=%s\n", dp->msg_type, osmo_hexdump_nospc(dp->data, payload_len));
}
free(m);
return NULL;
}
m->msg_len = payload_len;
memcpy(m->bb.data, data, payload_len);
return m;
}
struct radio_message * handle_nas(struct diag_packet *dp, unsigned len)
{
/* sanity checks */
if (dp->msg_subtype + sizeof(struct diag_packet) + 2 > len)
return 0;
if (!dp->msg_subtype)
return 0;
return new_l3(&dp->data[2], dp->msg_subtype, RAT_GSM, DOMAIN_CS, get_fn(dp), dp->msg_type, MSG_SDCCH);
}
struct radio_message * handle_bcch_and_rr(struct diag_packet *dp, unsigned len)
{
unsigned dtap_len;
dtap_len = len - 2 - sizeof(struct diag_packet);
switch (dp->msg_type) {
case 0x00:
case 0x05: // SDCCH UL RR
return new_l3(dp->data, dtap_len, RAT_GSM, DOMAIN_CS, get_fn(dp), 1, MSG_SDCCH);
case 0x04: // SACCH UL
return new_l3(dp->data, dtap_len, RAT_GSM, DOMAIN_CS, get_fn(dp), 1, MSG_SACCH);
case 0x80:
case 0x85: /* SDCCH DL RR */
return new_l3(dp->data, dtap_len, RAT_GSM, DOMAIN_CS, get_fn(dp), 0, MSG_SDCCH);
case 0x81: /* BCCH */
return new_l2(dp->data, dp->data_len, RAT_GSM, DOMAIN_CS, get_fn(dp), 0, MSG_BCCH);
case 0x83: /* CCCH */
return new_l2(dp->data, dp->data_len, RAT_GSM, DOMAIN_CS, get_fn(dp), 0, MSG_BCCH);
case 0x84: /* SACCH DL RR */
return new_l3(dp->data, dtap_len, RAT_GSM, DOMAIN_CS, get_fn(dp), 0, MSG_SACCH);
default:
if (msg_verbose > 1) {
print_common(dp, len);
}
}
return 0;
}
void handle_gsm_l1_txlev_timing_advance(struct diag_packet *dp, unsigned len)
{
struct gsm_l1_txlev_timing_advance *decoded = (struct gsm_l1_txlev_timing_advance*) &dp->msg_type;
decoded->arfcn_and_band = ntohs(decoded->arfcn_and_band);
if (len-16-2 != 4) {
if (msg_verbose > 1) {
printf("x gsm_l1_txlev_timing_advance length incorrect\n");
}
return;
}
if (msg_verbose > 1) {
//printf("x %s\n", osmo_hexdump_nospc(&dp->msg_type, len-16) );
printf("x -> arfcn: %d\n", get_arfcn_from_arfcn_and_band(decoded->arfcn_and_band));
printf("x -> band: %d\n", get_band_from_arfcn_and_band(decoded->arfcn_and_band));
printf("x -> timing advance: %u\n", decoded->timing_advance);
printf("x -> tx_power_level: %u\n", decoded->tx_power_level);
}
}
void handle_gsm_l1_surround_cell_ba_list(struct diag_packet *dp, unsigned len)
{
struct gsm_l1_surround_cell_ba_list *cl = (struct gsm_l1_surround_cell_ba_list *)&dp->msg_type;
struct surrounding_cell *sc = cl->surr_cells;
if (len-16-2 != sizeof(struct surrounding_cell)*cl->cell_count + 1) {
if (msg_verbose > 1) {
printf("x gsm_l1_surround_cell_ba_list length incorrect\n");
}
return;
}
if (msg_verbose > 1) {
int i;
for (i = 0; i < cl->cell_count; i++) {
uint8_t band = get_band_from_arfcn_and_band(ntohs(sc[i].bcch_arfcn_and_band));
if (band == 8 || band == 9) {
printf("arfcn neighbor %u %d %u\n",
get_arfcn_from_arfcn_and_band(ntohs(sc[i].bcch_arfcn_and_band)),
sc[i].rx_power,
sc[i].frame_number_offset
);
}
}
}
}
void handle_gsm_l1_burst_metrics(struct diag_packet *dp, unsigned len)
{
struct gsm_l1_burst_metrics *dat = (struct gsm_l1_burst_metrics *)&dp->msg_type;
int i;
if (len-16-2 != sizeof(struct gsm_l1_burst_metrics)) {
if (msg_verbose > 1) {
printf("x gsm_l1_burst_metrics length incorrect\n");
}
return;
}
last_burst.fn = get_fn(dp);
/* log burst information */
for (i = 0; i < 4; i++) {
uint8_t band = get_band_from_arfcn_and_band(ntohs(dat->metrics[i].arfcn_and_band));
uint16_t n_arfcn = get_arfcn_from_arfcn_and_band(ntohs(dat->metrics[i].arfcn_and_band));
if (band == 8 || band == 9) {
last_burst.arfcn[i] = n_arfcn;
} else {
last_burst.arfcn[i] = last_burst.arfcn[0];
}
}
if (msg_verbose > 1) {
for (i = 0; i < 4; i++) {
uint8_t band = get_band_from_arfcn_and_band(ntohs(dat->metrics[i].arfcn_and_band));
if (band == 8 || band == 9) {
printf("arfcn burst %u %d %u %u\n",
get_arfcn_from_arfcn_and_band(ntohs(dat->metrics[i].arfcn_and_band)),
dat->metrics[i].rx_power,
dat->metrics[i].frame_number,
get_fn(dp)
);
}
}
}
}
void handle_gsm_l1_neighbor_cell_auxiliary_measurments(struct diag_packet *dp, unsigned len)
{
struct gsm_l1_neighbor_cell_auxiliary_measurments *cl = (struct gsm_l1_neighbor_cell_auxiliary_measurments *)&dp->msg_type;
if (len-16-2 != sizeof(struct cell)*cl->cell_count + 1) {
if (msg_verbose > 1) {
printf("x gsm_l1_neighbor_cell_auxiliary_measurments length icorrect\n");
}
return;
}
if (msg_verbose > 1) {
int i;
for (i = 0; i < cl->cell_count; i++) {
struct cell* c = cl->cells + i;
uint8_t band = get_band_from_arfcn_and_band(ntohs(c[i].arfcn_and_band));
if (band == 8 || band == 9) {
printf("arfcn neighbor %u %d\n",
get_arfcn_from_arfcn_and_band(ntohs(c[i].arfcn_and_band)),
c[i].rx_power
);
}
}
}
}
void handle_gsm_monitor_bursts_v2(struct diag_packet *dp, unsigned len)
{
struct gsm_monitor_bursts_v2 *cl = (struct gsm_monitor_bursts_v2 *)&dp->msg_type;
if (len-16-2 != sizeof(struct monitor_record)*cl->number_of_records + 4) {
if (msg_verbose > 1) {
printf("x gsm_monitor_bursts_v2 length incorrect\n");
}
return;
}
if (msg_verbose > 1) {
int i;
for (i = 0; i < cl->number_of_records; i++) {
struct monitor_record* c = cl->records + i;
uint8_t band = get_band_from_arfcn_and_band(ntohs(c[i].arfcn_and_band));
if (band == 8 || band == 9) {
printf("arfcn monitor %u %d %d %u\n",
get_arfcn_from_arfcn_and_band(ntohs(c[i].arfcn_and_band)),
c[i].rx_power,
c[i].frame_number,
get_fn(dp)
);
}
}
}
}
void handle_gprs_grr_cell_reselection_measurements(struct diag_packet *dp, unsigned len)
{
struct gprs_grr_cell_reselection_measurements *cl = (struct gprs_grr_cell_reselection_measurements *)&dp->msg_type;
//printf("num %d len: %d, shoudl be %d\n", cl->neighboring_6_strongest_cells_count, len-16-2, sizeof(struct neighbor)*cl->neighboring_6_strongest_cells_count + 26);
//assert(len-16-2 == sizeof(struct neighbor)*cl->neighboring_6_strongest_cells_count + 26);
if (len-16-2 != sizeof(struct gprs_grr_cell_reselection_measurements)) {
if (msg_verbose > 1) {
printf("x gprs_grr_cell_reselection_measurements length incorrect\n");
}
return;
}
if (msg_verbose > 1) {
int i;
printf("x gprs_grr_cell_reselection_measurements\n");
for (i = 0; i < cl->neighboring_6_strongest_cells_count; i++) {
struct neighbor* c = cl->neigbors + i;
printf("x -> neighbor %d -- BCC arfcn %u band: %u PBCC arfcn %u band: %u rx_level_avg %u\n",
i,
get_arfcn_from_arfcn_and_band(ntohs(c[i].neighbor_cell_bcch_arfcn_and_band)),
get_band_from_arfcn_and_band(ntohs(c[i].neighbor_cell_bcch_arfcn_and_band)),
get_arfcn_from_arfcn_and_band(ntohs(c[i].neighbor_cell_pbcch_arfcn_and_band)),
get_band_from_arfcn_and_band(ntohs(c[i].neighbor_cell_pbcch_arfcn_and_band)),
c[i].neighbor_cell_rx_level_average
);
}
}
}
void handle_sacch_report(struct diag_packet *dp, unsigned len)
{
uint16_t b_arfcn = (uint16_t)(dp->msg_type) << 8 | dp->msg_subtype;
uint16_t old_arfcn = _s[0].arfcn;
_s[1].arfcn = _s[0].arfcn = get_arfcn_from_arfcn_and_band(b_arfcn);
if (old_arfcn != _s[0].arfcn) {
printf("SACCH report old=%d new=%d\n", old_arfcn, _s[0].arfcn);
}
}
void handle_diag(uint8_t *msg, unsigned len)
{
struct diag_packet *dp = (struct diag_packet *) msg;
struct radio_message *m = NULL;
if (dp->msg_class != 0x0010) {
if (dp->msg_class == 0x001d && len > 9) {
_s[0].timestamp.tv_sec = get_epoch(&msg[3]);
_s[1].timestamp = _s[0].timestamp;
}
if (msg_verbose > 1) {
fprintf(stderr, "Class %04x is not supported\n", dp->msg_class);
}
return;
}
/* Avoid short messages */
if (len < 16)
return;
now = get_epoch((uint8_t *) &dp->timestamp);
switch(dp->msg_protocol) {
case 0x5071:
if (msg_verbose > 1) {
fprintf(stderr, "handle_gsm_l1_surround_cell_ba_list\n");
}
handle_gsm_l1_surround_cell_ba_list(dp, len);
break;
case 0x506C:
if (msg_verbose > 1) {
fprintf(stderr, "handle_gsm_l1_burst_metrics\n");
}
handle_gsm_l1_burst_metrics(dp, len);
break;
case 0x5076:
if (msg_verbose > 1) {
fprintf(stderr, "handle_gsm_l1_txlev_timing_advance\n");
}
handle_gsm_l1_txlev_timing_advance(dp, len);
break;
case 0x507A:
// GSM L1 Serving Auxiliary Measurments
// not really interesting
break;
case 0x507B:
if (msg_verbose > 1) {
fprintf(stderr, "handle_gsm_l1_neighbor_cell_auxiliary_measurments\n");
}
handle_gsm_l1_neighbor_cell_auxiliary_measurments(dp,len);
break;
case 0x5082:
if (msg_verbose > 1) {
fprintf(stderr, "handle_gsm_monitor_bursts_v2\n");
}
handle_gsm_monitor_bursts_v2(dp, len);
break;
case 0x513A:
if (msg_verbose > 1) {
fprintf(stderr, "handle_sacch_report\n");
}
handle_sacch_report(dp, len);
break;
case 0x51FC:
if (msg_verbose > 1) {
fprintf(stderr, "handle_gprs_grr_cell_reselection_measurements\n");
}
handle_gprs_grr_cell_reselection_measurements(dp, len);
break;
case 0x412f: // 3G RRC
if (msg_verbose > 1) {
fprintf(stderr, "-> Handling 3G\n");
}
m = handle_3G(dp, len);
break;
case 0x512f: // GSM RR
if (msg_verbose > 1) {
fprintf(stderr, "Handling GSM RR\n");
}
m = handle_bcch_and_rr(dp, len);
break;
case 0x5230: // GPRS GMM (doubled msg)
if (msg_verbose > 1) {
fprintf(stderr, "-> Not handling GPRS GMM\n");
}
/* downlink handling, UL goes through DTAP */
if (dp->msg_type == 0x01)
m = new_l3(dp->data + 1, dp->data_len, RAT_GSM, DOMAIN_PS, get_fn(dp), dp->msg_type, MSG_SDCCH);
break;
case 0x713a: // DTAP (2G, 3G)
if (msg_verbose > 1) {
fprintf(stderr, "-> Handling NAS\n");
}
m = handle_nas(dp, len);
break;
case 0xb0c0: // LTE RRC
case 0xb0e0: // LTE NAS ESM DL (protected)
case 0xb0e1: // LTE NAS ESM UL (protected)
case 0xb0e2: // LTE NAS ESM DL
case 0xb0e3: // LTE NAS ESM UL
case 0xb0ea: // LTE NAS EMM DL (protected)
case 0xb0eb: // LTE NAS EMM UL (protected)
case 0xb0ec: // LTE NAS EMM DL
case 0xb0ed: // LTE NAS EMM UL
if (msg_verbose > 1) {
fprintf(stderr, "-> Handling 4G\n");
}
m = handle_4G(dp, len);
break;
case 0xb0f3: // unknown LTE
break;
default:
if (msg_verbose > 1) {
fprintf(stderr, "-> Handling default case\n");
print_common(dp, len);
}
break;
}
if (m) {
/* Attach timestamp */
m->timestamp.tv_sec = now;
if (m->bb.fn[0] > last_burst.fn) {
struct radio_message *z;
/* Swap m */
z = m;
m = last_m;
last_m = z;
}
} else {
/* Deliver delayed message */
m = last_m;
last_m = NULL;
}
if (m) {
/* Attach ARFCN */
if (m->bb.fn[0] == last_burst.fn) {
int i;
for (i = 0; i < 4; i++) {
m->bb.arfcn[i] = last_burst.arfcn[i];
}
}
handle_radio_msg(_s, m);
}
}