Example #1
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()
    def test_attach_act_dflt_ber_ctxt_rej(self):
        """Test Attach test case for sending Activate Default
        EPS Bearer Reject along with Attach Complete message"""
        # 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

        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
        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 = 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,
        )
        bid = attach_acc.esmInfo.epsBearerId

        # Trigger Attach Complete with
        # Activate Default EPS Bearer Context Reject
        time.sleep(0.2)
        act_rej = s1ap_types.ueActvDfltEpsBearerCtxtRej_t()
        act_rej.ue_Id = req.ue_id
        act_rej.bearerId = bid
        act_rej.esmCause = s1ap_types.TFW_EMM_CAUSE_REQ_REJ_UNSPECIFIED

        # Activate Default EPS Bearer Context Reject sent along with
        # Attach Complete message
        # Attach Complete + Activate Default EPS Bearer Context Reject
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_ACTV_DEFAULT_EPS_BEARER_CNTXT_REJECT,
            act_rej,
        )
        # Attach Reject
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_ATTACH_REJECT_IND.value,
        )

        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type,
            s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )
        print("******** released UE contexts ********")
    def test_attach_ics_drop_with_mme_restart(self):
        """Stateless Initial Context Setup Drop Test Case:
        1. Step-by-step UE attach procedure
        2. Set flag in S1APTester to drop ICS request message
        3. Restart MME after getting ICS request dropped indication
        4. Reset flag to not drop next ICS request messages
        5. Handle UE context release after no ICS response to MME
        6. Re-attach UE to verify if UE context was cleared properly
        after handling the NW Initiated detach once MME comes up
        """

        self._s1ap_wrapper.configUEDevice(1)
        req = self._s1ap_wrapper.ue_req

        # 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 = 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("******************** Sending 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,
        )
        print("******************** Received Authentiction Request Indication")

        # Send 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
        print("******************** Sending Authentiction Response")
        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 Indication")

        print(
            "******************** Setting flag to drop Initial Context Setup "
            "Request",
        )
        drop_init_ctxt_setup_req = s1ap_types.UeDropInitCtxtSetup()
        drop_init_ctxt_setup_req.ue_Id = req.ue_id
        drop_init_ctxt_setup_req.flag = 1
        # Timer to release UE context at s1ap tester
        drop_init_ctxt_setup_req.tmrVal = 2000
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SET_DROP_ICS, drop_init_ctxt_setup_req,
        )

        print("******************** Sending Security Mode Complete")
        # Send 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,
        )

        # enbApp sends UE_ICS_DROPD_IND message to tfwApp after dropping
        # ICS request
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(
            response.msg_type, s1ap_types.tfwCmd.UE_ICS_DROPD_IND.value,
        )
        print(
            "******************** Received Initial Context Setup Dropped "
            "Indication",
        )

        print("******************** Restarting MME service on gateway")
        self._s1ap_wrapper.magmad_util.restart_services(["mme"])

        print(
            "******************** Resetting flag to not drop next Initial "
            "Context Setup Request messages",
        )
        drop_init_ctxt_setup_req = s1ap_types.UeDropInitCtxtSetup()
        drop_init_ctxt_setup_req.ue_Id = req.ue_id
        drop_init_ctxt_setup_req.flag = 0
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SET_DROP_ICS, drop_init_ctxt_setup_req,
        )

        for j in range(30):
            print("Waiting for", j, "seconds")
            time.sleep(1)

        # It has been observed that despite getting the restart command on
        # time, MME sometimes restarts after a delay of 5-6 seconds. If MME
        # restarts before ICS timer expiry of 4 seconds, it will send NW
        # initiated detach request with type re-attach required after coming
        # up, else MME will do implicit detach and send UE context release
        # command to release the S1AP context
        print(
            "******************** Waiting for NW initiated Detach Request or "
            "UE Context Release Indication",
        )
        resp_count = 0
        while True:
            resp_count += 1
            # Receive NW initiated detach request
            response = self._s1ap_wrapper.s1_util.get_response()
            if (
                response.msg_type
                == s1ap_types.tfwCmd.UE_NW_INIT_DETACH_REQUEST.value
            ):
                nw_init_detach_req = response.cast(
                    s1ap_types.ueNwInitdetachReq_t,
                )
                print(
                    "******************** Received NW initiated Detach Request"
                    " with detach type ",
                    nw_init_detach_req.Type,
                )
                self.assertEqual(
                    nw_init_detach_req.Type,
                    s1ap_types.ueNwInitDetType_t.TFW_RE_ATTACH_REQUIRED.value,
                )

                if resp_count == 1:
                    # Send detach accept
                    detach_accept = s1ap_types.ueTrigDetachAcceptInd_t()
                    detach_accept.ue_Id = req.ue_id
                    print(
                        "******************** Sending UE triggered Detach "
                        "Accept message",
                    )
                    self._s1ap_wrapper._s1_util.issue_cmd(
                        s1ap_types.tfwCmd.UE_TRIGGERED_DETACH_ACCEPT,
                        detach_accept,
                    )
                else:
                    print(
                        "******************** Ignoring re-transmitted (",
                        resp_count,
                        ") NW initiated detach request message",
                    )
            else:
                break

        self.assertEqual(
            response.msg_type, s1ap_types.tfwCmd.UE_CTX_REL_IND.value,
        )
        print("******************** Received UE Context Release indication")

        print(
            "******************** Running End to End attach to verify if "
            "UE context was released properly after handling ICS drop 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 for EMM Information from MME
        self._s1ap_wrapper._s1_util.receive_emm_info()

        print(
            "******************** Running UE detach (switch-off) for ",
            "UE id ",
            req.ue_id,
        )
        # Now detach the UE
        self._s1ap_wrapper.s1_util.detach(
            req.ue_id,
            s1ap_types.ueDetachType_t.UE_SWITCHOFF_DETACH.value,
            True,
        )
    def test_attach_ics_failure_with_mme_restart(self):
        """Stateless Initial Context Setup Failure Test Case:
        1. Step-by-step UE attach procedure
        2. Set flag in S1APTester to send ICS failure to MME
        3. Restart MME after sending ICS failure to MME
        4. Reset flag to not send ICS failure for next ICS request
        5. Handle UE context release after ICS failure
        6. Re-attach UE to verify if UE context was cleared properly
        after handling the ICS failure message
        """

        self._s1ap_wrapper.configUEDevice(1)
        req = self._s1ap_wrapper.ue_req

        # 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 = 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("******************** Sending 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)
        print("******************** Received Authentiction Request Indication")

        # Send 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

        print("******************** Sending Authentiction Response")
        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 Indication")

        print(
            "******************** Setting flag to send Initial Context Setup "
            "Failure")
        init_ctxt_setup_fail = s1ap_types.ueInitCtxtSetupFail()
        init_ctxt_setup_fail.ue_Id = req.ue_id
        init_ctxt_setup_fail.flag = 1
        init_ctxt_setup_fail.causeType = 0
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SET_INIT_CTXT_SETUP_FAIL,
            init_ctxt_setup_fail)

        print("******************** Sending Security Mode Complete")
        # Send 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)
        print("******************** Received Initial Context Setup Indication")

        print("******************** Restarting MME service on gateway")
        self._s1ap_wrapper.magmad_util.restart_services(["mme"])

        init_ctxt_setup_fail = s1ap_types.ueInitCtxtSetupFail()
        init_ctxt_setup_fail.ue_Id = req.ue_id
        init_ctxt_setup_fail.flag = 0
        init_ctxt_setup_fail.causeType = 0
        print(
            "******************** Resetting flag to not send Initial Context "
            "Setup Failure")
        self._s1ap_wrapper._s1_util.issue_cmd(
            s1ap_types.tfwCmd.UE_SET_INIT_CTXT_SETUP_FAIL,
            init_ctxt_setup_fail)

        for j in range(30):
            print("Waiting for", j, "seconds")
            time.sleep(1)

        # Waiting for UE Context Release indication
        response = self._s1ap_wrapper.s1_util.get_response()
        self.assertEqual(response.msg_type,
                         s1ap_types.tfwCmd.UE_CTX_REL_IND.value)
        print("******************** Received UE Context Release indication")

        print(
            "******************** Running End to End attach to verify if "
            "UE context was released properly after handling ICS failure 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 for 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
        self._s1ap_wrapper.s1_util.detach(
            req.ue_id,
            s1ap_types.ueDetachType_t.UE_NORMAL_DETACH.value,
            True,
        )