blob: 575a8d7523925ec40020d7e10ed0f7baa9fbb1cc [file] [log] [blame]
anjana_sreekumar@infosys.com991c2062020-01-08 11:42:57 +05301/*
2 * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
3 * Copyright (c) 2017 Intel Corporation
4 * Copyright (c) 2019, Infosys Ltd.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <arpa/inet.h>
22#include <unistd.h>
23#include <string.h>
24#include <pthread.h>
25#include <errno.h>
26
27#include "log.h"
28#include "err_codes.h"
29#include "message_queues.h"
30#include "ipc_api.h"
31#include "msgType.h"
32//#include "stage5_s11_info.h"
33#include "gtpv2c.h"
34#include "gtpv2c_ie.h"
35#include "s11_config.h"
36#include "../../gtpV2Codec/gtpV2StackWrappers.h"
37
38/************************************************************************
39Current file : Stage 5 handler.
40ATTACH stages :
41 Stage 1 : IAM-->[stage1 handler]-->AIR, ULR
42 Stage 2 : AIA, ULA -->[stage2 handler]--> Auth req
43 Stage 3 : Auth resp-->[stage3 handler]-->Sec mode cmd
44 Stage 4 : sec mode resp-->[stage4 handler]-->esm infor req
45--> Stage 5 : esm infor resp-->[stage5 handler]-->create session
46 Stage 6 : create session resp-->[stage6 handler]-->init ctx setup
47 Stage 7 : attach complete-->[stage7 handler]-->modify bearer
48**************************************************************************/
49
50/****Globals and externs ***/
51
52/*S11 CP communication parameters*/
53extern int g_s11_fd;
54extern struct sockaddr_in g_s11_cp_addr;
55extern socklen_t g_s11_serv_size;
56
57extern s11_config g_s11_cfg;
58volatile uint32_t g_s11_sequence = 1;
59
60/****Global and externs end***/
61static char buf[S11_CSREQ_STAGE5_BUF_SIZE];
62
63struct CS_Q_msg *g_csReqInfo;
64
65extern struct GtpV2Stack* gtpStack_gp;
66struct MsgBuffer* csReqMsgBuf_p = NULL;
67
68void
69bswap8_array(uint8_t *src, uint8_t *dest, uint32_t len)
70{
71 for (uint32_t i=0; i<len; i++)
72 dest[i] = ((src[i] & 0x0F)<<4 | (src[i] & 0xF0)>>4);
73
74 return;
75}
76uint32_t
77convert_imsi_to_digits_array(uint8_t *src, uint8_t *dest, uint32_t len)
78{
79 uint8_t msb_digit = 0;
80 uint8_t lsb_digit = 0;
81 uint8_t num_of_digits = 0;
82
83 for(uint32_t i = 0; i < len; i++)
84 {
85 lsb_digit = ((src[i] & 0xF0) >> 4);
86 dest[(2*i) + 1] = lsb_digit;
87
88 msb_digit = (src[i] & 0x0F);
89 dest[2*i] = msb_digit;
90
91 if (lsb_digit != 0x0F)
92 num_of_digits = num_of_digits + 2;
93 else
94 num_of_digits++;
95 }
96
97 return num_of_digits;
98}
99
100/**
101* Stage specific message processing.
102*/
103static int
104create_session_processing(struct CS_Q_msg * g_csReqInfo)
105{
106 GtpV2MessageHeader gtpHeader;
107 gtpHeader.msgType = GTP_CREATE_SESSION_REQ;
108 gtpHeader.sequenceNumber = g_s11_sequence;
109 gtpHeader.teidPresent = true;
110 gtpHeader.teid = g_csReqInfo->ue_idx;
111
112 g_s11_sequence++;
113
114 log_msg(LOG_INFO,"In create session handler->ue_idx:%d\n",g_csReqInfo->ue_idx);
115
116 CreateSessionRequestMsgData msgData;
117 memset(&msgData, 0, sizeof(msgData));
118
119 msgData.imsiIePresent = true;
120 memset(msgData.imsi.imsiValue.digits, 0x0f, 16);
121
122
123 uint8_t imsi_len =
124 convert_imsi_to_digits_array(g_csReqInfo->IMSI,
125 msgData.imsi.imsiValue.digits,
126 BINARY_IMSI_LEN);
127
128 msgData.imsi.imsiValue.length = imsi_len;
129 log_msg(LOG_INFO, "IMSI Len: %d\n", imsi_len);
130
131 msgData.msisdnIePresent = true;
132 msgData.msisdn.msisdnValue.length = 10;
133 for (uint8_t i = 1; i <= 5; i++)
134 {
135 msgData.msisdn.msisdnValue.digits[2*(i-1)] = (g_csReqInfo->MSISDN[i-1] & 0x0F);
136 msgData.msisdn.msisdnValue.digits[(2*i) - 1] = ((g_csReqInfo->MSISDN[i-1] & 0xF0) >> 4);
137 }
138
139 struct TAI *tai = &(g_csReqInfo->tai);
140 struct CGI *cgi = &(g_csReqInfo->utran_cgi);
141
142 msgData.userLocationInformationIePresent = true;
143 msgData.userLocationInformation.taipresent = true;
144 msgData.userLocationInformation.ecgipresent = true;
145
146 msgData.userLocationInformation.tai.trackingAreaCode = ntohs(tai->tac);
147 msgData.userLocationInformation.tai.mccDigit1 = tai->plmn_id.idx[0] & 0x0F;
148 msgData.userLocationInformation.tai.mccDigit2 = (tai->plmn_id.idx[0] & 0xF0) >> 4;
149 msgData.userLocationInformation.tai.mccDigit3 = tai->plmn_id.idx[1] & 0x0F;
150 msgData.userLocationInformation.tai.mncDigit1 = tai->plmn_id.idx[2] & 0x0F;
151 msgData.userLocationInformation.tai.mncDigit2 = (tai->plmn_id.idx[2] & 0xF0) >> 4;
152 msgData.userLocationInformation.tai.mncDigit3 = (tai->plmn_id.idx[1] & 0xF0) >> 4;
153
154 msgData.userLocationInformation.ecgi.eUtranCellId = ntohl(cgi->cell_id);
155 msgData.userLocationInformation.ecgi.mccDigit1 = cgi->plmn_id.idx[0] & 0x0F;
156 msgData.userLocationInformation.ecgi.mccDigit2 = (cgi->plmn_id.idx[0] & 0xF0) >> 4;
157 msgData.userLocationInformation.ecgi.mccDigit3 = cgi->plmn_id.idx[1] & 0x0F;
158 msgData.userLocationInformation.ecgi.mncDigit1 = cgi->plmn_id.idx[2] & 0x0F;
159 msgData.userLocationInformation.ecgi.mncDigit2 = (cgi->plmn_id.idx[2] & 0xF0) >> 4;
160 msgData.userLocationInformation.ecgi.mncDigit3 = (cgi->plmn_id.idx[1] & 0xF0) >> 4;
161
162 msgData.servingNetworkIePresent = true;
163 msgData.servingNetwork.mccDigit1 = tai->plmn_id.idx[0] & 0x0F;
164 msgData.servingNetwork.mccDigit2 = (tai->plmn_id.idx[0] & 0xF0) >> 4;
165 msgData.servingNetwork.mccDigit3 = tai->plmn_id.idx[1] & 0x0F;
166 msgData.servingNetwork.mncDigit1 = tai->plmn_id.idx[2] & 0x0F;
167 msgData.servingNetwork.mncDigit2 = (tai->plmn_id.idx[2] & 0xF0) >> 4;
168 msgData.servingNetwork.mncDigit3 = (tai->plmn_id.idx[1] & 0xF0) >> 4;
169
170 msgData.ratType.ratType = 6;
171
172 msgData.indicationFlagsIePresent = true;
173 msgData.indicationFlags.iDFI = true;
174 msgData.indicationFlags.iMSV = true;
175
176 msgData.senderFTeidForControlPlane.ipv4present = true;
177 msgData.senderFTeidForControlPlane.interfaceType = 10;
178 msgData.senderFTeidForControlPlane.ipV4Address.ipValue = g_s11_cfg.local_egtp_ip;
179 msgData.senderFTeidForControlPlane.teidGreKey = g_csReqInfo->ue_idx;
180
181 msgData.pgwS5S8AddressForControlPlaneOrPmipIePresent = true;
182 msgData.pgwS5S8AddressForControlPlaneOrPmip.ipv4present = true;
183 msgData.pgwS5S8AddressForControlPlaneOrPmip.interfaceType = 7;
184 msgData.pgwS5S8AddressForControlPlaneOrPmip.ipV4Address.ipValue = g_s11_cfg.pgw_ip;
185
186 msgData.accessPointName.apnValue.count = g_csReqInfo->apn.len;
187 memcpy(msgData.accessPointName.apnValue.values, g_csReqInfo->apn.val, g_csReqInfo->apn.len);
188
189 msgData.selectionModeIePresent = true;
190 msgData.selectionMode.selectionMode = 1;
191
192 msgData.pdnTypeIePresent = true;
193 msgData.pdnType.pdnType = 1;
194
195 msgData.pdnAddressAllocationIePresent = true;
196 msgData.pdnAddressAllocation.pdnType = 1;
197 msgData.pdnAddressAllocation.ipV4Address.ipValue = 0;
198
199 msgData.maximumApnRestrictionIePresent = true;
200 msgData.maximumApnRestriction.restrictionValue = 0;
201
202 /* Bearer Context */
203 msgData.bearerContextsToBeCreatedCount = 1;
204 msgData.bearerContextsToBeCreated[0].epsBearerId.epsBearerId = 5;
205
206 msgData.bearerContextsToBeCreated[0].bearerLevelQos.pci = 1;
207 msgData.bearerContextsToBeCreated[0].bearerLevelQos.pl = 11;
208 msgData.bearerContextsToBeCreated[0].bearerLevelQos.pvi = 0;
209 msgData.bearerContextsToBeCreated[0].bearerLevelQos.qci = 9;
210
211 uint32_t mbr_uplink = htonl(MBR_UPLINK);
212 uint32_t mbr_downlink = htonl(MBR_DOWNLINK);
213
214 msgData.bearerContextsToBeCreated[0].bearerLevelQos.maxBitRateUl.count = 5;
215 msgData.bearerContextsToBeCreated[0].bearerLevelQos.maxBitRateDl.count = 5;
216 memcpy(&msgData.bearerContextsToBeCreated[0].bearerLevelQos.maxBitRateUl.values, &mbr_uplink, sizeof(mbr_uplink));
217 memcpy(&msgData.bearerContextsToBeCreated[0].bearerLevelQos.maxBitRateDl.values, &mbr_downlink, sizeof(mbr_downlink));
218 msgData.bearerContextsToBeCreated[0].bearerLevelQos.guraranteedBitRateUl.count = 5;
219 msgData.bearerContextsToBeCreated[0].bearerLevelQos.guaranteedBitRateDl.count = 5;
220
221 msgData.aggregateMaximumBitRateIePresent = true;
222 msgData.aggregateMaximumBitRate.maxMbrUplink = g_csReqInfo->max_requested_bw_ul;
223 msgData.aggregateMaximumBitRate.maxMbrDownlink = g_csReqInfo->max_requested_bw_dl;
224
225 bool rc = GtpV2Stack_buildGtpV2Message(gtpStack_gp, csReqMsgBuf_p, &gtpHeader, &msgData);
226 if (rc == false)
227 {
228 log_msg(LOG_ERROR, "Failed to encode create session request\n");
229 return E_FAIL;
230 }
231
232 log_msg(LOG_INFO, "send %d bytes.\n",MsgBuffer_getBufLen(csReqMsgBuf_p));
233
234 int res = sendto (
235 g_s11_fd,
236 MsgBuffer_getDataPointer(csReqMsgBuf_p),
237 MsgBuffer_getBufLen(csReqMsgBuf_p), 0,
238 (struct sockaddr*)&g_s11_cp_addr,
239 g_s11_serv_size);
240 if (res < 0) {
241 log_msg(LOG_ERROR,"Error in sendto in detach stage 3 post to next\n");
242 }
243
244 log_msg(LOG_INFO,"%d bytes sent. Err : %d, %s\n",res,errno,
245 strerror(errno));
246
247 MsgBuffer_reset(csReqMsgBuf_p);
248
249 return SUCCESS;
250}
251
252/**
253* Thread function for stage.
254*/
255void*
256create_session_handler(void *data)
257{
258 log_msg(LOG_INFO, "Create Session Request handler\n");
259
260 create_session_processing((struct CS_Q_msg *) data);
261
262 return NULL;
263}