From 074cf4526ff630c06c4efd111f242db3dd9eb497 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Wed, 17 Jan 2018 19:50:24 +0000 Subject: [PATCH] Cleaned up the handling of the Initial UE message. --- srsepc/hdr/mme/s1ap_nas_transport.h | 18 ++ srsepc/src/mme/s1ap.cc | 2 +- srsepc/src/mme/s1ap_nas_transport.cc | 256 +++++++++++++++++++++++++-- 3 files changed, 261 insertions(+), 15 deletions(-) diff --git a/srsepc/hdr/mme/s1ap_nas_transport.h b/srsepc/hdr/mme/s1ap_nas_transport.h index 8b63520b3..de1d67551 100644 --- a/srsepc/hdr/mme/s1ap_nas_transport.h +++ b/srsepc/hdr/mme/s1ap_nas_transport.h @@ -47,6 +47,24 @@ public: bool handle_initial_ue_message(LIBLTE_S1AP_MESSAGE_INITIALUEMESSAGE_STRUCT *init_ue, struct sctp_sndrcvinfo *enb_sri, srslte::byte_buffer_t *reply_buffer, bool *reply_flag); bool handle_uplink_nas_transport(LIBLTE_S1AP_MESSAGE_UPLINKNASTRANSPORT_STRUCT *ul_xport, struct sctp_sndrcvinfo *enb_sri, srslte::byte_buffer_t *reply_buffer, bool *reply_flag); + bool handle_nas_attach_request( uint32_t enb_ue_s1ap_id, + const LIBLTE_MME_ATTACH_REQUEST_MSG_STRUCT &attach_req, + const LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT &pdn_con_req, + srslte::byte_buffer_t *reply_buffer, + bool* reply_flag, + struct sctp_sndrcvinfo *enb_sri); + bool handle_nas_imsi_attach_request(uint32_t enb_ue_s1ap_id, + const LIBLTE_MME_ATTACH_REQUEST_MSG_STRUCT &attach_req, + const LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT &pdn_con_req, + srslte::byte_buffer_t *reply_buffer, + bool* reply_flag, + struct sctp_sndrcvinfo *enb_sri); + bool handle_nas_guti_attach_request(uint32_t enb_ue_s1ap_id, + const LIBLTE_MME_ATTACH_REQUEST_MSG_STRUCT &attach_req, + const LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT &pdn_con_req, + srslte::byte_buffer_t *reply_buffer, + bool* reply_flag, + struct sctp_sndrcvinfo *enb_sri); bool handle_nas_authentication_response(srslte::byte_buffer_t *nas_msg, ue_ctx_t *ue_ctx, srslte::byte_buffer_t *reply_buffer, bool* reply_flag); bool handle_nas_security_mode_complete(srslte::byte_buffer_t *nas_msg, ue_ctx_t *ue_ctx, srslte::byte_buffer_t *reply_buffer, bool *reply_flag); bool handle_nas_attach_complete(srslte::byte_buffer_t *nas_msg, ue_ctx_t *ue_ctx, srslte::byte_buffer_t *reply_buffer, bool *reply_flag); diff --git a/srsepc/src/mme/s1ap.cc b/srsepc/src/mme/s1ap.cc index 3f4003029..07563e7ca 100644 --- a/srsepc/src/mme/s1ap.cc +++ b/srsepc/src/mme/s1ap.cc @@ -285,7 +285,7 @@ s1ap::handle_initiating_message(LIBLTE_S1AP_INITIATINGMESSAGE_STRUCT *msg, stru ssize_t n_sent = sctp_send(m_s1mme,reply_buffer->msg, reply_buffer->N_bytes, enb_sri, 0); if(n_sent == -1) { - m_s1ap_log->console("Failed to send S1 Setup Setup Reply"); + m_s1ap_log->console("Failed to send S1 Setup Setup Reply\n"); m_pool->deallocate(reply_buffer); return false; } diff --git a/srsepc/src/mme/s1ap_nas_transport.cc b/srsepc/src/mme/s1ap_nas_transport.cc index 6438dbe63..fb8687277 100644 --- a/srsepc/src/mme/s1ap_nas_transport.cc +++ b/srsepc/src/mme/s1ap_nas_transport.cc @@ -82,19 +82,11 @@ s1ap_nas_transport::handle_initial_ue_message(LIBLTE_S1AP_MESSAGE_INITIALUEMESSA LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT pdn_con_req; LIBLTE_MME_SERVICE_REQUEST_MSG_STRUCT service_req; - uint64_t imsi = 0; - uint8_t k_asme[32]; - uint8_t autn[16]; - uint8_t rand[6]; - uint8_t xres[8]; - - ue_ctx_t ue_ctx; - m_s1ap_log->console("Received Initial UE Message.\n"); m_s1ap_log->info("Received Initial UE Message.\n"); //Get info from initial UE message - ue_ctx.enb_ue_s1ap_id = init_ue->eNB_UE_S1AP_ID.ENB_UE_S1AP_ID; + uint32_t enb_ue_s1ap_id = init_ue->eNB_UE_S1AP_ID.ENB_UE_S1AP_ID; //Log unhandled Initial UE message IEs log_unhandled_initial_ue_message_ies(init_ue); @@ -108,12 +100,26 @@ s1ap_nas_transport::handle_initial_ue_message(LIBLTE_S1AP_MESSAGE_INITIALUEMESSA if(msg_type == LIBLTE_MME_MSG_TYPE_ATTACH_REQUEST) { //Get NAS Attach Request and PDN connectivity request messages - if(!unpack_initial_ue_message(init_ue, &attach_req,&pdn_con_req)) + LIBLTE_ERROR_ENUM err = liblte_mme_unpack_attach_request_msg((LIBLTE_BYTE_MSG_STRUCT *) nas_msg, &attach_req); + if(err != LIBLTE_SUCCESS){ + m_s1ap_log->error("Error unpacking NAS attach request. Error: %s\n", liblte_error_text[err]); + m_pool->deallocate(nas_msg); + return false; + } + /*Get PDN Connectivity Request*/ + err = liblte_mme_unpack_pdn_connectivity_request_msg(&attach_req.esm_msg, &pdn_con_req); + if(err != LIBLTE_SUCCESS){ + m_s1ap_log->error("Error unpacking NAS PDN Connectivity Request. Error: %s\n", liblte_error_text[err]); + m_pool->deallocate(nas_msg); + return false; + } + /* if(!unpack_initial_ue_message(init_ue, &attach_req,&pdn_con_req)) { //Could not decode the attach request and the PDN connectivity request. m_s1ap_log->error("Could not unpack NAS Attach Request and PDN connectivity request.\n"); return false; - } + }*/ + handle_nas_attach_request(enb_ue_s1ap_id, attach_req,pdn_con_req,reply_buffer,reply_flag, enb_sri); } else if(msg_type == LIBLTE_MME_SECURITY_HDR_TYPE_SERVICE_REQUEST) { @@ -123,7 +129,9 @@ s1ap_nas_transport::handle_initial_ue_message(LIBLTE_S1AP_MESSAGE_INITIALUEMESSA return false; } m_pool->deallocate(nas_msg); + return true; + /* //Create basic UE Ctx ue_ctx_t *ue_ctx_ptr = &ue_ctx; ue_ctx.imsi = 0; @@ -244,6 +252,7 @@ s1ap_nas_transport::handle_initial_ue_message(LIBLTE_S1AP_MESSAGE_INITIALUEMESSA m_s1ap_log->info("Sending Athentication Request\n"); m_s1ap_log->console("Sending Athentication Request\n"); //TODO Start T3460 Timer! + */ return true; } @@ -302,7 +311,7 @@ s1ap_nas_transport::handle_uplink_nas_transport(LIBLTE_S1AP_MESSAGE_UPLINKNASTRA break; case LIBLTE_MME_MSG_TYPE_IDENTITY_RESPONSE: m_s1ap_log->info("Uplink NAS: Received Identity Response\n"); - m_s1ap_log->info("Uplink NAS: Received Identity Response\n"); + m_s1ap_log->console("Uplink NAS: Received Identity Response\n"); handle_identity_response(nas_msg, ue_ctx, reply_buffer, reply_flag); //ue_ctx->security_ctxt.ul_nas_count++; break; @@ -326,6 +335,225 @@ s1ap_nas_transport::handle_uplink_nas_transport(LIBLTE_S1AP_MESSAGE_UPLINKNASTRA return true; } +bool +s1ap_nas_transport::handle_nas_attach_request(uint32_t enb_ue_s1ap_id, + const LIBLTE_MME_ATTACH_REQUEST_MSG_STRUCT &attach_req, + const LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT &pdn_con_req, + srslte::byte_buffer_t *reply_buffer, + bool* reply_flag, + struct sctp_sndrcvinfo *enb_sri) +{ + //Get attach type from attach request + if(attach_req.eps_mobile_id.type_of_id == LIBLTE_MME_EPS_MOBILE_ID_TYPE_IMSI) + { + m_s1ap_log->console("Attach Request -- IMSI-style attach request\n"); + m_s1ap_log->info("Attach Request -- IMSI-style attach request\n"); + handle_nas_imsi_attach_request(enb_ue_s1ap_id, attach_req, pdn_con_req, reply_buffer, reply_flag, enb_sri); + } + else if(attach_req.eps_mobile_id.type_of_id == LIBLTE_MME_EPS_MOBILE_ID_TYPE_GUTI) + { + m_s1ap_log->console("Attach Request -- GUTI-style attach request\n"); + m_s1ap_log->info("Attach Request -- GUTI-style attach request\n"); + handle_nas_guti_attach_request(enb_ue_s1ap_id, attach_req, pdn_con_req, reply_buffer, reply_flag, enb_sri); + } + else + { + m_s1ap_log->error("Unhandle Mobile Id type in attach request\n"); + return false; + } + return true; +} + +bool +s1ap_nas_transport::handle_nas_imsi_attach_request(uint32_t enb_ue_s1ap_id, + const LIBLTE_MME_ATTACH_REQUEST_MSG_STRUCT &attach_req, + const LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT &pdn_con_req, + srslte::byte_buffer_t *reply_buffer, + bool* reply_flag, + struct sctp_sndrcvinfo *enb_sri) +{ + uint8_t k_asme[32]; + uint8_t autn[16]; + uint8_t rand[6]; + uint8_t xres[8]; + + ue_ctx_t ue_ctx; + ue_ctx.imsi = 0; + ue_ctx.enb_ue_s1ap_id = enb_ue_s1ap_id; + ue_ctx.mme_ue_s1ap_id = m_s1ap->get_next_mme_ue_s1ap_id(); + + //Save UE network capabilities + memcpy(&ue_ctx.ue_network_cap, &attach_req.ue_network_cap, sizeof(LIBLTE_MME_UE_NETWORK_CAPABILITY_STRUCT)); + ue_ctx.ms_network_cap_present = attach_req.ms_network_cap_present; + if(attach_req.ms_network_cap_present) + { + memcpy(&ue_ctx.ms_network_cap, &attach_req.ms_network_cap, sizeof(LIBLTE_MME_MS_NETWORK_CAPABILITY_STRUCT)); + } + uint8_t eps_bearer_id = pdn_con_req.eps_bearer_id; //TODO: Unused + ue_ctx.procedure_transaction_id = pdn_con_req.proc_transaction_id; + + //Save whether ESM information transfer is necessary + ue_ctx.eit = pdn_con_req.esm_info_transfer_flag_present; + //m_s1ap_log->console("EPS Bearer id: %d\n", eps_bearer_id); + //Initialize NAS count + ue_ctx.security_ctxt.ul_nas_count = 0; + ue_ctx.security_ctxt.dl_nas_count = 0; + //Add eNB info to UE ctxt + memcpy(&ue_ctx.enb_sri, enb_sri, sizeof(struct sctp_sndrcvinfo)); + //Initialize E-RABs + for(uint i = 0 ; i< MAX_ERABS_PER_UE; i++) + { + ue_ctx.erabs_ctx[i].state = ERAB_DEACTIVATED; + ue_ctx.erabs_ctx[i].erab_id = i; + } + + //IMSI style attach + ue_ctx.imsi = 0; + for(int i=0;i<=14;i++){ + ue_ctx.imsi += attach_req.eps_mobile_id.imsi[i]*std::pow(10,14-i); + } + + m_s1ap_log->console("Attach request -- IMSI: %015lu\n", ue_ctx.imsi); + m_s1ap_log->info("Attach request -- IMSI: %015lu\n", ue_ctx.imsi); + m_s1ap_log->console("Attach request -- eNB-UE S1AP Id: %d, MME-UE S1AP Id: %d\n", ue_ctx.enb_ue_s1ap_id, ue_ctx.mme_ue_s1ap_id); + m_s1ap_log->console("Attach Request -- UE Network Capabilities EEA: %d%d%d%d%d%d%d%d\n", + attach_req.ue_network_cap.eea[0], + attach_req.ue_network_cap.eea[1], + attach_req.ue_network_cap.eea[2], + attach_req.ue_network_cap.eea[3], + attach_req.ue_network_cap.eea[4], + attach_req.ue_network_cap.eea[5], + attach_req.ue_network_cap.eea[6], + attach_req.ue_network_cap.eea[7]); + m_s1ap_log->console("Attach Request -- UE Network Capabilities EIA: %d%d%d%d%d%d%d%d\n", + attach_req.ue_network_cap.eia[0], + attach_req.ue_network_cap.eia[1], + attach_req.ue_network_cap.eia[2], + attach_req.ue_network_cap.eia[3], + attach_req.ue_network_cap.eia[4], + attach_req.ue_network_cap.eia[5], + attach_req.ue_network_cap.eia[6], + attach_req.ue_network_cap.eia[7]); + m_s1ap_log->console("Attach Request -- MS Network Capabilities Present: %s\n", attach_req.ms_network_cap_present ? "true" : "false"); + m_s1ap_log->console("PDN Connectivity Request -- EPS Bearer Identity requested: %d\n", pdn_con_req.eps_bearer_id); + m_s1ap_log->console("PDN Connectivity Request -- Procedure Transaction Id: %d\n", pdn_con_req.proc_transaction_id); + m_s1ap_log->console("PDN Connectivity Request -- ESM Information Transfer requested: %s\n", pdn_con_req.esm_info_transfer_flag_present ? "true" : "false"); + + //Get Authentication Vectors from HSS + if(!m_hss->gen_auth_info_answer(ue_ctx.imsi, ue_ctx.security_ctxt.k_asme, autn, rand, ue_ctx.security_ctxt.xres)) + { + m_s1ap_log->console("User not found. IMSI %015lu\n",ue_ctx.imsi); + m_s1ap_log->info("User not found. IMSI %015lu\n",ue_ctx.imsi); + return false; + } + + m_s1ap->add_new_ue_ctx(ue_ctx); + //Pack NAS Authentication Request in Downlink NAS Transport msg + pack_authentication_request(reply_buffer, ue_ctx.enb_ue_s1ap_id, ue_ctx.mme_ue_s1ap_id, autn, rand); + + //Send reply to eNB + *reply_flag = true; + m_s1ap_log->info("Downlink NAS: Sending Athentication Request\n"); + m_s1ap_log->console("Downlink NAS: Sending Athentication Request\n"); + return true; +} + +bool +s1ap_nas_transport::handle_nas_guti_attach_request(uint32_t enb_ue_s1ap_id, + const LIBLTE_MME_ATTACH_REQUEST_MSG_STRUCT &attach_req, + const LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT &pdn_con_req, + srslte::byte_buffer_t *reply_buffer, + bool* reply_flag, + struct sctp_sndrcvinfo *enb_sri) +{ + //GUTI style attach + uint32_t m_tmsi = attach_req.eps_mobile_id.guti.m_tmsi; + std::map::iterator it = m_s1ap->m_tmsi_to_s1ap_id.find(m_tmsi); + if(it == m_s1ap->m_tmsi_to_s1ap_id.end()) + { + //Could not find IMSI from M-TMSI, send Id request + ue_ctx_t ue_ctx; + ue_ctx.imsi = 0; + ue_ctx.enb_ue_s1ap_id = enb_ue_s1ap_id; + ue_ctx.mme_ue_s1ap_id = m_s1ap->get_next_mme_ue_s1ap_id(); + + //Save UE network capabilities + memcpy(&ue_ctx.ue_network_cap, &attach_req.ue_network_cap, sizeof(LIBLTE_MME_UE_NETWORK_CAPABILITY_STRUCT)); + ue_ctx.ms_network_cap_present = attach_req.ms_network_cap_present; + if(attach_req.ms_network_cap_present) + { + memcpy(&ue_ctx.ms_network_cap, &attach_req.ms_network_cap, sizeof(LIBLTE_MME_MS_NETWORK_CAPABILITY_STRUCT)); + } + uint8_t eps_bearer_id = pdn_con_req.eps_bearer_id; //TODO: Unused + ue_ctx.procedure_transaction_id = pdn_con_req.proc_transaction_id; + + //Save whether ESM information transfer is necessary + ue_ctx.eit = pdn_con_req.esm_info_transfer_flag_present; + //m_s1ap_log->console("EPS Bearer id: %d\n", eps_bearer_id); + //Initialize NAS count + ue_ctx.security_ctxt.ul_nas_count = 0; + ue_ctx.security_ctxt.dl_nas_count = 0; + //Add eNB info to UE ctxt + memcpy(&ue_ctx.enb_sri, enb_sri, sizeof(struct sctp_sndrcvinfo)); + //Initialize E-RABs + for(uint i = 0 ; i< MAX_ERABS_PER_UE; i++) + { + ue_ctx.erabs_ctx[i].state = ERAB_DEACTIVATED; + ue_ctx.erabs_ctx[i].erab_id = i; + } + m_s1ap_log->console("Attach request -- IMSI: %015lu\n", ue_ctx.imsi); + m_s1ap_log->info("Attach request -- IMSI: %015lu\n", ue_ctx.imsi); + m_s1ap_log->console("Attach request -- eNB-UE S1AP Id: %d, MME-UE S1AP Id: %d\n", ue_ctx.enb_ue_s1ap_id, ue_ctx.mme_ue_s1ap_id); + m_s1ap_log->console("Attach Request -- UE Network Capabilities EEA: %d%d%d%d%d%d%d%d\n", + attach_req.ue_network_cap.eea[0], + attach_req.ue_network_cap.eea[1], + attach_req.ue_network_cap.eea[2], + attach_req.ue_network_cap.eea[3], + attach_req.ue_network_cap.eea[4], + attach_req.ue_network_cap.eea[5], + attach_req.ue_network_cap.eea[6], + attach_req.ue_network_cap.eea[7]); + m_s1ap_log->console("Attach Request -- UE Network Capabilities EIA: %d%d%d%d%d%d%d%d\n", + attach_req.ue_network_cap.eia[0], + attach_req.ue_network_cap.eia[1], + attach_req.ue_network_cap.eia[2], + attach_req.ue_network_cap.eia[3], + attach_req.ue_network_cap.eia[4], + attach_req.ue_network_cap.eia[5], + attach_req.ue_network_cap.eia[6], + attach_req.ue_network_cap.eia[7]); + m_s1ap_log->console("Attach Request -- MS Network Capabilities Present: %s\n", attach_req.ms_network_cap_present ? "true" : "false"); + m_s1ap_log->console("PDN Connectivity Request -- EPS Bearer Identity requested: %d\n", pdn_con_req.eps_bearer_id); + m_s1ap_log->console("PDN Connectivity Request -- Procedure Transaction Id: %d\n", pdn_con_req.proc_transaction_id); + m_s1ap_log->console("PDN Connectivity Request -- ESM Information Transfer requested: %s\n", pdn_con_req.esm_info_transfer_flag_present ? "true" : "false"); + + m_s1ap_log->console("Could not find M-TMSI=0x%x. Sending ID request\n",m_tmsi); + m_s1ap_log->info("Could not find M-TMSI=0x%d. Sending Id Request\n", m_tmsi); + ue_ctx.mme_ue_s1ap_id = m_s1ap->get_next_mme_ue_s1ap_id(); + m_s1ap->add_new_ue_ctx(ue_ctx); + pack_identity_request(reply_buffer, ue_ctx.enb_ue_s1ap_id, ue_ctx.mme_ue_s1ap_id); + *reply_flag = true; + return true; + } + else{ + m_s1ap_log->console("Attach Request -- Found M-TMSI: %d\n",m_tmsi); + ue_ctx_t *ue_ctx_ptr = m_s1ap->find_ue_ctx(it->second); + if(ue_ctx_ptr!=NULL) + { + m_s1ap_log->console("Found UE context. IMSI: %015lu\n",ue_ctx_ptr->imsi); + m_mme_gtpc->send_create_session_request(ue_ctx_ptr->imsi, ue_ctx_ptr->mme_ue_s1ap_id); + *reply_flag = false; //No reply needed + return true; + } + else + { + m_s1ap_log->error("Found M-TMSI but could not find UE context\n"); + return false; + } + } + return true; +} + bool s1ap_nas_transport::handle_nas_authentication_response(srslte::byte_buffer_t *nas_msg, ue_ctx_t *ue_ctx, srslte::byte_buffer_t *reply_buffer, bool* reply_flag) { @@ -491,8 +719,8 @@ s1ap_nas_transport::handle_identity_response(srslte::byte_buffer_t *nas_msg, ue_ for(int i=0;i<=14;i++){ imsi += id_resp.mobile_id.imsi[i]*std::pow(10,14-i); } - - m_s1ap_log->info("Id Response IMSI: %015lu\n", imsi); + m_s1ap_log->info("Id Response -- IMSI: %015lu\n", imsi); + m_s1ap_log->info("Id Response -- IMSI: %015lu\n", imsi); ue_ctx->imsi = imsi; //Get Authentication Vectors from HSS