ue_data.c 14.6 KB
Newer Older
Lionel Gauthier's avatar
GPLv3  
Lionel Gauthier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
Cedric Roux's avatar
 
Cedric Roux committed
15

Lionel Gauthier's avatar
GPLv3  
Lionel Gauthier committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.

 *******************************************************************************/
/*****************************************************************************
30
Source    ue_data.c
Cedric Roux's avatar
 
Cedric Roux committed
31

32
Version   0.1
Cedric Roux's avatar
 
Cedric Roux committed
33

34
Date    2012/11/02
Cedric Roux's avatar
 
Cedric Roux committed
35

36
Product   UE data generator
Cedric Roux's avatar
 
Cedric Roux committed
37

38
Subsystem UE data generator main process
Cedric Roux's avatar
 
Cedric Roux committed
39

40
Author    Frederic Maurel
Cedric Roux's avatar
 
Cedric Roux committed
41

42 43
Description Implements the utility used to generate data stored in the
    UE's non-volatile memory device
Cedric Roux's avatar
 
Cedric Roux committed
44 45 46 47 48 49 50

*****************************************************************************/

#include "userDef.h"
#include "memory.h"

#include "emmData.h"
Lionel Gauthier's avatar
Lionel Gauthier committed
51
#include "network.h"
Cedric Roux's avatar
 
Cedric Roux committed
52

53 54 55
#include <stdio.h>  // perror, printf, fprintf, snprintf
#include <stdlib.h> // exit, free
#include <string.h> // memset, strncpy
Cedric Roux's avatar
 
Cedric Roux committed
56 57 58 59 60

/****************************************************************************/
/****************  E X T E R N A L    D E F I N I T I O N S  ****************/
/****************************************************************************/

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#define USER_IMEI   "35611302209414"
#define USER_MANUFACTURER "EURECOM"
#define USER_MODEL    "LTE Android PC"
//#define USER_MANUFACTURER "SAGEM"
//#define USER_MODEL    "my225x"
#define USER_PIN    "0000"

#define PRINT_PLMN_DIGIT(d) if ((d) != 0xf) printf("%u", (d))

#define PRINT_PLMN(plmn)    \
    PRINT_PLMN_DIGIT((plmn).MCCdigit1); \
    PRINT_PLMN_DIGIT((plmn).MCCdigit2); \
    PRINT_PLMN_DIGIT((plmn).MCCdigit3); \
    PRINT_PLMN_DIGIT((plmn).MNCdigit1); \
    PRINT_PLMN_DIGIT((plmn).MNCdigit2); \
Cedric Roux's avatar
 
Cedric Roux committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    PRINT_PLMN_DIGIT((plmn).MNCdigit3)

/****************************************************************************/
/*******************  L O C A L    D E F I N I T I O N S  *******************/
/****************************************************************************/

static void _display_usage(const char* command);

static void _gen_user_data(user_nvdata_t* data);
static void _gen_emm_data(emm_nvdata_t* data);

static int _luhn(const char* cc);
static void _display_ue_data(const user_nvdata_t* data);
static void _display_emm_data(const emm_nvdata_t* data);

/****************************************************************************/
/******************  E X P O R T E D    F U N C T I O N S  ******************/
/****************************************************************************/

int main (int argc, const char* argv[])
{
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
  int rc;
  char* path;
  user_nvdata_t user_data;
  emm_nvdata_t emm_data;

  unsigned char gen_data;

  /*
   * ----------------------------
   * Read command line parameters
   * ----------------------------
   */
  if (argc != 2) {
    fprintf(stderr, "Invalid parameter\n");
    _display_usage(argv[0]);
    exit(EXIT_FAILURE);
  } else if ( (strcmp(argv[1], "--gen") == 0) ||
              (strcmp(argv[1], "-g") == 0) ) {
    /* Generate UE data files */
    gen_data = TRUE;
  } else if ( (strcmp(argv[1], "--print") == 0) ||
              (strcmp(argv[1], "-p") == 0) ) {
    /* Display content of UE data files */
    gen_data = FALSE;
  } else {
    /* Display usage */
    _display_usage(argv[0]);
    exit(EXIT_SUCCESS);
  }
  /*
   * ----------------------
   * UE's non-volatile data
   * ----------------------
   */
  path = memory_get_path(USER_NVRAM_DIRNAME, USER_NVRAM_FILENAME);
Cedric Roux's avatar
 
Cedric Roux committed
132

133 134 135 136
  if (path == NULL) {
    perror("ERROR\t: memory_get_path() failed");
    exit(EXIT_FAILURE);
  }
Cedric Roux's avatar
 
Cedric Roux committed
137

138
  if (gen_data) {
Cedric Roux's avatar
 
Cedric Roux committed
139
    /*
140
     * Initialize UE's non-volatile data
Cedric Roux's avatar
 
Cedric Roux committed
141
     */
142 143
    memset(&user_data, 0, sizeof(user_nvdata_t));
    _gen_user_data(&user_data);
Cedric Roux's avatar
 
Cedric Roux committed
144
    /*
145
     * Write UE's non-volatile data
Cedric Roux's avatar
 
Cedric Roux committed
146
     */
147 148
    rc = memory_write(path, &user_data, sizeof(user_nvdata_t));

Cedric Roux's avatar
 
Cedric Roux committed
149
    if (rc != RETURNok) {
150 151 152
      perror("ERROR\t: memory_write() failed");
      free(path);
      exit(EXIT_FAILURE);
Cedric Roux's avatar
 
Cedric Roux committed
153
    }
154 155 156 157 158 159 160 161 162 163
  }

  /*
   * Read UE's non-volatile data
   */
  memset(&user_data, 0, sizeof(user_nvdata_t));
  rc = memory_read(path, &user_data, sizeof(user_nvdata_t));

  if (rc != RETURNok) {
    perror("ERROR\t: memory_read() failed");
Cedric Roux's avatar
 
Cedric Roux committed
164
    free(path);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    exit(EXIT_FAILURE);
  }

  free(path);
  /*
   * Display UE's non-volatile data
   */
  printf("\nUE's non-volatile data:\n\n");
  _display_ue_data(&user_data);

  /*
   * ---------------------
   * EMM non-volatile data
   * ---------------------
   */
  path = memory_get_path(EMM_NVRAM_DIRNAME, EMM_NVRAM_FILENAME);

  if (path == NULL) {
    perror("ERROR\t: memory_get_path() failed");
    exit(EXIT_FAILURE);
  }

  if (gen_data) {
Cedric Roux's avatar
 
Cedric Roux committed
188
    /*
189
     * Initialize EMM non-volatile data
Cedric Roux's avatar
 
Cedric Roux committed
190
     */
191 192
    memset(&emm_data, 0, sizeof(emm_nvdata_t));
    _gen_emm_data(&emm_data);
Cedric Roux's avatar
 
Cedric Roux committed
193
    /*
194
     * Write EMM non-volatile data
Cedric Roux's avatar
 
Cedric Roux committed
195
     */
196
    rc = memory_write(path, &emm_data, sizeof(emm_nvdata_t));
Cedric Roux's avatar
 
Cedric Roux committed
197 198

    if (rc != RETURNok) {
199 200 201
      perror("ERROR\t: memory_write() failed");
      free(path);
      exit(EXIT_FAILURE);
Cedric Roux's avatar
 
Cedric Roux committed
202
    }
203
  }
Cedric Roux's avatar
 
Cedric Roux committed
204

205 206 207 208 209
  /*
   * Read EMM non-volatile data
   */
  memset(&emm_data, 0, sizeof(emm_nvdata_t));
  rc = memory_read(path, &emm_data, sizeof(emm_nvdata_t));
Cedric Roux's avatar
 
Cedric Roux committed
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
  if (rc != RETURNok) {
    perror("ERROR\t: memory_read() failed ");
    free(path);
    exit(EXIT_FAILURE);
  }

  free(path);
  /*
   * Display EMM non-volatile data
   */
  printf("\nEMM non-volatile data:\n\n");
  _display_emm_data(&emm_data);

  /*
   *---------------
   * Files location
   *---------------
   */
  path = memory_get_path(USER_NVRAM_DIRNAME, USER_NVRAM_FILENAME);
  printf("\nUE identity data file: %s\n", path);
  free(path);
  path = memory_get_path(EMM_NVRAM_DIRNAME, EMM_NVRAM_FILENAME);
  printf("EPS Mobility Management data file: %s\n", path);
  free(path);

  exit(EXIT_SUCCESS);
Cedric Roux's avatar
 
Cedric Roux committed
237 238 239 240 241 242 243 244 245 246 247
}

/****************************************************************************/
/*********************  L O C A L    F U N C T I O N S  *********************/
/****************************************************************************/

/*
 * Displays command line usage
 */
static void _display_usage(const char* command)
{
248 249 250 251 252 253 254 255 256 257 258
  fprintf(stderr, "usage: %s [OPTION]\n", command);
  fprintf(stderr, "\t[--gen|-g]\tGenerate the UE data files\n");
  fprintf(stderr, "\t[--print|-p]\tDisplay the content of the UE data files\n");
  fprintf(stderr, "\t[--help|-h]\tDisplay this usage\n");
  const char* path = getenv("NVRAM_DIR");

  if (path != NULL) {
    fprintf(stderr, "NVRAM_DIR = %s\n", path);
  } else {
    fprintf(stderr, "NVRAM_DIR environment variable is not defined\n");
  }
Cedric Roux's avatar
 
Cedric Roux committed
259 260 261 262 263 264 265
}

/*
 * Generates UE's non-volatile data
 */
static void _gen_user_data(user_nvdata_t* data)
{
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  /*
   * Product Serial Number Identification
   * IMEI = AA-BBBBBB-CCCCCC-D
   * AA-BBBBBB: Type Allocation Code (TAC)
   * CCCCCC: Serial Number
   * D: Luhn check digit
   */
  snprintf(data->IMEI, USER_IMEI_SIZE+1, "%s%d",
           USER_IMEI, _luhn(USER_IMEI));
  /*
   * Manufacturer identifier
   */
  strncpy(data->manufacturer, USER_MANUFACTURER, USER_MANUFACTURER_SIZE);
  /*
   * Model identifier
   */
  strncpy(data->model, USER_MODEL, USER_MODEL_SIZE);
  /*
   * SIM Personal Identification Number
   */
  strncpy(data->PIN, USER_PIN, USER_PIN_SIZE);
Cedric Roux's avatar
 
Cedric Roux committed
287 288 289 290 291 292 293
}

/*
 * Generates UE's non-volatile EMM data
 */
static void _gen_emm_data(emm_nvdata_t* data)
{
Lionel Gauthier's avatar
Lionel Gauthier committed
294
#if (SELECTED_PLMN == FCT1)
295 296 297 298
  /*
   * International Mobile Subscriber Identity
   * IMSI = MCC + MNC + MSIN = 310 (USA) + 028 (UNKNOWN) + 90832150
   */
299
#warning "IMSI 310.028.90832150"
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
  data->imsi.length = 8;
  data->imsi.u.num.parity = 0x0;  // Type of identity = IMSI, even
  data->imsi.u.num.digit1 = 3;    // MCC digit 1
  data->imsi.u.num.digit2 = 1;    // MCC digit 2
  data->imsi.u.num.digit3 = 0;    // MCC digit 3
  data->imsi.u.num.digit4 = 0;    // MNC digit 1
  data->imsi.u.num.digit5 = 2;    // MNC digit 2
  data->imsi.u.num.digit6 = 8;    // MNC digit 3
  data->imsi.u.num.digit7 = 9;
  data->imsi.u.num.digit8 = 0;
  data->imsi.u.num.digit9 = 8;
  data->imsi.u.num.digit10 = 3;
  data->imsi.u.num.digit11 = 2;
  data->imsi.u.num.digit12 = 1;
  data->imsi.u.num.digit13 = 5;
  data->imsi.u.num.digit14 = 0;
  data->imsi.u.num.digit15 = 0xF;
  /*
   * Last registered home PLMN
   */
  data->rplmn.MCCdigit1 = 3;
  data->rplmn.MCCdigit2 = 1;
  data->rplmn.MCCdigit3 = 0;
  data->rplmn.MNCdigit1 = 0;
  data->rplmn.MNCdigit2 = 2;
  data->rplmn.MNCdigit3 = 8;
Lionel Gauthier's avatar
Lionel Gauthier committed
326 327
#endif
#if (SELECTED_PLMN == SFR1)
328 329 330 331
  /*
   * International Mobile Subscriber Identity
   * IMSI = MCC + MNC + MSIN = 208 (France) + 10 (SFR) + 00001234
   */
Lionel Gauthier's avatar
Lionel Gauthier committed
332
     data->imsi.length = 8;
333 334 335 336 337 338
     data->imsi.u.num.parity = 0x0;  // Type of identity = IMSI, even
     data->imsi.u.num.digit1 = 2;  // MCC digit 1
     data->imsi.u.num.digit2 = 0;  // MCC digit 2
     data->imsi.u.num.digit3 = 8;  // MCC digit 3
     data->imsi.u.num.digit4 = 1;  // MNC digit 1
     data->imsi.u.num.digit5 = 0;  // MNC digit 2
Lionel Gauthier's avatar
Lionel Gauthier committed
339
     data->imsi.u.num.digit6 = 0;//0xF;  // MNC digit 3
340 341 342 343 344 345 346 347
     data->imsi.u.num.digit7 = 0;
     data->imsi.u.num.digit8 = 0;
     data->imsi.u.num.digit9 = 0;
     data->imsi.u.num.digit10 = 0;
     data->imsi.u.num.digit11 = 1;
     data->imsi.u.num.digit12 = 2;
     data->imsi.u.num.digit13 = 3;
     data->imsi.u.num.digit14 = 4;
Lionel Gauthier's avatar
Lionel Gauthier committed
348 349 350 351 352 353 354 355 356 357
     data->imsi.u.num.digit15 = 0xF;

     data->rplmn.MCCdigit1 = 2;
     data->rplmn.MCCdigit2 = 0;
     data->rplmn.MCCdigit3 = 8;
     data->rplmn.MNCdigit1 = 1;
     data->rplmn.MNCdigit2 = 0;
     data->rplmn.MNCdigit3 = 0xf;
#endif
#if (SELECTED_PLMN == TEST1)
358 359 360 361
  /*
   * International Mobile Subscriber Identity
   * IMSI = MCC + MNC + MSIN = 001  + 01  + 00001234
   */
362 363
  data->imsi.length = 8;
  data->imsi.u.num.parity = 0x0;  // Type of identity = IMSI, even
364
  data->imsi.u.num.digit1 = 0;    // MCC digit 1
365
  data->imsi.u.num.digit2 = 0;    // MCC digit 2
366 367 368
  data->imsi.u.num.digit3 = 1;    // MCC digit 3
  data->imsi.u.num.digit4 = 0;    // MNC digit 1
  data->imsi.u.num.digit5 = 1;    // MNC digit 2
369 370 371 372 373 374 375 376 377 378 379 380 381 382
  data->imsi.u.num.digit6 = 0;
  data->imsi.u.num.digit7 = 0;
  data->imsi.u.num.digit8 = 0;
  data->imsi.u.num.digit9 = 0;
  data->imsi.u.num.digit10 = 0;
  data->imsi.u.num.digit11 = 1;
  data->imsi.u.num.digit12 = 2;
  data->imsi.u.num.digit13 = 3;
  data->imsi.u.num.digit14 = 4;
  data->imsi.u.num.digit15 = 0xF;

  /*
   * Last registered home PLMN
   */
383
  data->rplmn.MCCdigit1 = 0;
384
  data->rplmn.MCCdigit2 = 0;
385 386 387
  data->rplmn.MCCdigit3 = 1;
  data->rplmn.MNCdigit1 = 0;
  data->rplmn.MNCdigit2 = 1;
388
  data->rplmn.MNCdigit3 = 0xf;
389
#endif
390 391 392 393
  /*
   * List of Equivalent PLMNs
   */
  data->eplmn.n_plmns = 0;
Cedric Roux's avatar
 
Cedric Roux committed
394 395 396 397 398 399 400
}

/*
 * Computes the check digit using Luhn algorithm
 */
static int _luhn(const char* cc)
{
401 402
  const int m[] = {0,2,4,6,8,1,3,5,7,9};
  int odd = 1, sum = 0;
Cedric Roux's avatar
 
Cedric Roux committed
403

404 405 406 407
  for (int i = strlen(cc); i--; odd = !odd) {
    int digit = cc[i] - '0';
    sum += odd ? m[digit] : digit;
  }
Cedric Roux's avatar
 
Cedric Roux committed
408

409
  return 10 - (sum % 10);
Cedric Roux's avatar
 
Cedric Roux committed
410 411 412 413 414 415 416
}

/*
 * Displays UE's non-volatile data
 */
static void _display_ue_data(const user_nvdata_t* data)
{
417 418 419 420
  printf("IMEI\t\t= %s\n", data->IMEI);
  printf("manufacturer\t= %s\n", data->manufacturer);
  printf("model\t\t= %s\n", data->model);
  printf("PIN\t\t= %s\n", data->PIN);
Cedric Roux's avatar
 
Cedric Roux committed
421 422 423 424 425 426 427
}

/*
 * Displays UE's non-volatile EMM data
 */
static void _display_emm_data(const emm_nvdata_t* data)
{
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
  printf("IMSI\t\t= ");

  if (data->imsi.u.num.digit6 == 0b1111) {
    if (data->imsi.u.num.digit15 == 0b1111) {
      printf("%u%u%u.%u%u.%u%u%u%u%u%u%u%u\n",
             data->imsi.u.num.digit1,
             data->imsi.u.num.digit2,
             data->imsi.u.num.digit3,
             data->imsi.u.num.digit4,
             data->imsi.u.num.digit5,

             data->imsi.u.num.digit7,
             data->imsi.u.num.digit8,
             data->imsi.u.num.digit9,
             data->imsi.u.num.digit10,
             data->imsi.u.num.digit11,
             data->imsi.u.num.digit12,
             data->imsi.u.num.digit13,
             data->imsi.u.num.digit14);
    } else {
      printf("%u%u%u.%u%u.%u%u%u%u%u%u%u%u%u\n",
             data->imsi.u.num.digit1,
             data->imsi.u.num.digit2,
             data->imsi.u.num.digit3,
             data->imsi.u.num.digit4,
             data->imsi.u.num.digit5,

             data->imsi.u.num.digit7,
             data->imsi.u.num.digit8,
             data->imsi.u.num.digit9,
             data->imsi.u.num.digit10,
             data->imsi.u.num.digit11,
             data->imsi.u.num.digit12,
             data->imsi.u.num.digit13,
             data->imsi.u.num.digit14,
             data->imsi.u.num.digit15);
    }
  } else {
    if (data->imsi.u.num.digit15 == 0b1111) {
      printf("%u%u%u.%u%u%u.%u%u%u%u%u%u%u%u\n",
             data->imsi.u.num.digit1,
             data->imsi.u.num.digit2,
             data->imsi.u.num.digit3,
             data->imsi.u.num.digit4,
             data->imsi.u.num.digit5,
             data->imsi.u.num.digit6,

             data->imsi.u.num.digit7,
             data->imsi.u.num.digit8,
             data->imsi.u.num.digit9,
             data->imsi.u.num.digit10,
             data->imsi.u.num.digit11,
             data->imsi.u.num.digit12,
             data->imsi.u.num.digit13,
             data->imsi.u.num.digit14);
483
    } else {
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
      printf("%u%u%u.%u%u%u.%u%u%u%u%u%u%u%u\n",
             data->imsi.u.num.digit1,
             data->imsi.u.num.digit2,
             data->imsi.u.num.digit3,
             data->imsi.u.num.digit4,
             data->imsi.u.num.digit5,
             data->imsi.u.num.digit6,

             data->imsi.u.num.digit7,
             data->imsi.u.num.digit8,
             data->imsi.u.num.digit9,
             data->imsi.u.num.digit10,
             data->imsi.u.num.digit11,
             data->imsi.u.num.digit12,
             data->imsi.u.num.digit13,
             data->imsi.u.num.digit14,
             data->imsi.u.num.digit15);
501
    }
502
  }
Cedric Roux's avatar
 
Cedric Roux committed
503

504 505 506
  printf("RPLMN\t\t= ");
  PRINT_PLMN(data->rplmn);
  printf("\n");
Cedric Roux's avatar
 
Cedric Roux committed
507

508 509 510 511 512
  for (int i = 0; i < data->eplmn.n_plmns; i++) {
    printf("EPLMN[%d]\t= ", i);
    PRINT_PLMN(data->eplmn.plmn[i]);
    printf("\n");
  }
Cedric Roux's avatar
 
Cedric Roux committed
513
}