MME2 changes - Propped commits from openmme/paging branch. Added scripts
for code gen
Change-Id: Ie55032217232214ac8544ca76ea34335205329e4
diff --git a/src/s11/handlers/create_session_handler.c b/src/s11/handlers/create_session_handler.c
new file mode 100644
index 0000000..575a8d7
--- /dev/null
+++ b/src/s11/handlers/create_session_handler.c
@@ -0,0 +1,263 @@
+/*
+ * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
+ * Copyright (c) 2017 Intel Corporation
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+#include <errno.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "msgType.h"
+//#include "stage5_s11_info.h"
+#include "gtpv2c.h"
+#include "gtpv2c_ie.h"
+#include "s11_config.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+
+/************************************************************************
+Current file : Stage 5 handler.
+ATTACH stages :
+ Stage 1 : IAM-->[stage1 handler]-->AIR, ULR
+ Stage 2 : AIA, ULA -->[stage2 handler]--> Auth req
+ Stage 3 : Auth resp-->[stage3 handler]-->Sec mode cmd
+ Stage 4 : sec mode resp-->[stage4 handler]-->esm infor req
+--> Stage 5 : esm infor resp-->[stage5 handler]-->create session
+ Stage 6 : create session resp-->[stage6 handler]-->init ctx setup
+ Stage 7 : attach complete-->[stage7 handler]-->modify bearer
+**************************************************************************/
+
+/****Globals and externs ***/
+
+/*S11 CP communication parameters*/
+extern int g_s11_fd;
+extern struct sockaddr_in g_s11_cp_addr;
+extern socklen_t g_s11_serv_size;
+
+extern s11_config g_s11_cfg;
+volatile uint32_t g_s11_sequence = 1;
+
+/****Global and externs end***/
+static char buf[S11_CSREQ_STAGE5_BUF_SIZE];
+
+struct CS_Q_msg *g_csReqInfo;
+
+extern struct GtpV2Stack* gtpStack_gp;
+struct MsgBuffer* csReqMsgBuf_p = NULL;
+
+void
+bswap8_array(uint8_t *src, uint8_t *dest, uint32_t len)
+{
+ for (uint32_t i=0; i<len; i++)
+ dest[i] = ((src[i] & 0x0F)<<4 | (src[i] & 0xF0)>>4);
+
+ return;
+}
+uint32_t
+convert_imsi_to_digits_array(uint8_t *src, uint8_t *dest, uint32_t len)
+{
+ uint8_t msb_digit = 0;
+ uint8_t lsb_digit = 0;
+ uint8_t num_of_digits = 0;
+
+ for(uint32_t i = 0; i < len; i++)
+ {
+ lsb_digit = ((src[i] & 0xF0) >> 4);
+ dest[(2*i) + 1] = lsb_digit;
+
+ msb_digit = (src[i] & 0x0F);
+ dest[2*i] = msb_digit;
+
+ if (lsb_digit != 0x0F)
+ num_of_digits = num_of_digits + 2;
+ else
+ num_of_digits++;
+ }
+
+ return num_of_digits;
+}
+
+/**
+* Stage specific message processing.
+*/
+static int
+create_session_processing(struct CS_Q_msg * g_csReqInfo)
+{
+ GtpV2MessageHeader gtpHeader;
+ gtpHeader.msgType = GTP_CREATE_SESSION_REQ;
+ gtpHeader.sequenceNumber = g_s11_sequence;
+ gtpHeader.teidPresent = true;
+ gtpHeader.teid = g_csReqInfo->ue_idx;
+
+ g_s11_sequence++;
+
+ log_msg(LOG_INFO,"In create session handler->ue_idx:%d\n",g_csReqInfo->ue_idx);
+
+ CreateSessionRequestMsgData msgData;
+ memset(&msgData, 0, sizeof(msgData));
+
+ msgData.imsiIePresent = true;
+ memset(msgData.imsi.imsiValue.digits, 0x0f, 16);
+
+
+ uint8_t imsi_len =
+ convert_imsi_to_digits_array(g_csReqInfo->IMSI,
+ msgData.imsi.imsiValue.digits,
+ BINARY_IMSI_LEN);
+
+ msgData.imsi.imsiValue.length = imsi_len;
+ log_msg(LOG_INFO, "IMSI Len: %d\n", imsi_len);
+
+ msgData.msisdnIePresent = true;
+ msgData.msisdn.msisdnValue.length = 10;
+ for (uint8_t i = 1; i <= 5; i++)
+ {
+ msgData.msisdn.msisdnValue.digits[2*(i-1)] = (g_csReqInfo->MSISDN[i-1] & 0x0F);
+ msgData.msisdn.msisdnValue.digits[(2*i) - 1] = ((g_csReqInfo->MSISDN[i-1] & 0xF0) >> 4);
+ }
+
+ struct TAI *tai = &(g_csReqInfo->tai);
+ struct CGI *cgi = &(g_csReqInfo->utran_cgi);
+
+ msgData.userLocationInformationIePresent = true;
+ msgData.userLocationInformation.taipresent = true;
+ msgData.userLocationInformation.ecgipresent = true;
+
+ msgData.userLocationInformation.tai.trackingAreaCode = ntohs(tai->tac);
+ msgData.userLocationInformation.tai.mccDigit1 = tai->plmn_id.idx[0] & 0x0F;
+ msgData.userLocationInformation.tai.mccDigit2 = (tai->plmn_id.idx[0] & 0xF0) >> 4;
+ msgData.userLocationInformation.tai.mccDigit3 = tai->plmn_id.idx[1] & 0x0F;
+ msgData.userLocationInformation.tai.mncDigit1 = tai->plmn_id.idx[2] & 0x0F;
+ msgData.userLocationInformation.tai.mncDigit2 = (tai->plmn_id.idx[2] & 0xF0) >> 4;
+ msgData.userLocationInformation.tai.mncDigit3 = (tai->plmn_id.idx[1] & 0xF0) >> 4;
+
+ msgData.userLocationInformation.ecgi.eUtranCellId = ntohl(cgi->cell_id);
+ msgData.userLocationInformation.ecgi.mccDigit1 = cgi->plmn_id.idx[0] & 0x0F;
+ msgData.userLocationInformation.ecgi.mccDigit2 = (cgi->plmn_id.idx[0] & 0xF0) >> 4;
+ msgData.userLocationInformation.ecgi.mccDigit3 = cgi->plmn_id.idx[1] & 0x0F;
+ msgData.userLocationInformation.ecgi.mncDigit1 = cgi->plmn_id.idx[2] & 0x0F;
+ msgData.userLocationInformation.ecgi.mncDigit2 = (cgi->plmn_id.idx[2] & 0xF0) >> 4;
+ msgData.userLocationInformation.ecgi.mncDigit3 = (cgi->plmn_id.idx[1] & 0xF0) >> 4;
+
+ msgData.servingNetworkIePresent = true;
+ msgData.servingNetwork.mccDigit1 = tai->plmn_id.idx[0] & 0x0F;
+ msgData.servingNetwork.mccDigit2 = (tai->plmn_id.idx[0] & 0xF0) >> 4;
+ msgData.servingNetwork.mccDigit3 = tai->plmn_id.idx[1] & 0x0F;
+ msgData.servingNetwork.mncDigit1 = tai->plmn_id.idx[2] & 0x0F;
+ msgData.servingNetwork.mncDigit2 = (tai->plmn_id.idx[2] & 0xF0) >> 4;
+ msgData.servingNetwork.mncDigit3 = (tai->plmn_id.idx[1] & 0xF0) >> 4;
+
+ msgData.ratType.ratType = 6;
+
+ msgData.indicationFlagsIePresent = true;
+ msgData.indicationFlags.iDFI = true;
+ msgData.indicationFlags.iMSV = true;
+
+ msgData.senderFTeidForControlPlane.ipv4present = true;
+ msgData.senderFTeidForControlPlane.interfaceType = 10;
+ msgData.senderFTeidForControlPlane.ipV4Address.ipValue = g_s11_cfg.local_egtp_ip;
+ msgData.senderFTeidForControlPlane.teidGreKey = g_csReqInfo->ue_idx;
+
+ msgData.pgwS5S8AddressForControlPlaneOrPmipIePresent = true;
+ msgData.pgwS5S8AddressForControlPlaneOrPmip.ipv4present = true;
+ msgData.pgwS5S8AddressForControlPlaneOrPmip.interfaceType = 7;
+ msgData.pgwS5S8AddressForControlPlaneOrPmip.ipV4Address.ipValue = g_s11_cfg.pgw_ip;
+
+ msgData.accessPointName.apnValue.count = g_csReqInfo->apn.len;
+ memcpy(msgData.accessPointName.apnValue.values, g_csReqInfo->apn.val, g_csReqInfo->apn.len);
+
+ msgData.selectionModeIePresent = true;
+ msgData.selectionMode.selectionMode = 1;
+
+ msgData.pdnTypeIePresent = true;
+ msgData.pdnType.pdnType = 1;
+
+ msgData.pdnAddressAllocationIePresent = true;
+ msgData.pdnAddressAllocation.pdnType = 1;
+ msgData.pdnAddressAllocation.ipV4Address.ipValue = 0;
+
+ msgData.maximumApnRestrictionIePresent = true;
+ msgData.maximumApnRestriction.restrictionValue = 0;
+
+ /* Bearer Context */
+ msgData.bearerContextsToBeCreatedCount = 1;
+ msgData.bearerContextsToBeCreated[0].epsBearerId.epsBearerId = 5;
+
+ msgData.bearerContextsToBeCreated[0].bearerLevelQos.pci = 1;
+ msgData.bearerContextsToBeCreated[0].bearerLevelQos.pl = 11;
+ msgData.bearerContextsToBeCreated[0].bearerLevelQos.pvi = 0;
+ msgData.bearerContextsToBeCreated[0].bearerLevelQos.qci = 9;
+
+ uint32_t mbr_uplink = htonl(MBR_UPLINK);
+ uint32_t mbr_downlink = htonl(MBR_DOWNLINK);
+
+ msgData.bearerContextsToBeCreated[0].bearerLevelQos.maxBitRateUl.count = 5;
+ msgData.bearerContextsToBeCreated[0].bearerLevelQos.maxBitRateDl.count = 5;
+ memcpy(&msgData.bearerContextsToBeCreated[0].bearerLevelQos.maxBitRateUl.values, &mbr_uplink, sizeof(mbr_uplink));
+ memcpy(&msgData.bearerContextsToBeCreated[0].bearerLevelQos.maxBitRateDl.values, &mbr_downlink, sizeof(mbr_downlink));
+ msgData.bearerContextsToBeCreated[0].bearerLevelQos.guraranteedBitRateUl.count = 5;
+ msgData.bearerContextsToBeCreated[0].bearerLevelQos.guaranteedBitRateDl.count = 5;
+
+ msgData.aggregateMaximumBitRateIePresent = true;
+ msgData.aggregateMaximumBitRate.maxMbrUplink = g_csReqInfo->max_requested_bw_ul;
+ msgData.aggregateMaximumBitRate.maxMbrDownlink = g_csReqInfo->max_requested_bw_dl;
+
+ bool rc = GtpV2Stack_buildGtpV2Message(gtpStack_gp, csReqMsgBuf_p, >pHeader, &msgData);
+ if (rc == false)
+ {
+ log_msg(LOG_ERROR, "Failed to encode create session request\n");
+ return E_FAIL;
+ }
+
+ log_msg(LOG_INFO, "send %d bytes.\n",MsgBuffer_getBufLen(csReqMsgBuf_p));
+
+ int res = sendto (
+ g_s11_fd,
+ MsgBuffer_getDataPointer(csReqMsgBuf_p),
+ MsgBuffer_getBufLen(csReqMsgBuf_p), 0,
+ (struct sockaddr*)&g_s11_cp_addr,
+ g_s11_serv_size);
+ if (res < 0) {
+ log_msg(LOG_ERROR,"Error in sendto in detach stage 3 post to next\n");
+ }
+
+ log_msg(LOG_INFO,"%d bytes sent. Err : %d, %s\n",res,errno,
+ strerror(errno));
+
+ MsgBuffer_reset(csReqMsgBuf_p);
+
+ return SUCCESS;
+}
+
+/**
+* Thread function for stage.
+*/
+void*
+create_session_handler(void *data)
+{
+ log_msg(LOG_INFO, "Create Session Request handler\n");
+
+ create_session_processing((struct CS_Q_msg *) data);
+
+ return NULL;
+}
diff --git a/src/s11/handlers/ddn_ack_handler.c b/src/s11/handlers/ddn_ack_handler.c
new file mode 100644
index 0000000..73a459a
--- /dev/null
+++ b/src/s11/handlers/ddn_ack_handler.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
+ * Copyright (c) 2017 Intel Corporation
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "gtpv2c.h"
+#include "gtpv2c_ie.h"
+#include "msgType.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+
+
+/****Globals and externs ***/
+
+/*S11 CP communication parameters*/
+extern int g_s11_fd;
+extern struct sockaddr_in g_s11_cp_addr;
+extern socklen_t g_s11_serv_size;
+extern volatile uint32_t g_s11_sequence;
+static char buf[S11_DDN_ACK_BUF_SIZE];
+
+struct thread_pool *g_tpool;
+
+extern struct GtpV2Stack* gtpStack_gp;
+extern volatile uint32_t g_s11_sequence;
+
+struct MsgBuffer* ddnAckMsgBuf_p = NULL;
+/****Global and externs end***/
+
+/**
+* Stage specific message processing.
+*/
+static int
+ddn_ack_processing(struct DDN_ACK_Q_msg *ddn_ack_msg)
+{
+ GtpV2MessageHeader gtpHeader;
+ gtpHeader.msgType = 177;
+ gtpHeader.sequenceNumber = ddn_ack_msg->seq_no;
+ gtpHeader.teidPresent = true;
+ gtpHeader.teid = ddn_ack_msg->ue_idx;
+
+ DownlinkDataNotificationAcknowledgeMsgData msgData;
+ memset(&msgData, 0, sizeof(DownlinkDataNotificationAcknowledgeMsgData));
+
+ msgData.cause.causeValue = ddn_ack_msg->cause;
+
+
+ GtpV2Stack_buildGtpV2Message(gtpStack_gp, ddnAckMsgBuf_p, >pHeader, &msgData);
+ g_s11_sequence++;
+
+ sendto(g_s11_fd,
+ MsgBuffer_getDataPointer(ddnAckMsgBuf_p),
+ MsgBuffer_getBufLen(ddnAckMsgBuf_p), 0,
+ (struct sockaddr*)&g_s11_cp_addr, g_s11_serv_size);
+
+ log_msg(LOG_INFO, "DDN Ack Sent, len - %d bytes.\n", MsgBuffer_getBufLen(ddnAckMsgBuf_p));
+ MsgBuffer_reset(ddnAckMsgBuf_p);
+ return SUCCESS;
+}
+
+
+/**
+* Thread function for stage.
+*/
+void*
+ddn_ack_handler(void *data)
+{
+ log_msg(LOG_INFO, "DDN Ack handler initialized\n");
+ ddn_ack_processing((struct DDN_ACK_Q_msg *)data);
+ return NULL;
+}
+
diff --git a/src/s11/handlers/delete_session_handler.c b/src/s11/handlers/delete_session_handler.c
new file mode 100644
index 0000000..b7758b3
--- /dev/null
+++ b/src/s11/handlers/delete_session_handler.c
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
+ * Copyright (c) 2017 Intel Corporation
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "gtpv2c.h"
+#include "gtpv2c_ie.h"
+#include "msgType.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+/************************************************************************
+Current file : Stage 1 handler.
+ATTACH stages :
+ Stage 1 : detach request -->delete session
+**************************************************************************/
+
+/****Globals and externs ***/
+
+/*S11 CP communication parameters*/
+extern int g_s11_fd;
+extern struct sockaddr_in g_s11_cp_addr;
+extern socklen_t g_s11_serv_size;
+extern volatile uint32_t g_s11_sequence;
+static char buf[S11_DTCHREQ_STAGE1_BUF_SIZE];
+
+struct thread_pool *g_tpool;
+
+extern struct GtpV2Stack* gtpStack_gp;
+extern volatile uint32_t g_s11_sequence;
+
+struct MsgBuffer* dsReqMsgBuf_p = NULL;
+/****Global and externs end***/
+
+/**
+* Stage specific message processing.
+*/
+static int
+delete_session_processing(struct DS_Q_msg *ds_msg)
+{
+ GtpV2MessageHeader gtpHeader;
+ gtpHeader.msgType = GTP_DELETE_SESSION_REQ;
+ gtpHeader.sequenceNumber = g_s11_sequence;
+ gtpHeader.teidPresent = true;
+ gtpHeader.teid = ds_msg->s11_sgw_c_fteid.header.teid_gre;
+
+ DeleteSessionRequestMsgData msgData;
+ memset(&msgData, 0, sizeof(DeleteSessionRequestMsgData));
+
+ msgData.indicationFlagsIePresent = true;
+ msgData.indicationFlags.iOI = true;
+
+ msgData.linkedEpsBearerIdIePresent = true;
+ msgData.linkedEpsBearerId.epsBearerId = ds_msg->bearer_id;
+
+ GtpV2Stack_buildGtpV2Message(gtpStack_gp, dsReqMsgBuf_p, >pHeader, &msgData);
+ g_s11_sequence++;
+
+ sendto(g_s11_fd,
+ MsgBuffer_getDataPointer(dsReqMsgBuf_p),
+ MsgBuffer_getBufLen(dsReqMsgBuf_p), 0,
+ (struct sockaddr*)&g_s11_cp_addr, g_s11_serv_size);
+ log_msg(LOG_INFO, "Send delete session request\n");
+
+ MsgBuffer_reset(dsReqMsgBuf_p);
+
+ return SUCCESS;
+}
+
+
+/**
+* Thread function for stage.
+*/
+void*
+delete_session_handler(void *data)
+{
+ log_msg(LOG_INFO, "Delete session handler initialized\n");
+
+ delete_session_processing((struct DS_Q_msg *)data);
+ return NULL;
+}
+
diff --git a/src/s11/handlers/modify_bearer_handler.c b/src/s11/handlers/modify_bearer_handler.c
new file mode 100644
index 0000000..978ae8a
--- /dev/null
+++ b/src/s11/handlers/modify_bearer_handler.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
+ * Copyright (c) 2017 Intel Corporation
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <string.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "s11_structs.h"
+#include "msgType.h"
+//#include "stage7_info.h"
+#include "gtpv2c.h"
+#include "gtpv2c_ie.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+
+/************************************************************************
+Current file : Stage 7 handler. To listen MB from mme-app and fwd to CP
+ATTACH stages :
+ Stage 1 : IAM-->[stage1 handler]-->AIR, ULR
+ Stage 2 : AIA, ULA -->[stage2 handler]--> Auth req
+ Stage 3 : Auth resp-->[stage3 handler]-->Sec mode cmd
+ Stage 4 : sec mode resp-->[stage4 handler]-->esm infor req
+ Stage 5 : esm infor resp-->[stage5 handler]-->create session
+ Stage 6 : create session resp-->[stage6 handler]-->init ctx setup
+--> Stage 7 : attach complete-->[stage7 handler]-->modify bearer
+**************************************************************************/
+
+/****Globals and externs ***/
+
+extern int g_s11_fd;
+extern struct sockaddr_in g_s11_cp_addr;
+extern socklen_t g_s11_serv_size;
+/*TODO: S11 protocol sequence number - need to make it atomic. multiple thread to access this*/
+extern volatile uint32_t g_s11_sequence;
+static char buf[S11_MBREQ_STAGE7_BUF_SIZE];
+
+/*TODO: S11 protocol sequence number - need to make it atomic. multiple thread to access this*/
+extern volatile uint32_t g_s11_sequence;
+
+struct MsgBuffer* mbReqMsgBuf_p = NULL;
+extern struct GtpV2Stack* gtpStack_gp;
+/****Global and externs end***/
+/**
+* Stage specific message processing.
+*/
+static int
+modify_bearer_processing(struct MB_Q_msg *mb_msg)
+{
+ GtpV2MessageHeader gtpHeader;
+ gtpHeader.msgType = GTP_MODIFY_BEARER_REQ;
+ gtpHeader.sequenceNumber = g_s11_sequence;
+ gtpHeader.teidPresent = true;
+ gtpHeader.teid = mb_msg->s11_sgw_c_fteid.header.teid_gre;
+
+ g_s11_sequence++;
+
+ ModifyBearerRequestMsgData msgData;
+ memset(&msgData, 0, sizeof(msgData));
+ msgData.bearerContextsToBeModifiedCount = 1;
+ msgData.bearerContextsToBeModified[0].epsBearerId.epsBearerId = 5;
+ msgData.bearerContextsToBeModified[0].s1EnodebFTeidIePresent = true;
+ msgData.bearerContextsToBeModified[0].s1EnodebFTeid.ipv4present = true;
+ msgData.bearerContextsToBeModified[0].s1EnodebFTeid.interfaceType = mb_msg->s1u_enb_fteid.header.iface_type;
+ msgData.bearerContextsToBeModified[0].s1EnodebFTeid.teidGreKey = mb_msg->s1u_enb_fteid.header.teid_gre;
+ msgData.bearerContextsToBeModified[0].s1EnodebFTeid.ipV4Address.ipValue = mb_msg->s1u_enb_fteid.ip.ipv4.s_addr;
+
+ GtpV2Stack_buildGtpV2Message(gtpStack_gp, mbReqMsgBuf_p, >pHeader, &msgData);
+ sendto(g_s11_fd,
+ MsgBuffer_getDataPointer(mbReqMsgBuf_p),
+ MsgBuffer_getBufLen(mbReqMsgBuf_p), 0,
+ (struct sockaddr*)&g_s11_cp_addr,
+ g_s11_serv_size);
+ //TODO " error chk, eagain etc?
+ log_msg(LOG_INFO, "Modify beader send, len - %d bytes.\n", MsgBuffer_getBufLen(mbReqMsgBuf_p));
+
+ MsgBuffer_reset(mbReqMsgBuf_p);
+
+ return SUCCESS;
+}
+
+/**
+* Thread function for stage.
+*/
+void*
+modify_bearer_handler(void *data)
+{
+ log_msg(LOG_INFO, "Modify bearer handler initialized\n");
+
+ modify_bearer_processing((struct MB_Q_msg *)data);
+
+ return NULL;
+}
diff --git a/src/s11/handlers/release_bearer_handler.c b/src/s11/handlers/release_bearer_handler.c
new file mode 100644
index 0000000..e18cc37
--- /dev/null
+++ b/src/s11/handlers/release_bearer_handler.c
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <string.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "s11_structs.h"
+#include "msgType.h"
+//#include "stage7_info.h"
+#include "gtpv2c.h"
+#include "gtpv2c_ie.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+
+/************************************************************************
+Current file : Stage 7 handler. To listen MB from mme-app and fwd to CP
+ATTACH stages :
+ Stage 1 : IAM-->[stage1 handler]-->AIR, ULR
+ Stage 2 : AIA, ULA -->[stage2 handler]--> Auth req
+ Stage 3 : Auth resp-->[stage3 handler]-->Sec mode cmd
+ Stage 4 : sec mode resp-->[stage4 handler]-->esm infor req
+ Stage 5 : esm infor resp-->[stage5 handler]-->create session
+ Stage 6 : create session resp-->[stage6 handler]-->init ctx setup
+--> Stage 7 : attach complete-->[stage7 handler]-->modify bearer
+**************************************************************************/
+
+/****Globals and externs ***/
+
+
+extern int g_s11_fd;
+extern struct sockaddr_in g_s11_cp_addr;
+extern socklen_t g_s11_serv_size;
+/*TODO: S11 protocol sequence number - need to make it atomic. multiple thread to access this*/
+extern volatile uint32_t g_s11_sequence;
+
+struct MsgBuffer* rbReqMsgBuf_p = NULL;
+extern struct GtpV2Stack* gtpStack_gp;
+
+
+/****Global and externs end***/
+/**
+* Stage specific message processing.
+*/
+static int
+release_bearer_processing(struct RB_Q_msg *rb_msg)
+{
+ GtpV2MessageHeader gtpHeader;
+ gtpHeader.msgType = GTP_RELEASE_BEARER_REQ;
+ gtpHeader.sequenceNumber = g_s11_sequence;
+ gtpHeader.teidPresent = true;
+ gtpHeader.teid = rb_msg->s11_sgw_c_fteid.header.teid_gre;
+
+ g_s11_sequence++;
+
+ ReleaseAccessBearersRequestMsgData msgData;
+ memset(&msgData, 0, sizeof(msgData));
+
+ msgData.indicationFlagsIePresent = true;
+ msgData.indicationFlags.iOI = true;
+
+ GtpV2Stack_buildGtpV2Message(gtpStack_gp, rbReqMsgBuf_p, >pHeader, &msgData);
+
+ sendto(g_s11_fd,
+ MsgBuffer_getDataPointer(rbReqMsgBuf_p),
+ MsgBuffer_getBufLen(rbReqMsgBuf_p), 0,
+ (struct sockaddr*)&g_s11_cp_addr,
+ g_s11_serv_size);
+ //TODO " error chk, eagain etc?
+ log_msg(LOG_INFO, "Release Bearer sent, len - %d bytes.\n", MsgBuffer_getBufLen(rbReqMsgBuf_p));
+
+ MsgBuffer_reset(rbReqMsgBuf_p);
+
+ return SUCCESS;
+
+}
+
+
+/**
+* Thread function for stage.
+*/
+void*
+release_bearer_handler(void *data)
+{
+
+ log_msg(LOG_INFO, "Release bearer handler initialized\n");
+
+ release_bearer_processing((struct RB_Q_msg *)data);
+
+ return NULL;
+}
+
+
diff --git a/src/s11/handlers/s11_CS_resp_handler.c b/src/s11/handlers/s11_CS_resp_handler.c
new file mode 100644
index 0000000..14c6af7
--- /dev/null
+++ b/src/s11/handlers/s11_CS_resp_handler.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
+ * Copyright (c) 2017 Intel Corporation
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "err_codes.h"
+#include "options.h"
+#include "ipc_api.h"
+#include "message_queues.h"
+#include "s11.h"
+#include "s11_config.h"
+#include "msgType.h"
+//#include "stage6_info.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+
+/*Globals and externs*/
+extern int g_resp_fd;
+extern struct GtpV2Stack* gtpStack_gp;
+/*End : globals and externs*/
+
+
+int
+s11_CS_resp_handler(MsgBuffer* message, GtpV2MessageHeader* hdr)
+{
+
+ struct gtp_incoming_msg_data_t csr_info;
+ /*****Message structure***
+ */
+
+ /*Check whether has teid flag is set. Also check whether this check is needed for CSR.*/
+ csr_info.ue_idx = hdr->teid;
+ csr_info.msg_type = create_session_response;
+
+ CreateSessionResponseMsgData msgData;
+ memset(&msgData, 0, sizeof(CreateSessionResponseMsgData));
+
+ bool rc = GtpV2Stack_decodeMessage(gtpStack_gp, hdr, message, &msgData);
+ if(rc == false)
+ {
+ log_msg(LOG_ERROR, "s11_CS_resp_handler: "
+ "Failed to decode Create Session Response Msg %d\n",
+ hdr->teid);
+ return E_PARSING_FAILED;
+ }
+
+ csr_info.msg_data.csr_Q_msg_m.s11_sgw_fteid.header.iface_type = 11;
+ csr_info.msg_data.csr_Q_msg_m.s11_sgw_fteid.header.teid_gre = msgData.senderFTeidForControlPlane.teidGreKey;
+ csr_info.msg_data.csr_Q_msg_m.s11_sgw_fteid.header.v4 = 1;
+ csr_info.msg_data.csr_Q_msg_m.s11_sgw_fteid.ip.ipv4.s_addr = msgData.senderFTeidForControlPlane.ipV4Address.ipValue;
+
+ csr_info.msg_data.csr_Q_msg_m.s5s8_pgwc_fteid.header.iface_type = 7;
+ csr_info.msg_data.csr_Q_msg_m.s5s8_pgwc_fteid.header.teid_gre = msgData.pgwS5S8S2bFTeid.teidGreKey;
+ csr_info.msg_data.csr_Q_msg_m.s5s8_pgwc_fteid.header.v4 = 1;
+ csr_info.msg_data.csr_Q_msg_m.s5s8_pgwc_fteid.ip.ipv4.s_addr = msgData.pgwS5S8S2bFTeid.ipV4Address.ipValue;
+
+ csr_info.msg_data.csr_Q_msg_m.s1u_sgw_fteid.header.iface_type = 1;
+ csr_info.msg_data.csr_Q_msg_m.s1u_sgw_fteid.header.teid_gre = msgData.bearerContextsCreated[0].s1USgwFTeid.teidGreKey;
+ csr_info.msg_data.csr_Q_msg_m.s1u_sgw_fteid.header.v4 = 1;
+ csr_info.msg_data.csr_Q_msg_m.s1u_sgw_fteid.ip.ipv4.s_addr = msgData.bearerContextsCreated[0].s1USgwFTeid.ipV4Address.ipValue;
+
+ csr_info.msg_data.csr_Q_msg_m.s5s8_pgwu_fteid.header.iface_type = 5;
+ csr_info.msg_data.csr_Q_msg_m.s5s8_pgwu_fteid.header.teid_gre = msgData.bearerContextsCreated[0].s5S8UPgwFTeid.teidGreKey;
+ csr_info.msg_data.csr_Q_msg_m.s5s8_pgwu_fteid.header.v4 = 1;
+ csr_info.msg_data.csr_Q_msg_m.s5s8_pgwu_fteid.ip.ipv4.s_addr = msgData.bearerContextsCreated[0].s5S8UPgwFTeid.ipV4Address.ipValue;
+
+ csr_info.msg_data.csr_Q_msg_m.pdn_addr.pdn_type = 1;
+ csr_info.msg_data.csr_Q_msg_m.pdn_addr.ip_type.ipv4.s_addr = msgData.pdnAddressAllocation.ipV4Address.ipValue;
+
+
+ csr_info.destInstAddr = htonl(mmeAppInstanceNum_c);
+ csr_info.srcInstAddr = htonl(s11AppInstanceNum_c);
+
+ /*Send CS response msg*/
+ send_tipc_message(g_resp_fd, mmeAppInstanceNum_c, (char *)&csr_info, GTP_READ_MSG_BUF_SIZE);
+ log_msg(LOG_INFO, "Send CS resp to mme-app stage6.\n");
+
+ return SUCCESS;
+}
diff --git a/src/s11/handlers/s11_DDN_handler.c b/src/s11/handlers/s11_DDN_handler.c
new file mode 100644
index 0000000..ac10f67
--- /dev/null
+++ b/src/s11/handlers/s11_DDN_handler.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "err_codes.h"
+#include "options.h"
+#include "ipc_api.h"
+#include "message_queues.h"
+#include "s11.h"
+#include "s11_config.h"
+#include "msgType.h"
+//#include "detach_stage2_info.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+/*Globals and externs*/
+extern int g_resp_fd;
+extern struct GtpV2Stack* gtpStack_gp;
+/*End : globals and externs*/
+
+
+int
+s11_DDN_handler(MsgBuffer* message, GtpV2MessageHeader* hdr)
+{
+
+
+ struct gtp_incoming_msg_data_t ddn_info;
+ ddn_info.msg_type = downlink_data_notification;
+ ddn_info.ue_idx = hdr->teid;
+ ddn_info.msg_data.ddn_Q_msg_m.seq_no = hdr->sequenceNumber;
+
+
+ DownlinkDataNotificationMsgData msgData;
+ memset(&msgData, 0, sizeof(DownlinkDataNotificationMsgData));
+
+ bool rc = GtpV2Stack_decodeMessage(gtpStack_gp, hdr, message, &msgData);
+ if(rc == false)
+ {
+
+ log_msg(LOG_ERROR, "s11_Ddn_handler: "
+ "Failed to decode ddn Msg %d\n",
+ hdr->teid);
+ return E_PARSING_FAILED;
+ }
+ /*****Message structure****/
+ log_msg(LOG_INFO, "Parse S11 Ddn message\n");
+
+ //TODO : check cause for the result verification
+
+ ddn_info.msg_data.ddn_Q_msg_m.arp.prioLevel = msgData.allocationRetentionPriority.pl;
+ ddn_info.msg_data.ddn_Q_msg_m.arp.preEmptionCapab = msgData.allocationRetentionPriority.pci;
+ ddn_info.msg_data.ddn_Q_msg_m.arp.preEmptionVulnebility = msgData.allocationRetentionPriority.pvi;
+ ddn_info.msg_data.ddn_Q_msg_m.cause = msgData.cause.causeValue;
+ ddn_info.msg_data.ddn_Q_msg_m.eps_bearer_id = msgData.epsBearerId.epsBearerId;
+
+ ddn_info.destInstAddr = htonl(mmeAppInstanceNum_c);
+ ddn_info.srcInstAddr = htonl(s11AppInstanceNum_c);
+
+ /*Send DDN msg*/
+ send_tipc_message(g_resp_fd, mmeAppInstanceNum_c, (char *)&ddn_info, GTP_READ_MSG_BUF_SIZE);
+
+ log_msg(LOG_INFO, "Send ddn to mme-app\n");
+
+
+ return SUCCESS;
+}
diff --git a/src/s11/handlers/s11_DS_resp_handler.c b/src/s11/handlers/s11_DS_resp_handler.c
new file mode 100644
index 0000000..abb6d53
--- /dev/null
+++ b/src/s11/handlers/s11_DS_resp_handler.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
+ * Copyright (c) 2017 Intel Corporation
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "err_codes.h"
+#include "options.h"
+#include "ipc_api.h"
+#include "message_queues.h"
+#include "s11.h"
+#include "s11_config.h"
+#include "msgType.h"
+//#include "detach_stage2_info.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+/*Globals and externs*/
+extern int g_resp_fd;
+
+/*End : globals and externs*/
+
+int
+s11_DS_resp_handler(MsgBuffer* message, GtpV2MessageHeader* hdr)
+{
+
+
+ struct gtp_incoming_msg_data_t dsr_info;
+ dsr_info.msg_type = delete_session_response;
+
+ /*****Message structure****/
+ log_msg(LOG_INFO, "Parse S11 DS resp message\n");
+
+ //TODO : check cause for the result verification
+
+ /*Check whether has teid flag is set.
+ * Also check whether this check is needed for DSR.
+ * */
+ dsr_info.ue_idx = hdr->teid;
+
+ dsr_info.destInstAddr = htonl(mmeAppInstanceNum_c);
+ dsr_info.srcInstAddr = htonl(s11AppInstanceNum_c);
+
+ /*Send CS response msg*/
+ send_tipc_message(g_resp_fd, mmeAppInstanceNum_c, (char *)&dsr_info,
+ GTP_READ_MSG_BUF_SIZE);
+ log_msg(LOG_INFO, "Send DS resp to mme-app stage8.\n");
+
+ return SUCCESS;
+}
diff --git a/src/s11/handlers/s11_MB_resp_handler.c b/src/s11/handlers/s11_MB_resp_handler.c
new file mode 100644
index 0000000..5c25327
--- /dev/null
+++ b/src/s11/handlers/s11_MB_resp_handler.c
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
+ * Copyright (c) 2017 Intel Corporation
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "err_codes.h"
+#include "options.h"
+#include "ipc_api.h"
+#include "message_queues.h"
+#include "s11.h"
+#include "s11_config.h"
+#include "msgType.h"
+//#include "stage8_info.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+/*Globals and externs*/
+extern int g_resp_fd;
+
+/*End : globals and externs*/
+
+int
+s11_MB_resp_handler(MsgBuffer* message, GtpV2MessageHeader* hdr)
+{
+
+ struct gtp_incoming_msg_data_t mbr_info;
+
+ /*****Message structure***
+ */
+ log_msg(LOG_INFO, "Parse S11 MB resp message\n");
+
+ //TODO : check cause foor the result verification
+
+ /*Check whether has teid flag is set. Also check whether this check is needed for CSR.*/
+ mbr_info.ue_idx = hdr->teid;
+ mbr_info.msg_type = modify_bearer_response;
+
+ mbr_info.destInstAddr = htonl(mmeAppInstanceNum_c);
+ mbr_info.srcInstAddr = htonl(s11AppInstanceNum_c);
+ /*Send CS response msg*/
+ send_tipc_message(g_resp_fd, mmeAppInstanceNum_c, (char *)&mbr_info, GTP_READ_MSG_BUF_SIZE);
+ log_msg(LOG_INFO, "Send MB resp to mme-app stage8.\n");
+
+ return SUCCESS;
+}
diff --git a/src/s11/handlers/s11_RB_resp_handler.c b/src/s11/handlers/s11_RB_resp_handler.c
new file mode 100644
index 0000000..d9d7897
--- /dev/null
+++ b/src/s11/handlers/s11_RB_resp_handler.c
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "err_codes.h"
+#include "options.h"
+#include "ipc_api.h"
+#include "message_queues.h"
+#include "s11.h"
+#include "s11_config.h"
+#include "msgType.h"
+//#include "stage8_info.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+
+/*Globals and externs*/
+extern int g_resp_fd;
+extern struct GtpV2Stack* gtpStack_gp;
+
+/*End : globals and externs*/
+
+int
+s11_RB_resp_handler(MsgBuffer* message, GtpV2MessageHeader* hdr)
+{
+ struct gtp_incoming_msg_data_t rbr_info;
+
+ /*****Message structure***
+ */
+ log_msg(LOG_INFO, "Parse S11 RB resp message\n");
+
+ //TODO : check cause for the result verification
+
+ rbr_info.ue_idx = hdr->teid;
+ rbr_info.msg_type = release_bearer_response;
+
+ ReleaseAccessBearersResponseMsgData msgData;
+ memset(&msgData, 0, sizeof(ReleaseAccessBearersResponseMsgData));
+
+ bool rc = GtpV2Stack_decodeMessage(gtpStack_gp, hdr, message, &msgData);
+ if(rc == false)
+ {
+ log_msg(LOG_ERROR, "s11_RB_resp_handler: "
+ "Failed to decode Release Access Bearer Response Msg %d\n",
+ hdr->teid);
+ return E_PARSING_FAILED;
+ }
+
+
+ rbr_info.destInstAddr = htonl(mmeAppInstanceNum_c);
+ rbr_info.srcInstAddr = htonl(s11AppInstanceNum_c);
+
+ /*Send CS response msg*/
+ send_tipc_message(g_resp_fd, mmeAppInstanceNum_c, (char *)&rbr_info, GTP_READ_MSG_BUF_SIZE);
+ log_msg(LOG_INFO, "Send RB resp to mme-app stage2.\n");
+
+ return SUCCESS;
+}
diff --git a/src/s11/handlers/s11_msg_delegator.c b/src/s11/handlers/s11_msg_delegator.c
new file mode 100644
index 0000000..fbb0d7b
--- /dev/null
+++ b/src/s11/handlers/s11_msg_delegator.c
@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
+ * Copyright (c) 2017 Intel Corporation
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "err_codes.h"
+#include "options.h"
+#include "ipc_api.h"
+#include "message_queues.h"
+#include "s11.h"
+#include "s11_config.h"
+#include "s11_structs.h"
+#include "../../gtpV2Codec/gtpV2StackWrappers.h"
+
+extern struct GtpV2Stack* gtpStack_gp;
+int s11_CS_resp_handler(MsgBuffer* message, GtpV2MessageHeader* hdr);
+int s11_MB_resp_handler(MsgBuffer* message, GtpV2MessageHeader* hdr);
+int s11_DS_resp_handler(MsgBuffer* message, GtpV2MessageHeader* hdr);
+int s11_RB_resp_handler(MsgBuffer* message, GtpV2MessageHeader* hdr);
+int s11_DDN_handler(MsgBuffer* message, GtpV2MessageHeader* hdr);
+
+/*
+ Get count of no of IEs in gtpv2c msg
+*/
+static int
+get_IE_cnt(char *msg, int len)
+{
+ int cnt = 0;
+ char *tmp = msg+11;
+ struct s11_IE_header *header = (struct s11_IE_header *)tmp;
+
+ for(; (char *)tmp <= msg + len; ++cnt) {
+ tmp += sizeof(struct s11_IE_header) + ntohs(header->ie_len);
+ header = (struct s11_IE_header*)tmp;
+ }
+ return cnt;
+}
+
+void
+network_cp_fteid(struct fteid *teid, char *data)
+{
+ unsigned int *tmp;
+
+ memcpy(teid, data, sizeof(struct fteid));
+ teid->header.teid_gre = ntohl(teid->header.teid_gre);
+ tmp = (unsigned int*)(data+5);
+ *tmp = ntohl(*tmp);
+ memcpy(&(teid->ip.ipv4), tmp, sizeof(struct in_addr));
+}
+
+int
+parse_bearer_ctx(struct bearer_ctx *bearer, char* data, short len)
+{
+ char no_of_ies = 4;
+ //TODO: count no of IEs
+
+ for(int i=0; i < no_of_ies; ++i) {
+ struct s11_IE_header *header = (struct s11_IE_header*)data;
+ char *value = data + sizeof(struct s11_IE_header);
+
+ switch(header->ie_type){
+ case S11_IE_CAUSE:
+ memcpy(&(bearer->cause), value, sizeof(struct gtp_cause));
+ break;
+
+ case S11_IE_FTEID_C:{
+ #define S1U_SGW_FTEID 1 /*binary 0001*/
+ if((0x0F & (unsigned char)(*value))
+ == S1U_SGW_FTEID) {
+ network_cp_fteid(&bearer->s1u_sgw_teid, value);
+ }
+ else { /*s5s8 pgw_U ftied*/
+ network_cp_fteid(&bearer->s5s8_pgw_u_teid, value);
+ }
+ break;
+ }
+
+ case S11_IE_EPS_BEARER_ID:
+ bearer->eps_bearer_id = (unsigned char)(*value);
+ break;
+
+ default:
+ log_msg(LOG_ERROR, "Unhandled S11 bearer IE: %d\n", header->ie_type);
+ }
+
+ data += ntohs(header->ie_len) + sizeof(struct s11_IE_header); /*goto next IE*/
+ }
+ return SUCCESS;
+}
+
+int
+parse_gtpv2c_IEs(char *msg, int len, struct s11_proto_IE *proto_ies)
+{
+ proto_ies->no_of_ies = get_IE_cnt(msg, len);
+
+ if(0 == proto_ies->no_of_ies) {
+ log_msg(LOG_INFO, "No IEs recvd in message\n");
+ return SUCCESS;
+ }
+ log_msg(LOG_INFO, "No of IEs - %d\n", proto_ies->no_of_ies);
+
+ /*allocated IEs for message*/
+ proto_ies->s11_ies = (struct s11_IE*)calloc(sizeof(struct s11_IE),
+ proto_ies->no_of_ies);
+ msg +=11;
+
+ for(int i=0; i < proto_ies->no_of_ies; ++i) {
+ struct s11_IE *ie = &(proto_ies->s11_ies[i]);
+ char *data = msg + sizeof(struct s11_IE_header);
+
+ memcpy(&(ie->header), msg, sizeof(struct s11_IE_header));
+
+ switch(ie->header.ie_type){
+ case S11_IE_CAUSE:
+ memcpy(&(ie->data.cause), data, sizeof(struct gtp_cause));
+ break;
+
+ case S11_IE_FTEID_C:{
+ #define S11_SGW_C_FTEID 11 /*binary 1011*/
+ if((0x0F & (unsigned char)(*data))
+ == S11_SGW_C_FTEID) {
+ network_cp_fteid(&(ie->data.s11_sgw_fteid), data);
+ }
+ else { /*s5s8 pgw_c ftied*/
+ network_cp_fteid(&(ie->data.s5s8_pgw_c_fteid), data);
+ }
+ break;
+ }
+
+ case S11_IE_PAA: {
+ memcpy(&(ie->data.pdn_addr.pdn_type), data,
+ sizeof(ie->data.pdn_addr.pdn_type));
+ memcpy(&(ie->data.pdn_addr.ip_type.ipv4), data+1, sizeof(int));
+ break;
+ }
+
+ case S11_IE_APN_RESTRICTION:
+ break;
+
+ case S11_IE_BEARER_CTX:
+ parse_bearer_ctx(&(ie->data.bearer), data, ntohs(ie->header.ie_len));
+ break;
+
+ default:
+ log_msg(LOG_ERROR, "Unhandled S11 IE: %d\n", ie->header.ie_type);
+ }
+
+ msg += (ntohs(((struct s11_IE_header*)msg)->ie_len) + sizeof(struct s11_IE_header)) ; /*goto next IE*/
+ }
+ return SUCCESS;
+}
+
+void
+handle_s11_message(void *message)
+{
+ log_msg(LOG_INFO, "S11 recv msg handler.\n");
+
+ MsgBuffer* msgBuf_p = (MsgBuffer*)(message);
+
+ GtpV2MessageHeader msgHeader;
+
+ bool rc = GtpV2Stack_decodeMessageHeader(gtpStack_gp, &msgHeader, msgBuf_p);
+
+ switch(msgHeader.msgType){
+ case S11_GTP_CREATE_SESSION_RESP:
+ s11_CS_resp_handler(msgBuf_p, &msgHeader);
+ break;
+
+ case S11_GTP_MODIFY_BEARER_RESP:
+ s11_MB_resp_handler(msgBuf_p, &msgHeader);
+ break;
+
+ case S11_GTP_DELETE_SESSION_RESP:
+ s11_DS_resp_handler(msgBuf_p, &msgHeader);
+ break;
+
+ case S11_GTP_RELEASE_BEARER_RESP:
+ s11_RB_resp_handler(msgBuf_p, &msgHeader);
+ break;
+
+ case S11_GTP_DDN:
+ s11_DDN_handler(msgBuf_p, &msgHeader);
+ break;
+
+ }
+ return;
+}