From fefb6148e69619215e0a13d77bd290c7a2940e78 Mon Sep 17 00:00:00 2001
From: Xue Song <xuesong@cn.fujitsu.com>
Date: Tue, 29 Sep 2020 06:11:02 +0000
Subject: [PATCH] Modify some warnings Add nr_rrc_pdcp_config_security Delete
 ENABLE_SECURITY

---
 openair2/RRC/NR/MESSAGES/asn1_msg.c | 51 +++++++++++++++++++-
 openair2/RRC/NR/MESSAGES/asn1_msg.h |  4 ++
 openair2/RRC/NR/nr_rrc_defs.h       |  5 +-
 openair2/RRC/NR/rrc_gNB.c           | 31 +++++++++---
 openair2/RRC/NR/rrc_gNB_NGAP.c      | 74 ++++++++++++++++++++++++++++-
 5 files changed, 152 insertions(+), 13 deletions(-)

diff --git a/openair2/RRC/NR/MESSAGES/asn1_msg.c b/openair2/RRC/NR/MESSAGES/asn1_msg.c
index 3efefd43ef..f6a1dc583a 100644
--- a/openair2/RRC/NR/MESSAGES/asn1_msg.c
+++ b/openair2/RRC/NR/MESSAGES/asn1_msg.c
@@ -1016,7 +1016,7 @@ uint16_t do_RRCReconfiguration(
 
     /******************** Secondary Cell Group ********************/
     rrc_gNB_carrier_data_t *carrier = &(gnb_rrc_inst->carrier);
-    fill_default_secondaryCellGroup( carrier->ServingCellConfigCommon,
+    fill_default_secondaryCellGroup( carrier->servingcellconfigcommon,
                                      ue_context_pP->ue_context.secondaryCellGroup,
                                      1,
                                      1,
@@ -1063,3 +1063,52 @@ uint16_t do_RRCReconfiguration(
 
     return((enc_rval.encoded+7)/8);
 }
+
+
+uint8_t do_RRCSetupRequest(uint8_t Mod_id, uint8_t *buffer,uint8_t *rv) {
+  asn_enc_rval_t enc_rval;
+  uint8_t buf[5],buf2=0;
+  NR_UL_CCCH_Message_t ul_ccch_msg;
+  NR_RRCSetupRequest_t *rrcSetupRequest;
+  memset((void *)&ul_ccch_msg,0,sizeof(NR_UL_CCCH_Message_t));
+  ul_ccch_msg.message.present           = NR_UL_CCCH_MessageType_PR_c1;
+  ul_ccch_msg.message.choice.c1->present = NR_UL_CCCH_MessageType__c1_PR_rrcSetupRequest;
+  rrcSetupRequest          = ul_ccch_msg.message.choice.c1->choice.rrcSetupRequest;
+
+
+  if (1) {
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.present = NR_InitialUE_Identity_PR_randomValue;
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.randomValue.size = 5;
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.randomValue.bits_unused = 0;
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.randomValue.buf = buf;
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.randomValue.buf[0] = rv[0];
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.randomValue.buf[1] = rv[1];
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.randomValue.buf[2] = rv[2];
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.randomValue.buf[3] = rv[3];
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.randomValue.buf[4] = rv[4];
+  } else {
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.present = NR_InitialUE_Identity_PR_ng_5G_S_TMSI_Part1;
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.ng_5G_S_TMSI_Part1.size = 1;
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.ng_5G_S_TMSI_Part1.bits_unused = 0;
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.ng_5G_S_TMSI_Part1.buf = buf;
+    rrcSetupRequest->rrcSetupRequest.ue_Identity.choice.ng_5G_S_TMSI_Part1.buf[0] = 0x12;
+  }
+
+  rrcSetupRequest->rrcSetupRequest.establishmentCause = NR_EstablishmentCause_mo_Signalling; //EstablishmentCause_mo_Data;
+  rrcSetupRequest->rrcSetupRequest.spare.buf = &buf2;
+  rrcSetupRequest->rrcSetupRequest.spare.size=1;
+  rrcSetupRequest->rrcSetupRequest.spare.bits_unused = 7;
+
+  if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
+    xer_fprint(stdout, &asn_DEF_NR_UL_CCCH_Message, (void *)&ul_ccch_msg);
+  }
+
+  enc_rval = uper_encode_to_buffer(&asn_DEF_NR_UL_CCCH_Message,
+                                   NULL,
+                                   (void *)&ul_ccch_msg,
+                                   buffer,
+                                   100);
+  AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %lu)!\n", enc_rval.failed_type->name, enc_rval.encoded);
+  LOG_D(RRC,"[UE] RRCSetupRequest Encoded %zd bits (%zd bytes)\n", enc_rval.encoded, (enc_rval.encoded+7)/8);
+  return((enc_rval.encoded+7)/8);
+}
diff --git a/openair2/RRC/NR/MESSAGES/asn1_msg.h b/openair2/RRC/NR/MESSAGES/asn1_msg.h
index 2222f8a3f9..fa424fb7a4 100644
--- a/openair2/RRC/NR/MESSAGES/asn1_msg.h
+++ b/openair2/RRC/NR/MESSAGES/asn1_msg.h
@@ -99,6 +99,10 @@ uint8_t do_NR_SecurityModeCommand(
                     const uint8_t cipheringAlgorithm,
                     NR_IntegrityProtAlgorithm_t *integrityProtAlgorithm);
 
+uint8_t do_NR_UECapabilityEnquiry_nr( const protocol_ctxt_t *const ctxt_pP,
+                                   uint8_t               *const buffer,
+                                   const uint8_t                Transaction_id);
+
 uint8_t do_NR_RRCConnectionRelease(uint8_t *buffer,
                                    uint8_t Transaction_id);
 uint16_t do_RRCReconfiguration(
diff --git a/openair2/RRC/NR/nr_rrc_defs.h b/openair2/RRC/NR/nr_rrc_defs.h
index cceb294dd6..a9a8c80ed1 100644
--- a/openair2/RRC/NR/nr_rrc_defs.h
+++ b/openair2/RRC/NR/nr_rrc_defs.h
@@ -134,8 +134,6 @@ typedef enum UE_STATE_NR_e {
 /* TS 36.331: RRC-TransactionIdentifier ::= INTEGER (0..3) */
 #define NR_RRC_TRANSACTION_IDENTIFIER_NUMBER             3
 
-#define ENABLE_SECURITY 1
-
 typedef struct {
   unsigned short                                      transport_block_size;      /*!< \brief Minimum PDU size in bytes provided by RLC to MAC layer interface */
   unsigned short                                      max_transport_blocks;      /*!< \brief Maximum PDU size in bytes provided by RLC to MAC layer interface */
@@ -289,13 +287,12 @@ typedef struct gNB_RRC_UE_s {
 
   ImsiMobileIdentity_t               imsi;
 
-#if defined(ENABLE_SECURITY)
   /* KgNB as derived from KASME received from EPC */
   uint8_t kgnb[32];
   int8_t  kgnb_ncc;
   uint8_t nh[32];
   int8_t  nh_ncc;
-#endif
+
   /* Used integrity/ciphering algorithms */
   NR_CipheringAlgorithm_t            ciphering_algorithm;
   e_NR_IntegrityProtAlgorithm        integrity_algorithm;
diff --git a/openair2/RRC/NR/rrc_gNB.c b/openair2/RRC/NR/rrc_gNB.c
index 771e797fa7..9612bf625d 100644
--- a/openair2/RRC/NR/rrc_gNB.c
+++ b/openair2/RRC/NR/rrc_gNB.c
@@ -68,9 +68,7 @@
 #include "OCG.h"
 #include "OCG_extern.h"
 
-#if defined(ENABLE_SECURITY)
-  #include "UTIL/OSA/osa_defs.h"
-#endif
+#include "UTIL/OSA/osa_defs.h"
 
 #include "rrc_eNB_S1AP.h"
 #include "rrc_gNB_NGAP.h"
@@ -92,6 +90,28 @@
 
 extern RAN_CONTEXT_t RC;
 
+extern boolean_t nr_rrc_pdcp_config_asn1_req(
+    const protocol_ctxt_t *const  ctxt_pP,
+    NR_SRB_ToAddModList_t  *const srb2add_list,
+    NR_DRB_ToAddModList_t  *const drb2add_list,
+    NR_DRB_ToReleaseList_t *const drb2release_list,
+    const uint8_t                   security_modeP,
+    uint8_t                  *const kRRCenc,
+    uint8_t                  *const kRRCint,
+    uint8_t                  *const kUPenc
+  #if (LTE_RRC_VERSION >= MAKE_VERSION(9, 0, 0))
+    ,LTE_PMCH_InfoList_r9_t  *pmch_InfoList_r9
+  #endif
+    ,rb_id_t                 *const defaultDRB,
+    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);
+
+extern rlc_op_status_t nr_rrc_rlc_config_asn1_req (const protocol_ctxt_t   * const ctxt_pP,
+    const NR_SRB_ToAddModList_t   * const srb2add_listP,
+    const NR_DRB_ToAddModList_t   * const drb2add_listP,
+    const NR_DRB_ToReleaseList_t  * const drb2release_listP,
+    const LTE_PMCH_InfoList_r9_t * const pmch_InfoList_r9_pP,
+    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);
+
 mui_t                               rrc_gNB_mui = 0;
 
 ///---------------------------------------------------------------------------------------------------------------///
@@ -570,6 +590,7 @@ rrc_gNB_process_RRCReconfigurationComplete(
                                 kRRCint,
                                 kUPenc,
                                 NULL,
+                                NULL,
                                 NULL);
     /* Refresh SRBs/DRBs */
     nr_rrc_rlc_config_asn1_req(ctxt_pP,
@@ -577,9 +598,7 @@ rrc_gNB_process_RRCReconfigurationComplete(
                             DRB_configList,
                             DRB_Release_configList2,
                             NULL,
-                            0,
-                            0
-                            );
+                            NULL);
 
     /* Loop through DRBs and establish if necessary */
     if (DRB_configList != NULL) {
diff --git a/openair2/RRC/NR/rrc_gNB_NGAP.c b/openair2/RRC/NR/rrc_gNB_NGAP.c
index 9d81462d40..b615cb833e 100644
--- a/openair2/RRC/NR/rrc_gNB_NGAP.c
+++ b/openair2/RRC/NR/rrc_gNB_NGAP.c
@@ -47,6 +47,7 @@
 
 #include "S1AP_NAS-PDU.h"
 #include "executables/softmodem-common.h"
+#include "UTIL/OSA/osa_defs.h"
 
 extern RAN_CONTEXT_t RC;
 
@@ -115,6 +116,75 @@ rrc_gNB_get_ue_context_from_ngap_ids(
     return NULL;
 }
 
+//------------------------------------------------------------------------------
+void
+nr_rrc_pdcp_config_security(
+    const protocol_ctxt_t  *const ctxt_pP,
+    rrc_gNB_ue_context_t   *const ue_context_pP,
+    const uint8_t          send_security_mode_command
+)
+//------------------------------------------------------------------------------
+{
+    NR_SRB_ToAddModList_t              *SRB_configList = ue_context_pP->ue_context.SRB_configList;
+    uint8_t                            *kRRCenc = NULL;
+    uint8_t                            *kRRCint = NULL;
+    uint8_t                            *kUPenc = NULL;
+    pdcp_t                             *pdcp_p   = NULL;
+    static int                          print_keys= 1;
+    hashtable_rc_t                      h_rc;
+    hash_key_t                          key;
+
+    /* Derive the keys from kgnb */
+    if (SRB_configList != NULL) {
+        derive_key_up_enc(ue_context_pP->ue_context.ciphering_algorithm,
+                        ue_context_pP->ue_context.kgnb,
+                        &kUPenc);
+    }
+
+    derive_key_rrc_enc(ue_context_pP->ue_context.ciphering_algorithm,
+                        ue_context_pP->ue_context.kgnb,
+                        &kRRCenc);
+    derive_key_rrc_int(ue_context_pP->ue_context.integrity_algorithm,
+                        ue_context_pP->ue_context.kgnb,
+                        &kRRCint);
+    if (!IS_SOFTMODEM_IQPLAYER) {
+        SET_LOG_DUMP(DEBUG_SECURITY) ;
+    }
+
+
+    if ( LOG_DUMPFLAG( DEBUG_SECURITY ) ) {
+        if (print_keys == 1 ) {
+        print_keys =0;
+        LOG_DUMPMSG(NR_RRC, DEBUG_SECURITY, ue_context_pP->ue_context.kgnb, 32,"\nKgNB:" );
+        LOG_DUMPMSG(NR_RRC, DEBUG_SECURITY, kRRCenc, 32,"\nKRRCenc:" );
+        LOG_DUMPMSG(NR_RRC, DEBUG_SECURITY, kRRCint, 32,"\nKRRCint:" );
+        }
+    }
+
+    key = PDCP_COLL_KEY_VALUE(ctxt_pP->module_id, ctxt_pP->rnti, ctxt_pP->enb_flag, DCCH, SRB_FLAG_YES);
+    h_rc = hashtable_get(pdcp_coll_p, key, (void **)&pdcp_p);
+
+    if (h_rc == HASH_TABLE_OK) {
+        pdcp_config_set_security(
+        ctxt_pP,
+        pdcp_p,
+        DCCH,
+        DCCH+2,
+        (send_security_mode_command == TRUE)  ?
+        0 | (ue_context_pP->ue_context.integrity_algorithm << 4) :
+        (ue_context_pP->ue_context.ciphering_algorithm )         |
+        (ue_context_pP->ue_context.integrity_algorithm << 4),
+        kRRCenc,
+        kRRCint,
+        kUPenc);
+    } else {
+        LOG_E(NR_RRC,
+            PROTOCOL_NR_RRC_CTXT_UE_FMT"Could not get PDCP instance for SRB DCCH %u\n",
+            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
+            DCCH);
+    }
+}
+
 //------------------------------------------------------------------------------
 /*
 * Initial UE NAS message on S1AP.
@@ -280,7 +350,7 @@ rrc_gNB_process_NGAP_INITIAL_CONTEXT_SETUP_REQ(
         
         uint8_t send_security_mode_command = TRUE;
 
-        rrc_pdcp_config_security(
+        nr_rrc_pdcp_config_security(
             &ctxt,
             ue_context_p,
             send_security_mode_command);
@@ -289,7 +359,7 @@ rrc_gNB_process_NGAP_INITIAL_CONTEXT_SETUP_REQ(
             rrc_gNB_generate_SecurityModeCommand (&ctxt, ue_context_p);
             send_security_mode_command = FALSE;
 
-            rrc_pdcp_config_security(
+            nr_rrc_pdcp_config_security(
                 &ctxt,
                 ue_context_p,
                 send_security_mode_command);
-- 
2.26.2