blob: 9fc6c686162232693edad9afdd5d3bef6837ae76 [file] [log] [blame]
anjana_sreekumar@infosys.com991c2062020-01-08 11:42:57 +05301/*
2 * Copyright (c) 2019, Infosys Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <utils/mmeCommonUtils.h>
18#include <controlBlock.h>
19#include <contextManager/dataBlocks.h>
20#include <contextManager/subsDataGroupManager.h>
21#include <log.h>
22#include <mme_app.h>
23#include <msgBuffer.h>
24#include <s1ap_structs.h>
25#include <utils/defaultMmeProcedureCtxt.h>
26
27using namespace mme;
28
29extern mme_config g_mme_cfg;
30
31bool MmeCommonUtils::isLocalGuti(const guti &guti_r)
32{
33 bool rc = false;
34
35 if (guti_r.mme_grp_id == g_mme_cfg.mme_group_id &&
36 guti_r.mme_code == g_mme_cfg.mme_code)
37 {
38 rc = true;
39 }
40
41 return rc;
42}
43
44uint32_t MmeCommonUtils::allocateMtmsi()
45{
46 uint32_t tmsi = 0;
47
48 while(1)
49 {
50 tmsi = rand() % 10000;
51
52 if (SubsDataGroupManager::Instance()->findCBWithmTmsi(tmsi) == -1)
53 break;
54 }
55
56 log_msg(LOG_INFO, "MTMSI allocated is %u\n", tmsi);
57
58 return tmsi;
59}
60
61AttachType MmeCommonUtils::getAttachType(UEContext* ueContext_p,
62 const struct ue_attach_info& ue_info)
63{
64 log_msg(LOG_INFO, "deriveAttachType\n");
65
66 AttachType attachType = maxAttachType_c;
67
68 if(UE_ID_IMSI(ue_info.flags))
69 {
70 log_msg(LOG_INFO, "IMSI attach received.\n");
71
72 attachType = imsiAttach_c;
73 }
74 else if (UE_ID_GUTI(ue_info.flags))
75 {
76 log_msg(LOG_INFO, "GUTI attach received. mTMSI is %u \n",
77 ue_info.mi_guti.m_TMSI);
78
79 attachType = unknownGutiAttach_c;
80
81 if (isLocalGuti(ue_info.mi_guti))
82 {
83 // The guti is allocated by this MME, check if a context exists.
84 // If the context does not exist, treat as unknown GUTI attach?
85 log_msg(LOG_INFO, "GUTI is local..");
86
87 if (ueContext_p != NULL)
88 {
89 if (ueContext_p->getMtmsi() == ue_info.mi_guti.m_TMSI)
90 {
91 log_msg(LOG_INFO, "and known\n");
92
93 attachType = knownGutiAttach_c;
94 }
95 else
96 {
97 log_msg(LOG_INFO, "mTMSI mismatches with UE context. "
98 "Treat as unknown GUTI attach\n");
99 }
100 }
101 else
102 {
103 log_msg(LOG_INFO, "UE context is null. Unknown GUTI attach triggered\n");
104 }
105
106 }
107 else
108 {
109 log_msg(LOG_INFO, "GUTI is not local..");
110 }
111 }
112 return attachType;
113}
114
115SM::ControlBlock* MmeCommonUtils::findControlBlock(cmn::utils::MsgBuffer* buf)
116{
117 SM::ControlBlock *cb = NULL;
118
119 const s1_incoming_msg_data_t* msgData_p = (s1_incoming_msg_data_t*)(buf->getDataPointer());
120 if(msgData_p == NULL)
121 {
122 log_msg(LOG_INFO, "MsgData is NULL .\n");
123 return cb;
124 }
125
126 switch (msgData_p->msg_type)
127 {
128 case attach_request:
129 {
130 const struct ue_attach_info &ue_info = (msgData_p->msg_data.ue_attach_info_m);
131 if(UE_ID_IMSI(ue_info.flags))
132 {
133 log_msg(LOG_INFO, "IMSI attach received.\n");
134
135 cb = SubsDataGroupManager::Instance()->allocateCB();
136 cb->setTempDataBlock(DefaultMmeProcedureCtxt::Instance());
137 }
138 else if (UE_ID_GUTI(ue_info.flags))
139 {
140 log_msg(LOG_INFO, "GUTI attach received.\n");
141
142 if (isLocalGuti(ue_info.mi_guti))
143 {
144 log_msg(LOG_INFO, "GUTI is local.\n");
145
146 int cbIndex = SubsDataGroupManager::Instance()->findCBWithmTmsi(ue_info.mi_guti.m_TMSI);
147 if (cbIndex > 0)
148 {
149 cb = SubsDataGroupManager::Instance()->findControlBlock(cbIndex);
150 }
151 else
152 {
153 log_msg(LOG_ERROR, "Failed to find control block with mTmsi.\n");
154
155 // allocate new cb and proceed?
156 cb = SubsDataGroupManager::Instance()->allocateCB();
157 cb->setTempDataBlock(DefaultMmeProcedureCtxt::Instance());
158 }
159 }
160 else
161 {
162 cb = SubsDataGroupManager::Instance()->allocateCB();
163 cb->setTempDataBlock(DefaultMmeProcedureCtxt::Instance());
164 }
165 }
166 break;
167 }
168 case service_request:
169 {
170 const struct service_req_Q_msg &service_req = (msgData_p->msg_data.service_req_Q_msg_m);
171 int cbIndex = SubsDataGroupManager::Instance()->findCBWithmTmsi(service_req.s_tmsi.m_TMSI);
172 if (cbIndex > 0)
173 {
174 cb = SubsDataGroupManager::Instance()->findControlBlock(cbIndex);
175 }
176 else
177 {
178 log_msg(LOG_INFO, "Failed to find control block with mTmsi.\n");
179 }
180
181 break;
182 }
183 case detach_request:
184 {
185 const struct detach_req_Q_msg &detach_Req = (msgData_p->msg_data.detachReq_Q_msg_m);
186 int cbIndex = SubsDataGroupManager::Instance()->findCBWithmTmsi(detach_Req.ue_m_tmsi);
187 if (cbIndex > 0)
188 {
189 cb = SubsDataGroupManager::Instance()->findControlBlock(cbIndex);
190 }
191 else
192 {
193 log_msg(LOG_INFO, "Failed to find control block with mTmsi. %d\n", detach_Req.ue_m_tmsi);
194 }
195 break;
196 }
197 case tau_request:
198 {
199 cb = SubsDataGroupManager::Instance()->findControlBlock(msgData_p->ue_idx);
200
201 if (cb == NULL)
202 log_msg(LOG_INFO, "Failed to retrieve CB using idx %d.\n", msgData_p->ue_idx);
203
204 break;
205 }
206 default:
207 {
208 log_msg(LOG_INFO, "Unhandled message is NULL .\n");
209 }
210 }
211
212 return cb;
213}
214
215
216
217