macpdu2wireshark.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "database.h"
#include "event.h"
#include "handler.h"
#include "config.h"
#include "utils.h"
#include "packet-mac-lte.h"

#define DEFAULT_IP   "127.0.0.1"
#define DEFAULT_PORT 9999

17 18
int no_sib = 0;

19 20 21 22 23 24 25 26 27 28 29 30 31 32
typedef struct {
  int socket;
  struct sockaddr_in to;
  OBUF buf;
  /* ul */
  int ul_rnti;
  int ul_frame;
  int ul_subframe;
  int ul_data;
  /* dl */
  int dl_rnti;
  int dl_frame;
  int dl_subframe;
  int dl_data;
33 34 35 36
  /* mib */
  int mib_frame;
  int mib_subframe;
  int mib_data;
37 38 39 40 41 42 43 44 45
  /* RA preamble */
  int preamble_frame;
  int preamble_subframe;
  int preamble_preamble;
  /* RAR */
  int rar_rnti;
  int rar_frame;
  int rar_subframe;
  int rar_data;
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
} ev_data;

void ul(void *_d, event e)
{
  ev_data *d = _d;
  ssize_t ret;
  int fsf;
  int i;

  d->buf.osize = 0;

  PUTS(&d->buf, MAC_LTE_START_STRING);
  PUTC(&d->buf, FDD_RADIO);
  PUTC(&d->buf, DIRECTION_UPLINK);
  PUTC(&d->buf, C_RNTI);

  PUTC(&d->buf, MAC_LTE_RNTI_TAG);
  PUTC(&d->buf, (e.e[d->ul_rnti].i>>8) & 255);
  PUTC(&d->buf, e.e[d->ul_rnti].i & 255);

  /* for newer version of wireshark? */
  fsf = (e.e[d->ul_frame].i << 4) + e.e[d->ul_subframe].i;
  /* for older version? */
  fsf = e.e[d->ul_subframe].i;
  PUTC(&d->buf, MAC_LTE_FRAME_SUBFRAME_TAG);
  PUTC(&d->buf, (fsf>>8) & 255);
  PUTC(&d->buf, fsf & 255);

  PUTC(&d->buf, MAC_LTE_PAYLOAD_TAG);
  for (i = 0; i < e.e[d->ul_data].bsize; i++)
    PUTC(&d->buf, ((char*)e.e[d->ul_data].b)[i]);

  ret = sendto(d->socket, d->buf.obuf, d->buf.osize, 0,
      (struct sockaddr *)&d->to, sizeof(struct sockaddr_in));
  if (ret != d->buf.osize) abort();
}

void dl(void *_d, event e)
{
  ev_data *d = _d;
  ssize_t ret;
  int fsf;
  int i;

90 91
  if (e.e[d->dl_rnti].i == 0xffff && no_sib) return;

92 93 94 95 96
  d->buf.osize = 0;

  PUTS(&d->buf, MAC_LTE_START_STRING);
  PUTC(&d->buf, FDD_RADIO);
  PUTC(&d->buf, DIRECTION_DOWNLINK);
97 98 99 100 101 102 103 104 105
  if (e.e[d->dl_rnti].i != 0xffff) {
    PUTC(&d->buf, C_RNTI);

    PUTC(&d->buf, MAC_LTE_RNTI_TAG);
    PUTC(&d->buf, (e.e[d->dl_rnti].i>>8) & 255);
    PUTC(&d->buf, e.e[d->dl_rnti].i & 255);
  } else {
    PUTC(&d->buf, SI_RNTI);
  }
106 107 108 109

  /* for newer version of wireshark? */
  fsf = (e.e[d->dl_frame].i << 4) + e.e[d->dl_subframe].i;
  /* for older version? */
110
  //fsf = e.e[d->dl_subframe].i;
111 112 113 114 115 116 117 118 119 120 121 122 123
  PUTC(&d->buf, MAC_LTE_FRAME_SUBFRAME_TAG);
  PUTC(&d->buf, (fsf>>8) & 255);
  PUTC(&d->buf, fsf & 255);

  PUTC(&d->buf, MAC_LTE_PAYLOAD_TAG);
  for (i = 0; i < e.e[d->dl_data].bsize; i++)
    PUTC(&d->buf, ((char*)e.e[d->dl_data].b)[i]);

  ret = sendto(d->socket, d->buf.obuf, d->buf.osize, 0,
      (struct sockaddr *)&d->to, sizeof(struct sockaddr_in));
  if (ret != d->buf.osize) abort();
}

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
void mib(void *_d, event e)
{
  ev_data *d = _d;
  ssize_t ret;
  int fsf;
  int i;

  d->buf.osize = 0;

  PUTS(&d->buf, MAC_LTE_START_STRING);
  PUTC(&d->buf, FDD_RADIO);
  PUTC(&d->buf, DIRECTION_DOWNLINK);
  PUTC(&d->buf, NO_RNTI);

  /* for newer version of wireshark? */
  fsf = (e.e[d->mib_frame].i << 4) + e.e[d->mib_subframe].i;
  /* for older version? */
  //fsf = e.e[d->mib_subframe].i;
  PUTC(&d->buf, MAC_LTE_FRAME_SUBFRAME_TAG);
  PUTC(&d->buf, (fsf>>8) & 255);
  PUTC(&d->buf, fsf & 255);

  PUTC(&d->buf, MAC_LTE_PAYLOAD_TAG);
  for (i = 0; i < e.e[d->mib_data].bsize; i++)
    PUTC(&d->buf, ((char*)e.e[d->mib_data].b)[i]);

  ret = sendto(d->socket, d->buf.obuf, d->buf.osize, 0,
      (struct sockaddr *)&d->to, sizeof(struct sockaddr_in));
  if (ret != d->buf.osize) abort();
}

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
void preamble(void *_d, event e)
{
  ev_data *d = _d;
  ssize_t ret;
  int fsf;

  d->buf.osize = 0;

  PUTS(&d->buf, MAC_LTE_START_STRING);
  PUTC(&d->buf, FDD_RADIO);
  PUTC(&d->buf, DIRECTION_UPLINK);

  PUTC(&d->buf, NO_RNTI);

  /* for newer version of wireshark? */
  fsf = (e.e[d->preamble_frame].i << 4) + e.e[d->preamble_subframe].i;
  /* for older version? */
  //fsf = e.e[d->preamble_subframe].i;
  PUTC(&d->buf, MAC_LTE_FRAME_SUBFRAME_TAG);
  PUTC(&d->buf, (fsf>>8) & 255);
  PUTC(&d->buf, fsf & 255);

  PUTC(&d->buf, MAC_LTE_SEND_PREAMBLE_TAG);
  PUTC(&d->buf, e.e[d->preamble_preamble].i);
  PUTC(&d->buf, 0); /* rach attempt - always 0 for us (not sure of this) */

  /* if we don't put this (even with no data) wireshark (2.4.5) is not happy */
  PUTC(&d->buf, MAC_LTE_PAYLOAD_TAG);

  ret = sendto(d->socket, d->buf.obuf, d->buf.osize, 0,
      (struct sockaddr *)&d->to, sizeof(struct sockaddr_in));
  if (ret != d->buf.osize) abort();
}

void rar(void *_d, event e)
{
  ev_data *d = _d;
  ssize_t ret;
  int fsf;
  int i;

  if (e.e[d->rar_rnti].i == 0xffff && no_sib) return;

  d->buf.osize = 0;

  PUTS(&d->buf, MAC_LTE_START_STRING);
  PUTC(&d->buf, FDD_RADIO);
  PUTC(&d->buf, DIRECTION_DOWNLINK);
  PUTC(&d->buf, RA_RNTI);

  PUTC(&d->buf, MAC_LTE_RNTI_TAG);
  PUTC(&d->buf, (e.e[d->rar_rnti].i>>8) & 255);
  PUTC(&d->buf, e.e[d->rar_rnti].i & 255);

  /* for newer version of wireshark? */
  fsf = (e.e[d->rar_frame].i << 4) + e.e[d->rar_subframe].i;
  /* for older version? */
  //fsf = e.e[d->rar_subframe].i;
  PUTC(&d->buf, MAC_LTE_FRAME_SUBFRAME_TAG);
  PUTC(&d->buf, (fsf>>8) & 255);
  PUTC(&d->buf, fsf & 255);

  PUTC(&d->buf, MAC_LTE_PAYLOAD_TAG);
  for (i = 0; i < e.e[d->rar_data].bsize; i++)
    PUTC(&d->buf, ((char*)e.e[d->rar_data].b)[i]);

  ret = sendto(d->socket, d->buf.obuf, d->buf.osize, 0,
      (struct sockaddr *)&d->to, sizeof(struct sockaddr_in));
  if (ret != d->buf.osize) abort();
}

void setup_data(ev_data *d, void *database, int ul_id, int dl_id, int mib_id,
    int preamble_id, int rar_id)
228 229 230 231
{
  database_event_format f;
  int i;

232 233 234 235 236 237 238 239 240
  d->ul_rnti           = -1;
  d->ul_frame          = -1;
  d->ul_subframe       = -1;
  d->ul_data           = -1;

  d->dl_rnti           = -1;
  d->dl_frame          = -1;
  d->dl_subframe       = -1;
  d->dl_data           = -1;
241

242 243 244
  d->mib_frame         = -1;
  d->mib_subframe      = -1;
  d->mib_data          = -1;
245

246 247 248 249 250 251 252 253
  d->preamble_frame    = -1;
  d->preamble_subframe = -1;
  d->preamble_preamble = -1;

  d->rar_rnti           = -1;
  d->rar_frame          = -1;
  d->rar_subframe       = -1;
  d->rar_data           = -1;
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

#define G(var_name, var_type, var) \
  if (!strcmp(f.name[i], var_name)) { \
    if (strcmp(f.type[i], var_type)) goto error; \
    var = i; \
    continue; \
  }

  /* ul: rnti, frame, subframe, data */
  f = get_format(database, ul_id);
  for (i = 0; i < f.count; i++) {
    G("rnti",     "int",    d->ul_rnti);
    G("frame",    "int",    d->ul_frame);
    G("subframe", "int",    d->ul_subframe);
    G("data",     "buffer", d->ul_data);
  }
  if (d->ul_rnti == -1 || d->ul_frame == -1 || d->ul_subframe == -1 ||
      d->ul_data == -1) goto error;

  /* dl: rnti, frame, subframe, data */
  f = get_format(database, dl_id);
  for (i = 0; i < f.count; i++) {
    G("rnti",     "int",    d->dl_rnti);
    G("frame",    "int",    d->dl_frame);
    G("subframe", "int",    d->dl_subframe);
    G("data",     "buffer", d->dl_data);
  }
  if (d->dl_rnti == -1 || d->dl_frame == -1 || d->dl_subframe == -1 ||
      d->dl_data == -1) goto error;

284 285 286 287 288 289 290 291 292 293 294 295
  if (mib_id != -1) {
    /* MIB: frame, subframe, data */
    f = get_format(database, mib_id);
    for (i = 0; i < f.count; i++) {
      G("frame",    "int",    d->mib_frame);
      G("subframe", "int",    d->mib_subframe);
      G("data",     "buffer", d->mib_data);
    }
    if (d->mib_frame == -1 || d->mib_subframe == -1 || d->mib_data == -1)
      goto error;
  }

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  /* preamble: frame, subframe, preamble */
  f = get_format(database, preamble_id);
  for (i = 0; i < f.count; i++) {
    G("frame",    "int", d->preamble_frame);
    G("subframe", "int", d->preamble_subframe);
    G("preamble", "int", d->preamble_preamble);
  }
  if (d->preamble_frame == -1 || d->preamble_subframe == -1 ||
      d->preamble_preamble == -1) goto error;

  /* rar: rnti, frame, subframe, data */
  f = get_format(database, rar_id);
  for (i = 0; i < f.count; i++) {
    G("rnti",     "int",    d->rar_rnti);
    G("frame",    "int",    d->rar_frame);
    G("subframe", "int",    d->rar_subframe);
    G("data",     "buffer", d->rar_data);
  }
  if (d->rar_rnti == -1 || d->rar_frame == -1 || d->rar_subframe == -1 ||
      d->rar_data == -1) goto error;

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
#undef G

  return;

error:
  printf("bad T_messages.txt\n");
  abort();
}

void *receiver(void *_d)
{
  ev_data *d = _d;
  int s;
  char buf[100000];

  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s == -1) { perror("socket"); abort(); }

  if (bind(s, (struct sockaddr *)&d->to, sizeof(struct sockaddr_in)) == -1)
    { perror("bind"); abort(); }

  while (1) {
    if (recv(s, buf, 100000, 0) <= 0) abort();
  }

  return 0;
}

void usage(void)
{
  printf(
"options:\n"
"    -d <database file>        this option is mandatory\n"
Cedric Roux's avatar
Cedric Roux committed
350
"    -i <dump file>            read events from this dump file\n"
351
"    -ip <IP address>          send packets to this IP address (default %s)\n"
352
"    -p <port>                 send packets to this port (default %d)\n"
353 354
"    -no-mib                   do not report MIB\n"
"    -no-sib                   do not report SIBs\n",
355 356 357 358 359 360 361 362 363 364 365 366 367 368
  DEFAULT_IP,
  DEFAULT_PORT
  );
  exit(1);
}

int main(int n, char **v)
{
  char *database_filename = NULL;
  char *input_filename = NULL;
  void *database;
  event_handler *h;
  int in;
  int i;
369
  int ul_id, dl_id, mib_id = -1, preamble_id, rar_id;
370 371 372
  ev_data d;
  char *ip = DEFAULT_IP;
  int port = DEFAULT_PORT;
373
  int do_mib = 1;
374 375 376 377 378 379 380

  memset(&d, 0, sizeof(ev_data));

  for (i = 1; i < n; i++) {
    if (!strcmp(v[i], "-h") || !strcmp(v[i], "--help")) usage();
    if (!strcmp(v[i], "-d"))
      { if (i > n-2) usage(); database_filename = v[++i]; continue; }
Cedric Roux's avatar
Cedric Roux committed
381
    if (!strcmp(v[i], "-i"))
382 383 384
      { if (i > n-2) usage(); input_filename = v[++i]; continue; }
    if (!strcmp(v[i], "-ip")) { if (i > n-2) usage(); ip = v[++i]; continue; }
    if (!strcmp(v[i], "-p")) {if(i>n-2)usage(); port=atoi(v[++i]); continue; }
385
    if (!strcmp(v[i], "-no-mib")) { do_mib = 0; continue; }
386
    if (!strcmp(v[i], "-no-sib")) { no_sib = 1; continue; }
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    usage();
  }

  if (database_filename == NULL) {
    printf("ERROR: provide a database file (-d)\n");
    exit(1);
  }

  if (input_filename == NULL) {
    printf("ERROR: provide an input file (-i)\n");
    exit(1);
  }

  in = open(input_filename, O_RDONLY);
  if (in == -1) { perror(input_filename); return 1; }

  database = parse_database(database_filename);
  load_config_file(database_filename);

  h = new_handler(database);

  ul_id = event_id_from_name(database, "ENB_MAC_UE_UL_PDU_WITH_DATA");
  dl_id = event_id_from_name(database, "ENB_MAC_UE_DL_PDU_WITH_DATA");
410
  if (do_mib) mib_id = event_id_from_name(database, "ENB_PHY_MIB");
411 412 413
  preamble_id=event_id_from_name(database, "ENB_PHY_INITIATE_RA_PROCEDURE");
  rar_id = event_id_from_name(database, "ENB_MAC_UE_DL_RAR_PDU_WITH_DATA");
  setup_data(&d, database, ul_id, dl_id, mib_id, preamble_id, rar_id);
414 415 416

  register_handler_function(h, ul_id, ul, &d);
  register_handler_function(h, dl_id, dl, &d);
417
  if (do_mib) register_handler_function(h, mib_id, mib, &d);
418 419
  register_handler_function(h, preamble_id, preamble, &d);
  register_handler_function(h, rar_id, rar, &d);
420 421 422 423 424 425 426 427 428 429

  d.socket = socket(AF_INET, SOCK_DGRAM, 0);
  if (d.socket == -1) { perror("socket"); exit(1); }

  d.to.sin_family = AF_INET;
  d.to.sin_port = htons(port);
  d.to.sin_addr.s_addr = inet_addr(ip);

  new_thread(receiver, &d);

430 431
  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };

432 433 434
  /* read messages */
  while (1) {
    event e;
435
    e = get_event(in, &ebuf, database);
436
    if (e.type == -1) break;
437 438
    if (!(e.type == ul_id || e.type == dl_id || e.type == mib_id ||
          e.type == preamble_id || e.type == rar_id)) continue;
439 440 441 442 443
    handle_event(h, e);
  }

  return 0;
}