• kvyat's avatar
    sync · 55551062
    kvyat authored
    55551062
x2ap_eNB_handler.c 36 KB
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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
/*******************************************************************************
    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.

    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@lists.eurecom.fr

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

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

/*! \file x2ap_eNB_handlers.c
 * \brief x2ap messages handlers for eNB part
 * \author Navid Nikaein
 * \date 2016
 * \version 0.1
 */

#include <stdint.h>

#include "intertask_interface.h"

#include "asn1_conversions.h"

#include "x2ap_common.h"
#include "x2ap_ies_defs.h"
#include "x2ap_eNB_defs.h"
#include "x2ap_eNB_handler.h"
#include "x2ap_eNB_decoder.h"

#include "x2ap_eNB_management_procedures.h"
#include "x2ap_eNB_generate_messages.h"

//#include "x2ap_eNB_default_values.h"

#include "assertions.h"
#include "conversions.h"
#include "msc.h"

static
int x2ap_eNB_handle_handover_preparation(uint32_t assoc_id,
                                         uint32_t stream,
                                         struct x2ap_message_s *message);
static
int x2ap_eNB_handle_handover_response(uint32_t assoc_id,
                                      uint32_t stream,
                                      struct x2ap_message_s *message);
static
int x2ap_eNB_handle_x2_setup_request (uint32_t assoc_id,
                                      uint32_t stream,
                                      struct x2ap_message_s *message);
static
int x2ap_eNB_handle_x2_setup_response(uint32_t               assoc_id,
                                      uint32_t               stream,
                                      struct x2ap_message_s *message_p);
static
int x2ap_eNB_handle_x2_setup_failure(uint32_t               assoc_id,
                                     uint32_t               stream,
                                     struct x2ap_message_s *message_p);

static
int x2ap_eNB_handle_error_indication(uint32_t               assoc_id,
                                     uint32_t               stream,
                                     struct x2ap_message_s *message_p);

int x2ap_eNB_handle_initial_context_request(uint32_t               assoc_id,
                                            uint32_t               stream,
                                            struct x2ap_message_s *message_p);

static
int x2ap_eNB_handle_ue_context_release_command(uint32_t               assoc_id,
                                               uint32_t               stream,
                                               struct x2ap_message_s *message_p);

/* Handlers matrix. Only eNB related procedure present here */
// x2ap_messages_callback[message.procedureCode][message.direction]
x2ap_message_decoded_callback x2ap_messages_callback[][3] = {
  //  { x2ap_eNB_handle_handover_preparation, 0, 0 }, /* HandoverPreparation */
  { x2ap_eNB_handle_handover_preparation, x2ap_eNB_handle_handover_response, 0 }, /* HandoverPreparation */
  { 0, 0, 0 }, /* HandoverCancel */
  { 0, 0, 0 }, /* loadIndication */
  { x2ap_eNB_handle_error_indication, 0, 0 }, /* errorIndication */
  { 0, 0, 0 }, /* snStatusTransfer */
  { 0, 0, 0 }, /* uEContextRelease */
  { x2ap_eNB_handle_x2_setup_request, x2ap_eNB_handle_x2_setup_response, x2ap_eNB_handle_x2_setup_failure }, /* x2Setup */
  { 0, 0, 0 }, /* reset */
  { 0, 0, 0 }, /* eNBConfigurationUpdate */
  { 0, 0, 0 }, /* resourceStatusReportingInitiation */
  { 0, 0, 0 }, /* resourceStatusReporting */
  { 0, 0, 0 }, /* privateMessage */
  { 0, 0, 0 }, /* mobilitySettingsChange */
  { 0, 0, 0 }, /* rLFIndication */
  { 0, 0, 0 }, /* handoverReport */
  { 0, 0, 0 }  /* cellActivation */

};

static const char *x2ap_direction2String[] = {
  "", /* Nothing */
  "Originating message", /* originating message */
  "Successfull outcome", /* successfull outcome */
  "UnSuccessfull outcome", /* successfull outcome */
};

int x2ap_eNB_handle_message(uint32_t assoc_id, int32_t stream,
                            const uint8_t * const data, const uint32_t data_length)
{
  struct x2ap_message_s message;

  DevAssert(data != NULL);

  memset(&message, 0, sizeof(struct x2ap_message_s));

  if (x2ap_eNB_decode_pdu(&message, data, data_length) < 0) {
    X2AP_ERROR("Failed to decode X2AP PDU\n");
    return -1;
  }

  /* Checking procedure Code and direction of message */
  if (message.procedureCode > sizeof(x2ap_messages_callback) / (3 * sizeof(x2ap_message_decoded_callback))
      || (message.direction > X2AP_PDU_PR_unsuccessfulOutcome)) {
    X2AP_ERROR("[SCTP %d] Either procedureCode %d or direction %d exceed expected\n",
               assoc_id, message.procedureCode, message.direction);
    return -1;
  }

  /* No handler present.
   * This can mean not implemented or no procedure for eNB (wrong direction).
   */
  if (x2ap_messages_callback[message.procedureCode][message.direction-1] == NULL) {
    X2AP_ERROR("[SCTP %d] No handler for procedureCode %d in %s\n",
               assoc_id, message.procedureCode,
               x2ap_direction2String[message.direction]);
    return -1;
  }

  /* Calling the right handler */
  return (*x2ap_messages_callback[message.procedureCode][message.direction-1])
         (assoc_id, stream, &message);
}


void x2ap_handle_x2_setup_message(x2ap_eNB_data_t *enb_desc_p, int sctp_shutdown)
{
  if (sctp_shutdown) {
    /* A previously connected eNB has been shutdown */

    /* TODO check if it was used by some eNB and send a message to inform these eNB if there is no more associated MME */
    if (enb_desc_p->state == X2AP_ENB_STATE_CONNECTED) {
      enb_desc_p->state = X2AP_ENB_STATE_DISCONNECTED;

      if (enb_desc_p->x2ap_eNB_instance-> x2_target_enb_associated_nb > 0) {
        /* Decrease associated eNB number */
        enb_desc_p->x2ap_eNB_instance-> x2_target_enb_associated_nb --;
      }

      /* If there are no more associated eNB, inform eNB app */
      if (enb_desc_p->x2ap_eNB_instance->x2_target_enb_associated_nb == 0) {
        MessageDef                 *message_p;

        message_p = itti_alloc_new_message(TASK_X2AP, X2AP_DEREGISTERED_ENB_IND);
        X2AP_DEREGISTERED_ENB_IND(message_p).nb_x2 = 0;
        itti_send_msg_to_task(TASK_ENB_APP, enb_desc_p->x2ap_eNB_instance->instance, message_p);
      }
    }
  } else {
    /* Check that at least one setup message is pending */
    DevCheck(enb_desc_p->x2ap_eNB_instance->x2_target_enb_pending_nb > 0,
             enb_desc_p->x2ap_eNB_instance->instance,
             enb_desc_p->x2ap_eNB_instance->x2_target_enb_pending_nb, 0);

    if (enb_desc_p->x2ap_eNB_instance->x2_target_enb_pending_nb > 0) {
      /* Decrease pending messages number */
      enb_desc_p->x2ap_eNB_instance->x2_target_enb_pending_nb --;
    }

    /* If there are no more pending messages, inform eNB app */
    if (enb_desc_p->x2ap_eNB_instance->x2_target_enb_pending_nb == 0) {
      MessageDef                 *message_p;

      message_p = itti_alloc_new_message(TASK_X2AP, X2AP_REGISTER_ENB_CNF);
      X2AP_REGISTER_ENB_CNF(message_p).nb_x2 = enb_desc_p->x2ap_eNB_instance->x2_target_enb_associated_nb;
      itti_send_msg_to_task(TASK_ENB_APP, enb_desc_p->x2ap_eNB_instance->instance, message_p);
    }
  }
}

int
x2ap_eNB_handle_handover_preparation(uint32_t assoc_id,
                                     uint32_t stream,
                                     struct x2ap_message_s *message)
{
  X2ap_HandoverRequest_IEs_t         *x2HandoverRequest;
  x2ap_eNB_data_t                    *x2ap_eNB_data;
  uint32_t                           eNB_id = 0;
  uint8_t                             i;
  int                                ta_ret;
  //uint16_t                                max_enb_connected;
  MessageDef                 *m;

  DevAssert (message != NULL);
  x2HandoverRequest = &message->msg.x2ap_HandoverRequest_IEs;

  if (stream == 0) {
    X2AP_ERROR ("Received new x2 handover request on stream == 0\n");
    /* TODO: send a x2 failure response */
    return 0;
  }

  X2AP_DEBUG ("Received a new X2 handover request\n");

  x2ap_eNB_data = x2ap_get_eNB(NULL, assoc_id, 0);
  DevAssert(x2ap_eNB_data != NULL);

  m = itti_alloc_new_message(TASK_X2AP, X2AP_HANDOVER_REQ);
  /* TODO: fill the message */
  extern int x2id_to_source_rnti[1];
  X2AP_HANDOVER_REQ(m).source_x2id = x2HandoverRequest->old_eNB_UE_X2AP_ID;
  X2AP_HANDOVER_REQ(m).source_rnti = x2id_to_source_rnti[x2HandoverRequest->old_eNB_UE_X2AP_ID];
  X2AP_HANDOVER_REQ(m).nb_e_rabs_tobesetup=x2HandoverRequest->uE_ContextInformation.e_RABs_ToBeSetup_List.list.count;

  X2ap_E_RABs_ToBeSetup_ListIEs_t *e_RABs_ToBeSetup_ListIEs=calloc(1, sizeof(X2ap_E_RABs_ToBeSetup_ListIEs_t));

if (x2ap_decode_x2ap_e_rabs_tobesetup_list(e_RABs_ToBeSetup_ListIEs,
										   &x2HandoverRequest->uE_ContextInformation.e_RABs_ToBeSetup_List)>0) {

  for (i=0; i< x2HandoverRequest->uE_ContextInformation.e_RABs_ToBeSetup_List.list.count;i++){
	  X2ap_E_RABs_ToBeSetup_Item_t *e_RABs_ToBeSetup_Item;
	  e_RABs_ToBeSetup_Item=(X2ap_E_RABs_ToBeSetup_Item_t *) e_RABs_ToBeSetup_ListIEs->x2ap_E_RABs_ToBeSetup_Item.array[i];

	  X2AP_HANDOVER_REQ(m).e_rabs_tobesetup[i].e_rab_id=e_RABs_ToBeSetup_Item->e_RAB_ID;

	  memcpy(X2AP_HANDOVER_REQ(m).e_rabs_tobesetup[i].eNB_addr.buffer,
			 e_RABs_ToBeSetup_Item->uL_GTPtunnelEndpoint.transportLayerAddress.buf,
			 e_RABs_ToBeSetup_Item->uL_GTPtunnelEndpoint.transportLayerAddress.size);

	  X2AP_HANDOVER_REQ(m).e_rabs_tobesetup[i].eNB_addr.length =
			  e_RABs_ToBeSetup_Item->uL_GTPtunnelEndpoint.transportLayerAddress.size * 8 - e_RABs_ToBeSetup_Item->uL_GTPtunnelEndpoint.transportLayerAddress.bits_unused;

	  OCTET_STRING_TO_INT32(&e_RABs_ToBeSetup_Item->uL_GTPtunnelEndpoint.gTP_TEID,
			  	  	  	    X2AP_HANDOVER_REQ(m).e_rabs_tobesetup[i].gtp_teid);

  }
}else {
	X2AP_ERROR ("Can't decode the e_RABs_ToBeSetup_List \n");
}
  if ((x2HandoverRequest->uE_ContextInformation.aS_SecurityInformation.key_eNodeB_star.buf) &&
	  (x2HandoverRequest->uE_ContextInformation.aS_SecurityInformation.key_eNodeB_star.size == 32)) {
    memcpy(X2AP_HANDOVER_REQ(m).kenb, x2HandoverRequest->uE_ContextInformation.aS_SecurityInformation.key_eNodeB_star.buf, 32);
    X2AP_HANDOVER_REQ(m).kenb_ncc = x2HandoverRequest->uE_ContextInformation.aS_SecurityInformation.nextHopChainingCount;
  } else {
	  X2AP_WARN ("Size of eNB key star does not match the expected value\n");
	  //return -1;
  }

  itti_send_msg_to_task(TASK_RRC_ENB, x2ap_eNB_data->x2ap_eNB_instance->instance, m);

#if 0
  if (x2SetupRequest->globalENB_ID.eNB_ID.present == X2ap_ENB_ID_PR_home_eNB_ID) {
    // Home eNB ID = 28 bits
    uint8_t  *eNB_id_buf = x2SetupRequest->globalENB_ID.eNB_ID.choice.home_eNB_ID.buf;

    if (x2SetupRequest->globalENB_ID.eNB_ID.choice.macro_eNB_ID.size != 28) {
      //TODO: handle case were size != 28 -> notify ? reject ?
    }

    eNB_id = (eNB_id_buf[0] << 20) + (eNB_id_buf[1] << 12) + (eNB_id_buf[2] << 4) + ((eNB_id_buf[3] & 0xf0) >> 4);
    X2AP_DEBUG ("Home eNB id: %07x\n", eNB_id);
  } else {
    // Macro eNB = 20 bits
    uint8_t *eNB_id_buf = x2SetupRequest->globalENB_ID.eNB_ID.choice.macro_eNB_ID.buf;

    if (x2SetupRequest->globalENB_ID.eNB_ID.choice.macro_eNB_ID.size != 20) {
      //TODO: handle case were size != 20 -> notify ? reject ?
    }

    eNB_id = (eNB_id_buf[0] << 12) + (eNB_id_buf[1] << 4) + ((eNB_id_buf[2] & 0xf0) >> 4);
    X2AP_DEBUG ("macro eNB id: %05x\n", eNB_id);
  }

  /*
   * If none of the provided PLMNs/TAC match the one configured in MME,
   * * * * the x2 setup should be rejected with a cause set to Unknown PLMN.
   */
  // ta_ret = x2ap_eNB_compare_ta_lists (&x2SetupRequest_p->supportedTAs);

  /*
   * Source and Target eNBs have no common PLMN
   */
  /*
  if (ta_ret != TA_LIST_RET_OK) {
    X2AP_ERROR ("No Common PLMN with the target eNB, generate_x2_setup_failure\n");
      return x2ap_eNB_generate_x2_setup_failure (assoc_id,
                                                 X2ap_Cause_PR_misc,
                                                 X2ap_CauseMisc_unknown_PLMN,
                                                 X2ap_TimeToWait_v20s);
  }
  */
  X2AP_DEBUG ("Adding eNB to the list of associated eNBs\n");

  if ((x2ap_eNB_data = x2ap_is_eNB_id_in_list (eNB_id)) == NULL) {
      /*
       * eNB has not been fount in list of associated eNB,
       * * * * Add it to the tail of list and initialize data
       */
    if ((x2ap_eNB_data = x2ap_is_eNB_assoc_id_in_list (assoc_id)) == NULL) {
      /*
       * ??
       */
      return -1;
    } else {
      x2ap_eNB_data->state = X2AP_ENB_STATE_RESETTING;
      x2ap_eNB_data->eNB_id = eNB_id;
    }


  } else {
    x2ap_eNB_data->state = X2AP_ENB_STATE_RESETTING;

    /*
     * eNB has been fount in list, consider the x2 setup request as a reset connection,
     * * * * reseting any previous UE state if sctp association is != than the previous one
     */
    if (x2ap_eNB_data->assoc_id != assoc_id) {
      X2SetupFailure_IEs_t                x2SetupFailure;

      memset (&x2SetupFailure, 0, sizeof (x2SetupFailure));
      /*
       * Send an overload cause...
       */
      X2AP_ERROR ("Rejeting x2 setup request as eNB id %d is already associated to an active sctp association" "Previous known: %d, new one: %d\n", eNB_id, x2ap_eNB_data->assoc_id, assoc_id);
      x2ap_eNB_generate_x2_setup_failure (assoc_id,
                                          X2ap_Cause_PR_protocol,
                                          X2ap_CauseProtocol_unspecified,
                                          -1);
      return -1;
    }

    /*
     * TODO: call the reset procedure
     */
  }

  return x2ap_generate_x2_setup_response (x2ap_eNB_data);
#endif
}

int
x2ap_eNB_handle_handover_response(uint32_t assoc_id,
                                  uint32_t stream,
                                  struct x2ap_message_s *message)
{
  X2ap_HandoverRequestAcknowledge_IEs_t *x2HandoverRequestAck;
  x2ap_eNB_data_t                    *x2ap_eNB_data;
  uint32_t                           eNB_id = 0;
  int                                ta_ret;
  //uint16_t                                max_enb_connected;
  MessageDef                 *m;

  DevAssert (message != NULL);
  x2HandoverRequestAck = &message->msg.x2ap_HandoverRequestAcknowledge_IEs;

  if (stream == 0) {
    X2AP_ERROR ("Received new x2 handover response on stream == 0\n");
    /* TODO: what to do? */
    return 0;
  }

  X2AP_DEBUG ("Received a new X2 handover response\n");

  x2ap_eNB_data = x2ap_get_eNB(NULL, assoc_id, 0);
  DevAssert(x2ap_eNB_data != NULL);

  m = itti_alloc_new_message(TASK_X2AP, X2AP_HANDOVER_REQ_ACK);
  /* TODO: fill the message */
  extern int x2id_to_source_rnti[1];
  X2AP_HANDOVER_REQ_ACK(m).source_x2id = x2HandoverRequestAck->old_eNB_UE_X2AP_ID;
  X2AP_HANDOVER_REQ_ACK(m).source_rnti = x2id_to_source_rnti[x2HandoverRequestAck->old_eNB_UE_X2AP_ID];

  X2ap_TargeteNBtoSource_eNBTransparentContainer_t *c = &x2HandoverRequestAck->targeteNBtoSource_eNBTransparentContainer;

  if (c->size > 1024 /* TODO: this is the size of rrc_buffer in struct x2ap_handover_req_ack_s*/)
    { printf("%s:%d: fatal: buffer too big\n", __FILE__, __LINE__); abort(); }

  memcpy(X2AP_HANDOVER_REQ_ACK(m).rrc_buffer, c->buf, c->size);
  X2AP_HANDOVER_REQ_ACK(m).rrc_buffer_size = c->size;

  itti_send_msg_to_task(TASK_RRC_ENB, x2ap_eNB_data->x2ap_eNB_instance->instance, m);
  return 0;
}

int
x2ap_eNB_handle_x2_setup_request (uint32_t assoc_id,
                                  uint32_t stream,
                                  struct x2ap_message_s *message)

{

  X2SetupRequest_IEs_t               *x2SetupRequest;
  x2ap_eNB_data_t                    *x2ap_eNB_data;
  uint32_t                           eNB_id = 0;
  int                                ta_ret;
  //uint16_t                                max_enb_connected;

  DevAssert (message != NULL);
  x2SetupRequest = &message->msg.x2SetupRequest_IEs;

  MSC_LOG_RX_MESSAGE (MSC_X2AP_TARGET_ENB,
                      MSC_X2AP_SRC_ENB, NULL, 0,
                      "0 X2Setup/%s assoc_id %u stream %u",
                      x2ap_direction2String[message->direction],
                      assoc_id, stream);
  /*
   * We received a new valid X2 Setup Request on a stream != 0.
   * * * * This should not happen -> reject eNB x2 setup request.
   */

  if (stream != 0) {
    X2AP_ERROR ("Received new x2 setup request on stream != 0\n");
      /*
       * Send a x2 setup failure with protocol cause unspecified
       */
    return x2ap_eNB_generate_x2_setup_failure (assoc_id,
                                               X2ap_Cause_PR_protocol,
                                               X2ap_CauseProtocol_unspecified,
                                               -1);
  }

  X2AP_DEBUG ("Received a new X2 setup request\n");

  if (x2SetupRequest->globalENB_ID.eNB_ID.present == X2ap_ENB_ID_PR_home_eNB_ID) {
    // Home eNB ID = 28 bits
    uint8_t  *eNB_id_buf = x2SetupRequest->globalENB_ID.eNB_ID.choice.home_eNB_ID.buf;

    if (x2SetupRequest->globalENB_ID.eNB_ID.choice.macro_eNB_ID.size != 28) {
      //TODO: handle case were size != 28 -> notify ? reject ?
    }

    eNB_id = (eNB_id_buf[0] << 20) + (eNB_id_buf[1] << 12) + (eNB_id_buf[2] << 4) + ((eNB_id_buf[3] & 0xf0) >> 4);
    X2AP_DEBUG ("Home eNB id: %07x\n", eNB_id);
  } else {
    // Macro eNB = 20 bits
    uint8_t *eNB_id_buf = x2SetupRequest->globalENB_ID.eNB_ID.choice.macro_eNB_ID.buf;

    if (x2SetupRequest->globalENB_ID.eNB_ID.choice.macro_eNB_ID.size != 20) {
      //TODO: handle case were size != 20 -> notify ? reject ?
    }

    eNB_id = (eNB_id_buf[0] << 12) + (eNB_id_buf[1] << 4) + ((eNB_id_buf[2] & 0xf0) >> 4);
    X2AP_DEBUG ("macro eNB id: %05x\n", eNB_id);
  }

  /*
   * If none of the provided PLMNs/TAC match the one configured in MME,
   * * * * the x2 setup should be rejected with a cause set to Unknown PLMN.
   */
  // ta_ret = x2ap_eNB_compare_ta_lists (&x2SetupRequest_p->supportedTAs);

  /*
   * Source and Target eNBs have no common PLMN
   */
  /*
  if (ta_ret != TA_LIST_RET_OK) {
    X2AP_ERROR ("No Common PLMN with the target eNB, generate_x2_setup_failure\n");
      return x2ap_eNB_generate_x2_setup_failure (assoc_id,
                                                 X2ap_Cause_PR_misc,
                                                 X2ap_CauseMisc_unknown_PLMN,
                                                 X2ap_TimeToWait_v20s);
  }
  */
  X2AP_DEBUG ("Adding eNB to the list of associated eNBs\n");

  if ((x2ap_eNB_data = x2ap_is_eNB_id_in_list (eNB_id)) == NULL) {
      /*
       * eNB has not been fount in list of associated eNB,
       * * * * Add it to the tail of list and initialize data
       */
    if ((x2ap_eNB_data = x2ap_is_eNB_assoc_id_in_list (assoc_id)) == NULL) {
      /*
       * ??
       */
      return -1;
    } else {
      x2ap_eNB_data->state = X2AP_ENB_STATE_RESETTING;
      x2ap_eNB_data->eNB_id = eNB_id;
    }


  } else {
    x2ap_eNB_data->state = X2AP_ENB_STATE_RESETTING;

    /*
     * eNB has been fount in list, consider the x2 setup request as a reset connection,
     * * * * reseting any previous UE state if sctp association is != than the previous one
     */
    if (x2ap_eNB_data->assoc_id != assoc_id) {
      X2SetupFailure_IEs_t                x2SetupFailure;

      memset (&x2SetupFailure, 0, sizeof (x2SetupFailure));
      /*
       * Send an overload cause...
       */
      X2AP_ERROR ("Rejeting x2 setup request as eNB id %d is already associated to an active sctp association" "Previous known: %d, new one: %d\n", eNB_id, x2ap_eNB_data->assoc_id, assoc_id);
      x2ap_eNB_generate_x2_setup_failure (assoc_id,
                                          X2ap_Cause_PR_protocol,
                                          X2ap_CauseProtocol_unspecified,
                                          -1);
      return -1;
    }

    /*
     * TODO: call the reset procedure
     */
  }

  return x2ap_generate_x2_setup_response (x2ap_eNB_data);

}

static
int x2ap_eNB_handle_x2_setup_failure(uint32_t               assoc_id,
                                     uint32_t               stream,
                                     struct x2ap_message_s *message_p)
{

  X2SetupFailure_IEs_t   *x2_setup_failure;
  x2ap_eNB_data_t        *x2ap_eNB_data;

  DevAssert(message_p != NULL);

  x2_setup_failure = &message_p->msg.x2SetupFailure_IEs;

  /* x2 Setup Failure == Non UE-related procedure -> stream 0 */
  if (stream != 0) {
    X2AP_WARN("[SCTP %d] Received x2 setup failure on stream != 0 (%d)\n",
    assoc_id, stream);
  }

  if ((x2ap_eNB_data = x2ap_get_eNB (NULL, assoc_id, 0)) == NULL) {
    X2AP_ERROR("[SCTP %d] Received X2 setup response for non existing "
    "eNB context\n", assoc_id);
    return -1;
}

  // need a FSM to handle all cases
  if ((x2_setup_failure->cause.present == X2ap_Cause_PR_misc) &&
      (x2_setup_failure->cause.choice.misc == X2ap_CauseMisc_unspecified)) {
    X2AP_WARN("Received X2 setup failure for eNB ... eNB is not ready\n");
  } else {
    X2AP_ERROR("Received x2 setup failure for eNB... please check your parameters\n");
  }

  x2ap_eNB_data->state = X2AP_ENB_STATE_WAITING;
  x2ap_handle_x2_setup_message(x2ap_eNB_data, 0);

  return 0;
}

static
int x2ap_eNB_handle_x2_setup_response(uint32_t               assoc_id,
                                      uint32_t               stream,
                                      struct x2ap_message_s *message)
{

  X2SetupResponse_IEs_t *x2SetupResponse;
  x2ap_eNB_data_t       *x2ap_eNB_data;
  uint32_t                           eNB_id = 0;

  DevAssert(message != NULL);
  x2SetupResponse = &message->msg.x2SetupResponse_IEs;

  MSC_LOG_RX_MESSAGE (MSC_X2AP_TARGET_ENB,
                      MSC_X2AP_SRC_ENB, NULL, 0,
                      "0 X2Setup/%s assoc_id %u stream %u",
                      x2ap_direction2String[message->direction],
                      assoc_id, stream);

  /* X2 Setup Response == Non UE-related procedure -> stream 0 */
  if (stream != 0) {
    X2AP_ERROR("[SCTP %d] Received X2 setup response on stream != 0 (%d)\n",
               assoc_id, stream);
    return -1;
  }

  if ((x2ap_eNB_data = x2ap_get_eNB(NULL, assoc_id, 0)) == NULL) {
    X2AP_ERROR("[SCTP %d] Received X2 setup response for non existing "
               "MME context\n", assoc_id);
    return -1;
  }

  /* check and store eNB info here*/
  if (x2SetupResponse->globalENB_ID.eNB_ID.present == X2ap_ENB_ID_PR_home_eNB_ID) {
    // Home eNB ID = 28 bits
    uint8_t  *eNB_id_buf = x2SetupResponse->globalENB_ID.eNB_ID.choice.home_eNB_ID.buf;

    if (x2SetupResponse->globalENB_ID.eNB_ID.choice.macro_eNB_ID.size != 28) {
      //TODO: handle case were size != 28 -> notify ? reject ?
    }

    eNB_id = (eNB_id_buf[0] << 20) + (eNB_id_buf[1] << 12) + (eNB_id_buf[2] << 4) + ((eNB_id_buf[3] & 0xf0) >> 4);
    X2AP_DEBUG ("Home eNB id: %07x\n", eNB_id);
  } else {
    // Macro eNB = 20 bits
    uint8_t *eNB_id_buf = x2SetupResponse->globalENB_ID.eNB_ID.choice.macro_eNB_ID.buf;

    if (x2SetupResponse->globalENB_ID.eNB_ID.choice.macro_eNB_ID.size != 20) {
      //TODO: handle case were size != 20 -> notify ? reject ?
    }

    eNB_id = (eNB_id_buf[0] << 12) + (eNB_id_buf[1] << 4) + ((eNB_id_buf[2] & 0xf0) >> 4);
    X2AP_DEBUG ("macro eNB id: %05x\n", eNB_id);
  }

  if ((x2ap_eNB_data = x2ap_is_eNB_id_in_list (eNB_id)) == NULL) {
      /*
       * eNB has not been fount in list of associated eNB,
       * * * * Add it to the tail of list and initialize data
       */
    if ((x2ap_eNB_data = x2ap_is_eNB_assoc_id_in_list (assoc_id)) == NULL) {
      /*
       * ??
       */
      return -1;
    } else {
      x2ap_eNB_data->state = X2AP_ENB_STATE_RESETTING;
      x2ap_eNB_data->eNB_id = eNB_id;
    }


  } else {
    x2ap_eNB_data->state = X2AP_ENB_STATE_RESETTING;
    /*
    if (x2ap_eNB_data->assoc_id != assoc_id) {
      X2SetupFailure_IEs_t                x2SetupFailure;

      memset (&x2SetupFailure, 0, sizeof (x2SetupFailure));
      // Send an overload cause
      X2AP_ERROR ("Rejeting x2 setup response as eNB id %d is already associated to an active sctp association" "Previous known: %d, new one: %d\n", eNB_id, x2ap_eNB_data->assoc_id, assoc_id);
      x2ap_eNB_generate_x2_setup_failure (assoc_id,
                                          X2ap_Cause_PR_protocol,
                                          X2ap_CauseProtocol_unspecified,
                                          -1);
      return -1;
    }
    */

    /*
     * TODO: call the reset procedure
     */
  }


  /* Optionaly set the target eNB name */



 /* The association is now ready as source and target eNBs know parameters of each other.
   * Mark the association as connected.
   */
  x2ap_eNB_data->state = X2AP_ENB_STATE_READY;
  x2ap_eNB_data->x2ap_eNB_instance->x2_target_enb_associated_nb ++;
  x2ap_handle_x2_setup_message(x2ap_eNB_data, 0);


  return 0;
}


static
int x2ap_eNB_handle_error_indication(uint32_t               assoc_id,
                                     uint32_t               stream,
                                     struct x2ap_message_s *message_p)
{

  X2ap_ErrorIndication_IEs_t  *x2_error_indication;
  x2ap_eNB_data_t        *x2ap_eNB_data;
  char       *message_string = NULL;

  DevAssert(message_p != NULL);

  x2_error_indication = &message_p->msg.x2ap_ErrorIndication_IEs;

  /* X2 Setup Failure == Non UE-related procedure -> stream 0 */
  if (stream != 0) {
    X2AP_WARN("[SCTP %d] Received X2 Error indication on stream != 0 (%d)\n",
              assoc_id, stream);
  }

  x2ap_xer_print_x2ap_errorindication_(x2ap_xer__print2sp,
                                       message_string,
                                       message_p);

  if ( x2_error_indication->presenceMask & X2AP_ERRORINDICATION_IES_OLD_ENB_UE_X2AP_ID_PRESENT ) {
    X2AP_WARN("Received X2 Error indication OLD ENB UE X2AP ID 0x%x\n", x2_error_indication->old_eNB_UE_X2AP_ID);
  }

  if ( x2_error_indication->presenceMask & X2AP_ERRORINDICATION_IES_NEW_ENB_UE_X2AP_ID_PRESENT) {
    X2AP_WARN("Received X2 Error indication NEW eNB UE X2AP ID 0x%x\n", x2_error_indication->new_eNB_UE_X2AP_ID);
  }

  if ( x2_error_indication->presenceMask & X2AP_ERRORINDICATION_IES_CAUSE_PRESENT) {
    switch(x2_error_indication->cause.present) {
      case X2ap_Cause_PR_NOTHING:
            X2AP_WARN("Received X2 Error indication cause NOTHING\n");
      break;
      case X2ap_Cause_PR_radioNetwork:
              switch (x2_error_indication->cause.choice.radioNetwork) {
              case X2ap_CauseRadioNetwork_handover_desirable_for_radio_reasons:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_handover_desirable_for_radio_reasons\n");
            break;
                case X2ap_CauseRadioNetwork_time_critical_handover:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_time_critical_handover\n");
            break;
                case X2ap_CauseRadioNetwork_resource_optimisation_handover:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_resource_optimisation_handover\n");
            break;
                case X2ap_CauseRadioNetwork_reduce_load_in_serving_cell:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_reduce_load_in_serving_cell\n");
            break;
                case X2ap_CauseRadioNetwork_partial_handover:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_partial_handover\n");
            break;
                case X2ap_CauseRadioNetwork_unknown_new_eNB_UE_X2AP_ID:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_unknown_new_eNB_UE_X2AP_ID\n");
            break;
                case X2ap_CauseRadioNetwork_unknown_old_eNB_UE_X2AP_ID:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_unknown_old_eNB_UE_X2AP_ID\n");
            break;
                case X2ap_CauseRadioNetwork_unknown_pair_of_UE_X2AP_ID:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_unknown_pair_of_UE_X2AP_ID\n");
            break;
                case X2ap_CauseRadioNetwork_ho_target_not_allowed:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_ho_target_not_allowed\n");
            break;
                case X2ap_CauseRadioNetwork_tx2relocoverall_expiry:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_tx2relocoverall_expiry\n");
            break;
                case X2ap_CauseRadioNetwork_trelocprep_expiry:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_trelocprep_expiry\n");
            break;
                case X2ap_CauseRadioNetwork_cell_not_available:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_cell_not_available\n");
            break;
                case X2ap_CauseRadioNetwork_no_radio_resources_available_in_target_cell:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_no_radio_resources_available_in_target_cell\n");
            break;
                case X2ap_CauseRadioNetwork_invalid_MME_GroupID:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_invalid_MME_GroupID\n");
            break;
                case X2ap_CauseRadioNetwork_unknown_MME_Code:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_unknown_MME_Code\n");
            break;
                case X2ap_CauseRadioNetwork_encryption_and_or_integrity_protection_algorithms_not_supported:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_encryption_and_or_integrity_protection_algorithms_not_supported\n");
            break;
                case X2ap_CauseRadioNetwork_reportCharacteristicsEmpty:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_reportCharacteristicsEmpty\n");
            break;
                case X2ap_CauseRadioNetwork_noReportPeriodicity:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_noReportPeriodicity\n");
            break;
                case X2ap_CauseRadioNetwork_existingMeasurementID:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_existingMeasurementID\n");
            break;
                case X2ap_CauseRadioNetwork_unknown_eNB_Measurement_ID:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_unknown_eNB_Measurement_ID\n");
            break;
                case X2ap_CauseRadioNetwork_measurement_temporarily_not_available:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_measurement_temporarily_not_available\n");
            break;
                case X2ap_CauseRadioNetwork_unspecified:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_unspecified\n");
            break;
                case X2ap_CauseRadioNetwork_load_balancing:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_load_balancing\n");
            break;
                case X2ap_CauseRadioNetwork_handover_optimisation:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_handover_optimisation\n");
            break;
                case X2ap_CauseRadioNetwork_value_out_of_allowed_range:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_value_out_of_allowed_range\n");
            break;
                case X2ap_CauseRadioNetwork_multiple_E_RAB_ID_instances:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_multiple_E_RAB_ID_instances\n");
            break;
                case X2ap_CauseRadioNetwork_switch_off_ongoing:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_switch_off_ongoing\n");
            break;
                case X2ap_CauseRadioNetwork_not_supported_QCI_value:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_not_supported_QCI_value\n");
            break;
                case X2ap_CauseRadioNetwork_measurement_not_supported_for_the_object:
            X2AP_WARN("Received X2 Error indication X2ap_CauseRadioNetwork_measurement_not_supported_for_the_object\n");

                default:
            X2AP_WARN("Received X2 Error indication cause radio network case not handled\n");
              }
      break;

      case X2ap_Cause_PR_transport:
              switch (x2_error_indication->cause.choice.transport) {
              case X2ap_CauseTransport_transport_resource_unavailable:
            X2AP_WARN("Received X2 Error indication X2ap_CauseTransport_transport_resource_unavailable\n");
            break;
              case X2ap_CauseTransport_unspecified:
            X2AP_WARN("Received X2 Error indication SX2ap_CauseTransport_unspecified\n");
            break;
                default:
            X2AP_WARN("Received X2 Error indication cause transport case not handled\n");
              }
      break;

    case X2ap_Cause_PR_protocol:
              switch (x2_error_indication->cause.choice.protocol) {
                case X2ap_CauseProtocol_transfer_syntax_error:
            X2AP_WARN("Received X2 Error indication X2ap_CauseProtocol_transfer_syntax_error\n");
            break;
                case X2ap_CauseProtocol_abstract_syntax_error_reject:
            X2AP_WARN("Received X2 Error indication X2ap_CauseProtocol_abstract_syntax_error_reject\n");
            break;
                case X2ap_CauseProtocol_abstract_syntax_error_ignore_and_notify:
            X2AP_WARN("Received X2 Error indication X2ap_CauseProtocol_abstract_syntax_error_ignore_and_notify\n");
            break;
                case X2ap_CauseProtocol_message_not_compatible_with_receiver_state:
            X2AP_WARN("Received X2 Error indication X2ap_CauseProtocol_message_not_compatible_with_receiver_state\n");
            break;
                case X2ap_CauseProtocol_semantic_error:
            X2AP_WARN("Received X2 Error indication X2ap_CauseProtocol_semantic_error\n");
            break;
                 case X2ap_CauseProtocol_unspecified:
            X2AP_WARN("Received X2 Error indication X2ap_CauseProtocol_unspecified\n");
            break;
                case X2ap_CauseProtocol_abstract_syntax_error_falsely_constructed_message:
            X2AP_WARN("Received X2 Error indication X2ap_CauseProtocol_abstract_syntax_error_falsely_constructed_message\n");
            break;
               default:
            X2AP_WARN("Received X2 Error indication cause protocol case not handled\n");
              }
      break;

      case X2ap_Cause_PR_misc:
        switch (x2_error_indication->cause.choice.protocol) {
          case X2ap_CauseMisc_control_processing_overload:
            X2AP_WARN("Received X2 Error indication X2ap_CauseMisc_control_processing_overload\n");
            break;
          case X2ap_CauseMisc_hardware_failure:
                X2AP_WARN("Received X2 Error indication X2ap_CauseMisc_hardware_failure\n");
                break;
          case X2ap_CauseMisc_om_intervention:
                X2AP_WARN("Received X2 Error indication X2ap_CauseMisc_om_intervention\n");
                break;
          case X2ap_CauseMisc_not_enough_user_plane_processing_resources:
                X2AP_WARN("Received X2 Error indication X2ap_CauseMisc_not_enough_user_plane_processing_resources\n");
                break;
          case X2ap_CauseMisc_unspecified:
                X2AP_WARN("Received X2 Error indication X2ap_CauseMisc_unspecified\n");
                break;
        default:
            X2AP_WARN("Received X2 Error indication cause misc case not handled\n");
        }
      break;
    }
  }
  if ( x2_error_indication->presenceMask &X2AP_ERRORINDICATION_IES_CRITICALITYDIAGNOSTICS_PRESENT) {
    // TODO continue
  }
  // TODO continue
  return 0;
}