def handle_msg(self, msg):
         if msg.msg_type == s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value:
             self.auth_req_ind_count += 1
             m = msg.cast(s1ap_types.ueAuthReqInd_t)
             print("====> Received UE_AUTH_REQ_IND ue-id", m.ue_Id)
             auth_res = s1ap_types.ueAuthResp_t()
             auth_res.ue_Id = m.ue_Id
             sqn_recvd = s1ap_types.ueSqnRcvd_t()
             sqn_recvd.pres = 0
             auth_res.sqnRcvd = sqn_recvd
             self._s1ap_wrapper._s1_util.issue_cmd(
                 s1ap_types.tfwCmd.UE_AUTH_RESP, auth_res
             )
         elif msg.msg_type == s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value:
             self.sec_mod_cmd_ind_count += 1
             m = msg.cast(s1ap_types.ueSecModeCmdInd_t)
             print("============>  Received UE_SEC_MOD_CMD_IND ue-id", m.ue_Id)
             sec_mode_complete = s1ap_types.ueSecModeComplete_t()
             sec_mode_complete.ue_Id = m.ue_Id
             self._s1ap_wrapper._s1_util.issue_cmd(
                 s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE, sec_mode_complete
             )
         elif msg.msg_type == s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value:
             self.attach_accept_ind_count += 1
             m = msg.cast(s1ap_types.ueAttachAccept_t)
             print("====================> Received UE_ATTACH_ACCEPT_IND ue-id", m.ue_Id)
             pdn_type = m.esmInfo.pAddr.pdnType
             addr = m.esmInfo.pAddr.addrInfo
             if self._s1ap_wrapper._s1_util.CM_ESM_PDN_IPV4 == pdn_type:
                 # Cast and cache the IPv4 address
                 ip = ipaddress.ip_address(bytes(addr[:4]))
                 with self._s1ap_wrapper._s1_util._lock:
                     self._s1ap_wrapper._s1_util._ue_ip_map[m.ue_Id] = ip
             attach_complete = s1ap_types.ueAttachComplete_t()
             attach_complete.ue_Id = m.ue_Id
             self._s1ap_wrapper._s1_util.issue_cmd(
                 s1ap_types.tfwCmd.UE_ATTACH_COMPLETE, attach_complete
             )
         elif msg.msg_type == s1ap_types.tfwCmd.UE_IDENTITY_REQ_IND.value:
             self.identity_req_ind_count += 1
             m = msg.cast(s1ap_types.ueIdentityReqInd_t)
             print("=> Received UE_IDENTITY_REQ_IND ue-id", m.ue_Id)
             us_identity_resp = s1ap_types.ueIdentityResp_t()
             us_identity_resp.ue_Id = m.ue_Id
             us_identity_resp.idType = m.idType
             self._s1ap_wrapper._s1_util.issue_cmd(
                 s1ap_types.tfwCmd.UE_IDENTITY_RESP, us_identity_resp
             )
         elif msg.msg_type == s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value:
             self.int_ctx_setup_ind_count += 1
         elif msg.msg_type == s1ap_types.tfwCmd.UE_EMM_INFORMATION.value:
             self.ue_emm_information_count += 1
         elif msg.msg_type == s1ap_types.tfwCmd.UE_CTX_REL_IND.value:
             self.ue_ctx_rel_ind_count += 1
         else:
             print("Unhandled msg type", msg.msg_type)
Esempio n. 2
0
    def test_attach_with_multiple_mme_restarts(self):
        num_ues = 1
        self._s1ap_wrapper.configUEDevice(num_ues)
        print("************************* Sending Attach Request for ue-id : 1")
        attach_req = s1ap_types.ueAttachRequest_t()
        attach_req.ue_Id = 1
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt

        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        print("************************* Received UE_AUTH_REQ_IND")

        # Try consecutive mme restarts
        self._s1ap_wrapper.magmad_util.restart_mme_and_wait()
        self._s1ap_wrapper.magmad_util.restart_mme_and_wait()

        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = 1
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        sqn_recvd.pres = 0
        auth_res.sqnRcvd = sqn_recvd
        print("************************* Sending Auth Response ue-id",
              auth_res.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )
        print("************************* Received UE_SEC_MOD_CMD_IND")

        self._s1ap_wrapper.magmad_util.restart_mme_and_wait()

        print("************************* Sending UE_SEC_MOD_COMPLETE")
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value,
        )
        print("************************* Received INT_CTX_SETUP_IND")

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value,
        )
        print("************************* Received UE_ATTACH_ACCEPT_IND")

        self._s1ap_wrapper.magmad_util.restart_mme_and_wait()

        # Trigger Attach Complete
        print("************************* Sending UE_ATTACH_COMPLETE")
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )

        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()
        print("************************* Running UE detach")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = 1
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST,
            detach_req,
        )

        # Wait for UE context release command
        return self._s1ap_wrapper.s1_util.get_response()
    def test_esm_information(self):
        """ Testing of sending Esm Information procedure """
        num_ues = 1

        self._s1ap_wrapper.configUEDevice(num_ues)
        print("************************* sending Attach Request for ue-id : 1")
        attach_req = s1ap_types.ueAttachRequest_t()
        attach_req.ue_Id = 1
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt

        # enabling ESM Information transfer flag
        attach_req.eti.pres = 1
        attach_req.eti.esm_info_transfer_flag = 1

        print("Sending Attach Request ue-id", attach_req.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST, attach_req)

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value)
        print("Received auth req ind ")

        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = 1
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        sqn_recvd.pres = 0
        auth_res.sqnRcvd = sqn_recvd
        print("Sending Auth Response ue-id", auth_res.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(s1ap_types.tfwCmd.UE_AUTH_RESP,
                                              auth_res)

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value)
        print("Received Security Mode Command ue-id", auth_res.ue_Id)

        time.sleep(1)

        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE, sec_mode_complete)

        # Esm Information Request indication
        print("Received Esm Information Request ue-id",
              sec_mode_complete.ue_Id)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_ESM_INFORMATION_REQ.value)
        esm_info_req = response.cast(s1ap_types.ueEsmInformationReq_t)

        # Sending Esm Information Response
        print("Sending Esm Information Response ue-id",
              sec_mode_complete.ue_Id)
        esm_info_response = s1ap_types.ueEsmInformationRsp_t()
        esm_info_response.ue_Id = 1
        esm_info_response.tId = esm_info_req.tId
        esm_info_response.pdnAPN_pr.pres = 1
        s = "magma.ipv4"
        esm_info_response.pdnAPN_pr.len = len(s)
        esm_info_response.pdnAPN_pr.pdn_apn = (ctypes.c_ubyte * 100)(
            *[ctypes.c_ubyte(ord(c)) for c in s[:100]])
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ESM_INFORMATION_RSP, esm_info_response)

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value)

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE, attach_complete)
        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()

        print("*** Running UE detach ***")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = 1
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST, detach_req)
        # Wait for UE context release command
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_CTX_REL_IND.value)
Esempio n. 4
0
    def test_attach_outoforder_attach_cmp_icsrsp(self):
        """
        Attach, Out of order attach complete and initial context setup response
        """
        """
        Simulating this scenario by delaying ICS response message to change the
        order of attach complete and ics resp
        """
        # Ground work.
        self._s1ap_wrapper.configUEDevice(1)
        req = self._s1ap_wrapper.ue_req

        # Trigger Attach Request
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        pdn_type = s1ap_types.pdn_Type()
        pdn_type.pres = True
        pdn_type.pdn_type = 3
        attach_req.ue_Id = req.ue_id
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        attach_req.pdnType_pr = pdn_type

        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )

        delay_init_ctxt_setup_resp = s1ap_types.UeDelayInitCtxtSetupRsp()
        delay_init_ctxt_setup_resp.ue_Id = req.ue_id
        delay_init_ctxt_setup_resp.flag = 1
        delay_init_ctxt_setup_resp.tmrVal = 2000
        print("*** Setting Initial Context Setup Resp Delay ***")
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SET_DELAY_ICS_RSP,
            delay_init_ctxt_setup_resp,
        )

        # Trigger Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = req.ue_id
        sqnRecvd = s1ap_types.ueSqnRcvd_t()
        sqnRecvd.pres = 0
        auth_res.sqnRcvd = sqnRecvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )

        # Trigger Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value,
        )

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )

        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()
        print(
            "************************* Running UE detach for UE id ",
            req.ue_id,
        )
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = req.ue_id
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST,
            detach_req,
        )
        # Wait for UE context release command
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )

        time.sleep(1)
Esempio n. 5
0
    def test_attach_complete_after_ics_timer_expiry(self):
        """Simulating sending of attach complete msg after ICS timer expiry.

        MME should discard attach complete message as it is already in the
        process of implicit detach

        ### Do not add this TC to sanity suite ###
        """
        # Ground work.
        self._s1ap_wrapper.configUEDevice(1)
        req = self._s1ap_wrapper.ue_req

        # Trigger Attach Request
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        pdn_type = s1ap_types.pdn_Type()
        pdn_type.pres = True
        pdn_type.pdn_type = 1
        attach_req.ue_Id = req.ue_id
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        attach_req.pdnType_pr = pdn_type
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST, attach_req,
        )
        print("****** Sent ATTACH_REQUEST")
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type, s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        print("****** Received AUTH_REQ_IND")
        delay_init_ctxt_setup_resp = s1ap_types.UeDelayInitCtxtSetupRsp()
        delay_init_ctxt_setup_resp.ue_Id = req.ue_id
        delay_init_ctxt_setup_resp.flag = 1
        delay_init_ctxt_setup_resp.tmrVal = 4500
        print("*** Setting Initial Context Setup Resp Delay ***")
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SET_DELAY_ICS_RSP, delay_init_ctxt_setup_resp,
        )

        # Trigger Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = req.ue_id
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        sqn_recvd.pres = 0
        auth_res.sqnRcvd = sqn_recvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP, auth_res,
        )
        print("****** Sent UE_AUTH_RESP")
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type, s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )
        print("****** Received UE_SEC_MOD_CMD_IND")

        # Trigger Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE, sec_mode_complete,
        )
        print("****** Sent UE_SEC_MOD_COMPLETE")

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type, s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value,
        )
        print("****** Received INT_CTX_SETUP_IND")

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type, s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value,
        )
        print("****** Received UE_ATTACH_ACCEPT_IND")

        print("****** Sleeping for 4 seconds. Waiting for ICS timer to expire")
        time.sleep(4)
        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE, attach_complete,
        )
        print("****** Sent ATTACH_COMPLETE")

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type, s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )
        print("****** Received UE_CTX_REL_IND")
Esempio n. 6
0
    def test_attach_ipv4v6_pdn_type(self):
        """ Test Attach for the UEs that are dual IP stack IPv4v6
            capable """
        # Ground work.
        self._s1ap_wrapper.configUEDevice(1)
        req = self._s1ap_wrapper.ue_req

        # Trigger Attach Request with PDN_Type = IPv4v6
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        pdn_type = s1ap_types.pdn_Type()
        pdn_type.pres = True
        # Set PDN TYPE to IPv4V6 i.e. 3. IPV4 is equal to 1
        # IPV6 is equal to 2 in value
        pdn_type.pdn_type = 3
        attach_req.ue_Id = req.ue_id
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        attach_req.pdnType_pr = pdn_type

        print("********Triggering Attach Request with PND Type IPv4v6 test")

        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST, attach_req)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value)

        # Trigger Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = req.ue_id
        sqnRecvd = s1ap_types.ueSqnRcvd_t()
        sqnRecvd.pres = 0
        auth_res.sqnRcvd = sqnRecvd
        self._s1ap_wrapper._s1_util.issue_cmd(s1ap_types.tfwCmd.UE_AUTH_RESP,
                                              auth_res)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value)

        # Trigger Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE, sec_mode_complete)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value)

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE, attach_complete)
        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()

        print("************************* Running UE detach")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = req.ue_id
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST, detach_req)
        # Wait for UE context release command
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_CTX_REL_IND.value)
    def test_attach_enb_rlf(self):
        """
        Attach, after attach procedure is completed, eNB detects the RLF
        i.e., releasing the context by sending UE context release request
        with cause Radio Link Failure by that time we sending one more
        attach request
        """
        # Ground work.
        self._s1ap_wrapper.configUEDevice(1)

        # Trigger Attach Request
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.ue_Id = 1
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt

        print("***Triggering Attach Request ***")

        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )

        # Trigger Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = 1
        sqnRecvd = s1ap_types.ueSqnRcvd_t()
        sqnRecvd.pres = 0
        auth_res.sqnRcvd = sqnRecvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )

        # Trigger Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value,
        )

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )

        print("***Triggering Re-Attach Request ***")

        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.ue_Id = 1
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )

        # Send UE context release request with cause Radio Link Failure
        ue_ctxt_rel_req = s1ap_types.ueCntxtRelReq_t()
        ue_ctxt_rel_req.ue_Id = 1
        ue_ctxt_rel_req.cause.causeVal = (
            gpp_types.CauseRadioNetwork.RADIO_CONNECTION_WITH_UE_LOST.value)
        self._s1ap_wrapper.s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_CNTXT_REL_REQUEST,
            ue_ctxt_rel_req,
        )
        time.sleep(1)
Esempio n. 8
0
    def test_guti_attach_with_zero_mtmsi(self):
        """1. Perform IMSI attach
        2. Move UE to idle mode
        3. Send GUTI attach request for the same UE with M-TMSI value 0
        4. Detach the UE
        """
        num_ues = 1
        self._s1ap_wrapper.configUEDevice(num_ues)
        req = self._s1ap_wrapper.ue_req
        print(
            "************************* Running End to End attach for UE id:",
            req.ue_id,
        )
        # Now actually complete the attach
        self._s1ap_wrapper._s1_util.attach(
            req.ue_id,
            s1ap_types.tfwCmd.UE_END_TO_END_ATTACH_REQUEST,
            s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND,
            s1ap_types.ueAttachAccept_t,
        )

        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()

        time.sleep(0.5)

        # Send UE context release request to move UE to idle mode
        print(
            "************************* Sending UE context release request ",
            "for UE id ",
            req.ue_id,
        )
        ureq = s1ap_types.ueCntxtRelReq_t()
        ureq.ue_Id = req.ue_id
        ureq.cause.causeVal = gpp_types.CauseRadioNetwork.USER_INACTIVITY.value
        self._s1ap_wrapper.s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_CNTXT_REL_REQUEST,
            ureq,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )

        time.sleep(5)

        # Send GUTI attach request with M-TMSI value as 0
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_GUTI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        pdn_type = s1ap_types.pdn_Type()
        pdn_type.pres = True
        pdn_type.pdn_type = 1
        mcc = "001"
        mnc = "01"
        mcc_len = len(mcc)
        mnc_len = len(mnc)
        attach_req.ue_Id = req.ue_id
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        attach_req.pdnType_pr = pdn_type
        attach_req.guti_mi.pres = True
        for i in range(0, mcc_len):
            attach_req.guti_mi.guti.mcc[i] = ctypes.c_ubyte(int(mcc[i]))
        for i in range(0, mnc_len):
            attach_req.guti_mi.guti.mnc[i] = ctypes.c_ubyte(int(mnc[i]))
        attach_req.guti_mi.guti.mmeGrdId = 314
        attach_req.guti_mi.guti.mmeCode = 30
        attach_req.guti_mi.guti.mTmsi = 0

        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )
        print(
            "********************** Sent attach req for UE id ",
            attach_req.ue_Id,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_IDENTITY_REQ_IND.value,
        )
        id_req = response.cast(s1ap_types.ueIdentityReqInd_t)
        print(
            "********************** Received identity req for UE id",
            id_req.ue_Id,
        )

        identity_resp = s1ap_types.ueIdentityResp_t()
        identity_resp.ue_Id = id_req.ue_Id
        identity_resp.idType = s1ap_types.TFW_MID_TYPE_IMSI
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_IDENTITY_RESP,
            identity_resp,
        )
        print(
            "********************** Sent identity rsp for UE id",
            id_req.ue_Id,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        auth_req = response.cast(s1ap_types.ueAuthReqInd_t)
        print(
            "********************** Received auth req for UE id",
            auth_req.ue_Id,
        )
        # Send Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = auth_req.ue_Id
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        sqn_recvd.pres = 0
        auth_res.sqnRcvd = sqn_recvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )
        print("********************** Sent auth rsp for UE id", auth_req.ue_Id)

        response = self._s1ap_wrapper.s1_util.get_response()
        sec_mode_cmd = response.cast(s1ap_types.ueSecModeCmdInd_t)

        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )
        print(
            "********************** Received security mode cmd for UE id",
            sec_mode_cmd.ue_Id,
        )
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = sec_mode_cmd.ue_Id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )
        print(
            "********************** Sent security mode complete for UE id",
            sec_mode_cmd.ue_Id,
        )

        # Receive initial context setup and attach accept indication
        response = (self._s1ap_wrapper._s1_util.
                    receive_initial_ctxt_setup_and_attach_accept())
        attach_acc = response.cast(s1ap_types.ueAttachAccept_t)
        print(
            "********************** Received attach accept for UE Id:",
            attach_acc.ue_Id,
        )

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = attach_acc.ue_Id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )
        print(
            "********************** Sent attach complete for UE id",
            attach_complete.ue_Id,
        )

        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()
        print(
            "************************* Running UE detach for UE id ",
            attach_complete.ue_Id,
        )
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = attach_complete.ue_Id
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST,
            detach_req,
        )
        # Wait for UE context release command
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )
    def test_attach_detach_duplicate_nas_resp_messages(self):
        """ Duplicate NAS Response Messages Test Case"""
        # Ground work.
        self._s1ap_wrapper.configUEDevice(1)
        req = self._s1ap_wrapper.ue_req
        maxNasMsgRetransmission = 4

        # Trigger Attach Request
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.ue_Id = req.ue_id
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt

        print("*** Triggering Attach Request ***")
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST, attach_req)

        # Waiting for Authentication Request
        # Wait for last Timer T3460 Expiry
        for i in range(maxNasMsgRetransmission):
            print(
                "*** Waiting for Authentication Request Message (",
                str(i + 1),
                ") ***",
            )
            response = self._s1ap_wrapper.s1_util.get_response()
            self.assertEqual(response.msg_type,
                             s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value)
            print(
                "*** Authentication Request Message Received (",
                str(i + 1),
                ") ***",
            )

        # Trigger Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = req.ue_id
        sqnRecvd = s1ap_types.ueSqnRcvd_t()
        sqnRecvd.pres = 0
        auth_res.sqnRcvd = sqnRecvd
        for i in range(maxNasMsgRetransmission):
            print(
                "*** Sending Authentication Response Message (",
                str(i + 1),
                ") ***",
            )
            self._s1ap_wrapper._s1_util.issue_cmd(
                s1ap_types.tfwCmd.UE_AUTH_RESP, auth_res)

        # Waiting for Security mode command
        # Wait for last Timer T3460 Expiry
        for i in range(maxNasMsgRetransmission):
            print(
                "*** Waiting for Security Mode Command Message (",
                str(i + 1),
                ") ***",
            )
            response = self._s1ap_wrapper.s1_util.get_response()
            self.assertEqual(response.msg_type,
                             s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value)
            print(
                "*** Security Mode Command Message Received (",
                str(i + 1),
                ") ***",
            )

        # Trigger Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = req.ue_id
        for i in range(maxNasMsgRetransmission):
            print(
                "*** Sending Security Mode Complete Message (",
                str(i + 1),
                ") ***",
            )
            self._s1ap_wrapper._s1_util.issue_cmd(
                s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE, sec_mode_complete)

        # Waiting for Attach accept
        # Wait for last Timer T3450 Expiry
        for i in range(maxNasMsgRetransmission):
            print(
                "*** Waiting for Attach Accept Message (",
                str(i + 1),
                ") ***",
            )
            # Attach accept will be sent in ICSR only for the first time
            # Re-transmitted Attach Accept will be sent in DL NAS transport
            if i < 1:
                response = self._s1ap_wrapper.s1_util.get_response()
                self.assertEqual(
                    response.msg_type,
                    s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value,
                )

            response = self._s1ap_wrapper.s1_util.get_response()
            self.assertEqual(response.msg_type,
                             s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value)
            print(
                "*** Attach Accept Message Received (",
                str(i + 1),
                ") ***",
            )

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = req.ue_id
        for i in range(maxNasMsgRetransmission):
            print(
                "*** Sending Attach Complete Message (",
                str(i + 1),
                ") ***",
            )
            self._s1ap_wrapper._s1_util.issue_cmd(
                s1ap_types.tfwCmd.UE_ATTACH_COMPLETE, attach_complete)

        print("*** Running UE detach ***")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = req.ue_id
        detach_req.ueDetType = s1ap_types.ueDetachType_t.UE_NORMAL_DETACH.value
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST, detach_req)
    def test_attach_esm_information_with_apn_correction(self):
        """ Testing of sending wrong APN in Esm Information procedure and
        using APN correction feature to override it """
        num_ues = 1

        print('************************* restarting mme')
        self._s1ap_wrapper.magmad_util.restart_services(['mme'])
        for j in range(15):
            print("Waiting mme restart for", j, "seconds")
            time.sleep(1)

        self._s1ap_wrapper.configUEDevice_ues_same_imsi(num_ues)
        print("************************* sending Attach Request for ue-id : 1")
        attach_req = s1ap_types.ueAttachRequest_t()
        attach_req.ue_Id = 1
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt

        # enabling ESM Information transfer flag
        attach_req.eti.pres = 1
        attach_req.eti.esm_info_transfer_flag = 1

        print("Sending Attach Request ue-id", attach_req.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        print("Received auth req ind ")

        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = 1
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        sqn_recvd.pres = 0
        auth_res.sqnRcvd = sqn_recvd
        print("Sending Auth Response ue-id", auth_res.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )
        print("Received Security Mode Command ue-id", auth_res.ue_Id)

        time.sleep(1)

        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )

        # Esm Information Request indication
        print(
            "Received Esm Information Request ue-id",
            sec_mode_complete.ue_Id,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ESM_INFORMATION_REQ.value,
        )
        esm_info_req = response.cast(s1ap_types.ueEsmInformationReq_t)
        # Sending Esm Information Response
        print(
            "Sending Esm Information Response ue-id",
            sec_mode_complete.ue_Id,
        )
        esm_info_response = s1ap_types.ueEsmInformationRsp_t()
        esm_info_response.ue_Id = 1
        esm_info_response.tId = esm_info_req.tId
        esm_info_response.pdnAPN_pr.pres = 1
        s = "oai"
        esm_info_response.pdnAPN_pr.len = len(s)
        esm_info_response.pdnAPN_pr.pdn_apn = (ctypes.c_ubyte * 100)(
            *[ctypes.c_ubyte(ord(c)) for c in s[:100]], )
        print("Sending Esm Information with wrong APN ", s)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ESM_INFORMATION_RSP,
            esm_info_response,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value,
        )

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )
        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()

        print("*** Running UE detach ***")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = 1
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST,
            detach_req,
        )
        # Wait for UE context release command
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )

        # Disable APN Correction
        self._s1ap_wrapper.magmad_util.config_apn_correction(
            MagmadUtil.apn_correction_cmds.DISABLE, )
        self._s1ap_wrapper.magmad_util.restart_services(['mme'])
        for j in range(10):
            print("Waiting mme restart for", j, "seconds")
            time.sleep(1)
Esempio n. 11
0
    def test_different_enb_s1ap_id_same_ue(self):
        """ Testing of sending different enb S1ap-ID for same UE """
        num_ues = 1

        self._s1ap_wrapper.configUEDevice_ues_same_imsi(num_ues)
        print("************************* sending Attach Request for ue-id : 1")
        attach_req = s1ap_types.ueAttachRequest_t()
        attach_req.ue_Id = 1
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        print("Sending Attach Request ue-id", attach_req.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST, attach_req)

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertTrue(response, s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value)
        print("Received auth req ind ")

        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = 1
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        sqn_recvd.pres = 0
        auth_res.sqnRcvd = sqn_recvd
        print("Sending Auth Response ue-id", auth_res.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(s1ap_types.tfwCmd.UE_AUTH_RESP,
                                              auth_res)

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertTrue(response, s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value)
        print("Received Security Mode Command ue-id", auth_res.ue_Id)

        attach_req = s1ap_types.ueAttachRequest_t()
        attach_req.ue_Id = 2
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        print("Sending Attach Request ue-id", attach_req.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST, attach_req)

        time.sleep(1)

        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE, sec_mode_complete)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertTrue(response, s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertTrue(response, s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value)

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE, attach_complete)
        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()

        print("************************* Running UE detach for UE id:1 ")

        print("************************* Running UE detach")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = 1
        detach_req.ueDetType = s1ap_types.ueDetachType_t.\
            UE_SWITCHOFF_DETACH.value
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST, detach_req)
        # Wait for UE context release command
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertTrue(response, s1ap_types.tfwCmd.UE_CTX_REL_IND.value)
Esempio n. 12
0
    def test_attach_delay_ue_ctxt_rel_cmp(self):
        """ Attach, Delay Ue context release complete """
        # Ground work.
        self._s1ap_wrapper.configUEDevice(1)
        req = self._s1ap_wrapper.ue_req

        # Trigger Attach Request
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.ue_Id = req.ue_id
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt

        print("********Triggering Attach Request ")

        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST, attach_req)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value)

        # Trigger Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = req.ue_id
        sqnRecvd = s1ap_types.ueSqnRcvd_t()
        sqnRecvd.pres = 0
        auth_res.sqnRcvd = sqnRecvd
        self._s1ap_wrapper._s1_util.issue_cmd(s1ap_types.tfwCmd.UE_AUTH_RESP,
                                              auth_res)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value)

        # Trigger Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE, sec_mode_complete)

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value)

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value)

        delay_ue_ctxt_rel_cmp = s1ap_types.UeDelayUeCtxtRelCmp()
        delay_ue_ctxt_rel_cmp.ue_Id = req.ue_id
        delay_ue_ctxt_rel_cmp.flag = 1
        delay_ue_ctxt_rel_cmp.tmrVal = 1000

        print("*** Setting Delay for Ue context release complete ***")
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SET_DELAY_UE_CTXT_REL_CMP,
            delay_ue_ctxt_rel_cmp,
        )

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE, attach_complete)
        time.sleep(0.5)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_EMM_INFORMATION.value)

        time.sleep(0.5)
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = req.ue_id
        detach_req.ueDetType = s1ap_types.ueDetachType_t.UE_NORMAL_DETACH.value
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST, detach_req)
        response = self._s1ap_wrapper._s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_DETACH_ACCEPT_IND.value)

        print(
            "*** Sending UE context release request ",
            "for UE id ***",
            req.ue_id,
        )

        # Send UE context release request to move UE to idle mode
        # ue_ctxt_rel_req = s1ap_types.ueCntxtRelReq_t()
        uectxtrel_req = s1ap_types.ueCntxtRelReq_t()
        uectxtrel_req.ue_Id = req.ue_id
        uectxtrel_req.cause.causeVal = (
            gpp_types.CauseRadioNetwork.RELEASE_DUE_TO_EUTRAN_GENERATED_REASON.
            value)

        self._s1ap_wrapper.s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_CNTXT_REL_REQUEST, uectxtrel_req)
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_CTX_REL_IND.value)

        time.sleep(10)
Esempio n. 13
0
    def test_imei_restriction_no_imeisv_in_smc(self):
        """
        This TC does the following:
        UE 1:
        1. Send security mode complete message without imeisv
        2. Receive identity request with id type imeisv
        3. Send identity rsp with blocked imeisv
        4. Verify attach reject is received with cause(5)
           IMEI_NOT_ACCEPTED
        UE 2:
        1. Send security mode complete message without imeisv
        2. Receive identity request with id type imeisv
        3. Send identity rsp with an allowed imeisv
        4. Detach the UE

        If this TC is executed individually run
        test_modify_mme_config_for_sanity.py to add
        { IMEI="9900048235103723" }
        under the BLOCKED_IMEI_LIST in mme.conf.template.

        After execution of this TC run test_restore_mme_config_after_sanity.py
        to restore the old mme.conf.template.
        """
        num_ues = 2
        ue_ids = []
        self._s1ap_wrapper.configUEDevice(num_ues)
        for _ in range(num_ues):
            req = self._s1ap_wrapper.ue_req
            ue_ids.append(req.ue_id)
        # Send Attach Request
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        pdn_type = s1ap_types.pdn_Type()
        pdn_type.pres = True
        pdn_type.pdn_type = 1
        attach_req.ue_Id = ue_ids[0]
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        attach_req.pdnType_pr = pdn_type

        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )
        print(
            "********************** Sent attach req for UE id ",
            ue_ids[0],
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        auth_req = response.cast(s1ap_types.ueAuthReqInd_t)
        print(
            "********************** Received auth req for UE id",
            auth_req.ue_Id,
        )

        # Send Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = ue_ids[0]
        sqnRecvd = s1ap_types.ueSqnRcvd_t()
        sqnRecvd.pres = 0
        auth_res.sqnRcvd = sqnRecvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )
        print("********************** Sent auth rsp for UE id", ue_ids[0])

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )
        sec_mod_cmd = response.cast(s1ap_types.ueSecModeCmdInd_t)
        print(
            "********************** Received security mode cmd for UE id",
            sec_mod_cmd.ue_Id,
        )

        # Send Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = ue_ids[0]
        # Do not include imeisv
        sec_mode_complete.noImeisv = True
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )
        print(
            "********************** Sent security mode complete for UE id",
            ue_ids[0],
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_IDENTITY_REQ_IND.value,
        )
        id_req = response.cast(s1ap_types.ueIdentityReqInd_t)
        print(
            "********************** Received identity req for UE id",
            id_req.ue_Id,
        )

        # Mobile Identity types
        # IMSI=1, IMEI=2, IMEISV=3, TMSI=4, TMGI=5, GUTI=6
        identity_resp = s1ap_types.ueIdentityResp_t()
        identity_resp.ue_Id = ue_ids[0]
        identity_resp.idType = 3
        identity_resp.idValPres = True
        imeisv = "9900048235103723"
        # Check if the len of imeisv exceeds 16
        self.assertLessEqual(len(imeisv), 16)
        for i in range(0, len(imeisv)):
            identity_resp.idVal[i] = ctypes.c_ubyte(int(imeisv[i]))
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_IDENTITY_RESP,
            identity_resp,
        )
        print("********************** Sent identity rsp for UE id", ue_ids[0])

        # Receive Attach Reject
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ATTACH_REJECT_IND.value,
        )
        attach_rej = response.cast(s1ap_types.ueAttachRejInd_t)
        print(
            "********************** Received attach reject for UE id %d"
            " with emm cause %d" % (ue_ids[0], attach_rej.cause), )

        # Verify cause
        self.assertEqual(
            attach_rej.cause,
            s1ap_types.TFW_EMM_CAUSE_IMEI_NOT_ACCEPTED,
        )

        # UE Context release
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )
        ue_context_rel = response.cast(s1ap_types.ueCntxtRelReq_t)
        print(
            "********************** Received UE_CTX_REL_IND for UE id ",
            ue_context_rel.ue_Id,
        )

        # Attach the 2nd UE
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        pdn_type = s1ap_types.pdn_Type()
        pdn_type.pres = True
        pdn_type.pdn_type = 1
        attach_req.ue_Id = ue_ids[1]
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        attach_req.pdnType_pr = pdn_type

        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )
        print(
            "********************** Sent attach req for UE id ",
            ue_ids[1],
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        auth_req = response.cast(s1ap_types.ueAuthReqInd_t)
        print(
            "********************** Received auth req for UE id",
            auth_req.ue_Id,
        )
        # Send Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = ue_ids[1]
        sqnRecvd = s1ap_types.ueSqnRcvd_t()
        sqnRecvd.pres = 0
        auth_res.sqnRcvd = sqnRecvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )
        print("********************** Sent auth rsp for UE id", ue_ids[1])

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )
        sec_mod_cmd = response.cast(s1ap_types.ueSecModeCmdInd_t)
        print(
            "********************** Received security mode cmd for UE id",
            sec_mod_cmd.ue_Id,
        )

        # Send Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = ue_ids[1]
        # Do not include imeisv
        sec_mode_complete.noImeisv = True
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )
        print(
            "********************** Sent security mode complete for UE id",
            ue_ids[1],
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_IDENTITY_REQ_IND.value,
        )
        id_req = response.cast(s1ap_types.ueIdentityReqInd_t)
        print(
            "********************** Received identity req for UE id",
            id_req.ue_Id,
        )
        # Send Identity Request
        id_req = response.cast(s1ap_types.ueIdentityReqInd_t)
        identity_resp = s1ap_types.ueIdentityResp_t()
        identity_resp.ue_Id = id_req.ue_Id
        identity_resp.idType = id_req.idType
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_IDENTITY_RESP,
            identity_resp,
        )
        print("********************** Sent identity rsp for UE id", ue_ids[1])

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value,
        )
        attach_acc = response.cast(s1ap_types.ueAttachAccept_t)
        print(
            "********************** Received attach accept for UE id",
            attach_acc.ue_Id,
        )
        # Send Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = ue_ids[1]
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )
        print(
            "********************** Sent attach complete for UE id",
            ue_ids[1],
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_EMM_INFORMATION.value,
        )

        print("********************** Sleeping for 0.5 seconds ")
        time.sleep(0.5)
        # Now detach the UE
        print("********************** Running UE detach for UE id ", ue_ids[1])
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = ue_ids[1]
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST,
            detach_req,
        )
        # Wait for UE context release command
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )
        ue_context_rel = response.cast(s1ap_types.ueCntxtRelReq_t)
        print(
            "********************** Received UE_CTX_REL_IND for UE id ",
            ue_context_rel.ue_Id,
        )
Esempio n. 14
0
    def _create_attach_ipv4v6_pdn_type_req(self, pdn_type_value):
        # Ground work.
        self._s1ap_wrapper.configUEDevice(1)
        ue_req = self._s1ap_wrapper.ue_req
        # Trigger Attach Request with PDN_Type = IPv4v6
        attach_req = s1ap_types.ueAttachRequest_t()
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        pdn_type = s1ap_types.pdn_Type()
        pdn_type.pres = True
        # Set PDN TYPE to IPv4V6 i.e. 3. IPV4 is equal to 1
        # IPV6 is equal to 2 in value
        pdn_type.pdn_type = pdn_type_value
        attach_req.ue_Id = ue_req.ue_id
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt
        attach_req.pdnType_pr = pdn_type

        print(
            "********Triggering Attach Request with PDN Type IPv4v6 test, "
            "pdn_type_value",
            pdn_type_value,
        )
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )

        # Trigger Authentication Response
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = ue_req.ue_id
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        sqn_recvd.pres = 0
        auth_res.sqnRcvd = sqn_recvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )

        # Trigger Security Mode Complete
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = ue_req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )
        # Attach Reject will be sent since IPv6 PDN Type is not configured
        if pdn_type_value == 2:
            response = self._s1ap_wrapper.s1_util.get_response()
            self.assertEqual(
                response.msg_type,
                s1ap_types.tfwCmd.UE_ATTACH_REJECT_IND.value,
            )
            return self._s1ap_wrapper.s1_util.get_response()

        # Receive initial context setup and attach accept indication
        response = (self._s1ap_wrapper._s1_util.
                    receive_initial_ctxt_setup_and_attach_accept())
        attach_acc = response.cast(s1ap_types.ueAttachAccept_t)
        print(
            "********************** Received attach accept for UE Id:",
            attach_acc.ue_Id,
        )

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = ue_req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )

        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()
        print("************************* Running UE detach")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = ue_req.ue_id
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST,
            detach_req,
        )

        # Wait for UE context release command
        return self._s1ap_wrapper.s1_util.get_response()
Esempio n. 15
0
    def test_resync(self):
        """Attach logic where we bump up the seq number stored in UE SQN
        array in order to trigger authentication failure from UE to
        verify the re-sync of seq number logic in EPC"""

        # Ground work.
        self._s1ap_wrapper.configUEDevice(1)
        req = self._s1ap_wrapper.ue_req

        print("************************* Running sync failure test")

        attach_req = s1ap_types.ueAttachRequest_t()
        attach_req.ue_Id = req.ue_id
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt

        # Trigger Attach
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = req.ue_id
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        max_sqn_recvd = s1ap_types.ueSqnRcvd_t()
        # Use SQN received from EPC as is
        sqn_recvd.pres = False
        # Set UE side max SQN value to bigger than value received from EPC
        # This is to trigger auth failure with cause = Sync Failure
        max_sqn_recvd.pres = True
        max_sqn_recvd.sqn = (0, 0, 0, 0, 0, 96)
        auth_res.sqnRcvd = sqn_recvd
        auth_res.maxSqnRcvd = max_sqn_recvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )
        response = self._s1ap_wrapper.s1_util.get_response()

        # Verify that EPC re-sends Authentication Request
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        print("************************* Retrying authentication")
        # Use SQN received from EPC as is
        sqn_recvd.pres = False
        auth_res.sqnRcvd = sqn_recvd
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )

        # Trigger Security mode complete message
        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )

        # Receive initial context setup and attach accept indication
        response = (self._s1ap_wrapper._s1_util.
                    receive_initial_ctxt_setup_and_attach_accept())
        attach_acc = response.cast(s1ap_types.ueAttachAccept_t)
        print(
            "********************** Received attach accept for UE Id:",
            attach_acc.ue_Id,
        )

        # Trigger Attach complete message
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = req.ue_id
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )
        print("************************* UE Attach Complete")

        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()

        print("************************* Running UE detach")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = req.ue_id
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST,
            detach_req,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )
    def test_attach_esm_info_timerexpiration_max_retries(self):
        """Testing of sending Esm Information procedure"""
        num_ues = 1
        # The maximum no. of allowed transmissions of ESM information request
        # as per 3gpp spec 24.301 is 3. Since MME does not send the PDN
        # connectivity reject message as of now after maximum number of
        # T3489 expires, test script does not receive any failure response and
        # has no provision to handle it as well. Therefore, test case gets
        # stuck waiting for response, if max_retries value is greater than 3
        max_retries = 3

        self._s1ap_wrapper.configUEDevice(num_ues)
        print("************************* sending Attach Request for ue-id : 1")
        attach_req = s1ap_types.ueAttachRequest_t()
        attach_req.ue_Id = 1
        sec_ctxt = s1ap_types.TFW_CREATE_NEW_SECURITY_CONTEXT
        id_type = s1ap_types.TFW_MID_TYPE_IMSI
        eps_type = s1ap_types.TFW_EPS_ATTACH_TYPE_EPS_ATTACH
        attach_req.mIdType = id_type
        attach_req.epsAttachType = eps_type
        attach_req.useOldSecCtxt = sec_ctxt

        # enabling ESM Information transfer flag
        attach_req.eti.pres = 1
        attach_req.eti.esm_info_transfer_flag = 1

        print("Sending Attach Request ue-id", attach_req.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_REQUEST,
            attach_req,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_AUTH_REQ_IND.value,
        )
        print("Received auth req ind ")

        auth_res = s1ap_types.ueAuthResp_t()
        auth_res.ue_Id = 1
        sqn_recvd = s1ap_types.ueSqnRcvd_t()
        sqn_recvd.pres = 0
        auth_res.sqnRcvd = sqn_recvd
        print("Sending Auth Response ue-id", auth_res.ue_Id)
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_AUTH_RESP,
            auth_res,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_SEC_MOD_CMD_IND.value,
        )
        print("Received Security Mode Command ue-id", auth_res.ue_Id)

        time.sleep(1)

        sec_mode_complete = s1ap_types.ueSecModeComplete_t()
        sec_mode_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SEC_MOD_COMPLETE,
            sec_mode_complete,
        )

        for i in range(max_retries):
            # Esm Information Request indication
            response = self._s1ap_wrapper.s1_util.get_response()
            self.assertEqual(
                response.msg_type,
                s1ap_types.tfwCmd.UE_ESM_INFORMATION_REQ.value,
            )
            print(
                "Received Esm Information Request (",
                i + 1,
                ") ue-id",
                sec_mode_complete.ue_Id,
            )
            esm_info_req = response.cast(s1ap_types.ueEsmInformationReq_t)

        # Sleep for 5 seconds to trigger last max retry count timer expiry
        time.sleep(5)

        # Sending Esm Information Response
        print(
            "Sending Esm Information Response ue-id",
            sec_mode_complete.ue_Id,
        )
        esm_info_response = s1ap_types.ueEsmInformationRsp_t()
        esm_info_response.ue_Id = 1
        esm_info_response.tId = esm_info_req.tId
        esm_info_response.pdnAPN_pr.pres = 1
        s = "magma.ipv4"
        esm_info_response.pdnAPN_pr.len = len(s)
        esm_info_response.pdnAPN_pr.pdn_apn = (ctypes.c_ubyte * 100)(
            *[ctypes.c_ubyte(ord(c)) for c in s[:100]],
        )
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ESM_INFORMATION_RSP,
            esm_info_response,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.INT_CTX_SETUP_IND.value,
        )
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ATTACH_ACCEPT_IND.value,
        )

        # Trigger Attach Complete
        attach_complete = s1ap_types.ueAttachComplete_t()
        attach_complete.ue_Id = 1
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ATTACH_COMPLETE,
            attach_complete,
        )
        # Wait on EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()

        print("*** Running UE detach ***")
        # Now detach the UE
        detach_req = s1ap_types.uedetachReq_t()
        detach_req.ue_Id = 1
        detach_req.ueDetType = (
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value
        )
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_DETACH_REQUEST,
            detach_req,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )