MME2 changes - Propped commits from openmme/paging branch. Added scripts
for code gen

Change-Id: Ie55032217232214ac8544ca76ea34335205329e4
diff --git a/src/s6a/handlers/aia_handler.c b/src/s6a/handlers/aia_handler.c
new file mode 100644
index 0000000..bb096dd
--- /dev/null
+++ b/src/s6a/handlers/aia_handler.c
@@ -0,0 +1,269 @@
+/*
+ * 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 <unistd.h>
+#include <freeDiameter/freeDiameter-host.h>
+#include <freeDiameter/libfdcore.h>
+#include <freeDiameter/libfdproto.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "s6a_fd.h"
+#include "s6a.h"
+#include "msgType.h"
+//#include "stage1_s6a_msg.h"
+#include "hss_message.h"
+
+#define DIAMETER_SUCCESS 2001
+
+/** Global and externs**/
+extern struct fd_dict_objects g_fd_dict_objs;
+extern struct fd_dict_data g_fd_dict_data;
+
+extern int g_Q_mme_S6a_fd;
+/**global and externs end**/
+
+/**
+ * @brief Parse authentication information avp recvd in AIA. This contains
+ * important security information for the UE
+ * @param [in] avp_data - AVP value received
+ * @param [out] aia - Security info is filled in to aia msg Q
+ * @return int - error code
+ */
+static int
+get_aia_sec_vector(struct avp *avp_data, struct aia_Q_msg *aia)
+{
+	struct avp *sub_avp = NULL;
+	struct avp_hdr *element = NULL;
+
+	CHECK_FCT_DO(fd_msg_avp_hdr(avp_data, &element),
+			return S6A_FD_ERROR);
+
+	/*Authentication-Info*/
+	if ((NULL == element) ||
+			(element->avp_code != g_fd_dict_data.auth_info.avp_code))
+		return S6A_FD_ERROR;
+
+	/*Find first sub child of Authentication-Info*/
+	CHECK_FCT_DO(fd_msg_browse(avp_data, MSG_BRW_FIRST_CHILD, &sub_avp,
+			NULL),
+			return S6A_FD_ERROR);
+
+	/*Lookup for sub element "E-UTRAN-Vector" in loop*/
+	while (NULL != sub_avp) {
+
+		fd_msg_avp_hdr(sub_avp, &element);
+
+		if ((NULL != element) && (element->avp_code ==
+			g_fd_dict_data.E_UTRAN_vector.avp_code))
+			/*Found the entry*/
+			break;
+
+		/*Iterate sub entries*/
+		CHECK_FCT_DO(fd_msg_browse(sub_avp, MSG_BRW_NEXT, &sub_avp,
+			NULL),
+			return S6A_FD_ERROR);
+	}
+
+	/*Element "E-UTRAN-Vector" not found, then return*/
+	if (NULL == sub_avp) return S6A_FD_ERROR;
+
+	CHECK_FCT_DO(fd_msg_browse(sub_avp, MSG_BRW_FIRST_CHILD,
+			&sub_avp, NULL),
+			return S6A_FD_ERROR);
+
+	/*Iterate all sub elements of E-UTRAN-Vector and filter sec vector params*/
+	for (;
+		NULL != sub_avp; /*Till null*/
+//		CHECK_FCT_DO(fd_msg_browse(sub_avp, MSG_BRW_NEXT, &sub_avp, NULL),
+//			return S6A_FD_ERROR) /*Iterate elements*/
+		) {
+
+		fd_msg_avp_hdr(sub_avp, &element);
+
+		/*AVP: RAND(1447) l=28 f=VM- vnd=TGPP*/
+		if (element->avp_code == g_fd_dict_data.RAND.avp_code) {
+
+			//if (element->avp_value->os.len > sizeof(aia->sec.rand.val))
+			if (element->avp_value->os.len > AIA_RAND_SIZE)
+				return S6A_FD_ERROR;
+
+			aia->sec.rand.len = element->avp_value->os.len;
+			memcpy(aia->sec.rand.val, element->avp_value->os.data,
+				aia->sec.rand.len);
+		}
+
+		/*AVP: XRES(1448) l=20 f=VM- vnd=TGPP*/
+		if (element->avp_code == g_fd_dict_data.XRES.avp_code) {
+
+			if (element->avp_value->os.len > AIA_RES_SIZE)
+				return S6A_FD_ERROR;
+
+			aia->sec.xres.len = element->avp_value->os.len;
+			memcpy(aia->sec.xres.val, element->avp_value->os.data,
+				aia->sec.xres.len);
+		}
+
+		/*AVP: AUTN(1449) l=28 f=VM- vnd=TGPP*/
+		if (element->avp_code == g_fd_dict_data.AUTN.avp_code) {
+
+			if (element->avp_value->os.len > AIA_AUTN_SIZE)
+				return S6A_FD_ERROR;
+
+			aia->sec.autn.len = element->avp_value->os.len;
+			memcpy(aia->sec.autn.val, element->avp_value->os.data,
+				aia->sec.autn.len);
+		}
+
+		/*AVP: KASME(1450) l=44 f=VM- vnd=TGPP*/
+		if (element->avp_code == g_fd_dict_data.KASME.avp_code) {
+
+			if (element->avp_value->os.len > AIA_KASME_SIZE)
+				return S6A_FD_ERROR;
+
+			aia->sec.kasme.len = element->avp_value->os.len;
+			memcpy(aia->sec.kasme.val, element->avp_value->os.data,
+				aia->sec.kasme.len);
+		}
+
+		CHECK_FCT_DO(fd_msg_browse(sub_avp, MSG_BRW_NEXT, &sub_avp, NULL),
+			return S6A_FD_ERROR) /*Iterate elements*/
+
+	}
+
+	return SUCCESS;
+}
+
+static
+void send_to_stage2(struct s6_incoming_msg_data_t *incoming_msg_p)
+{
+	TRACE_ENTRY("\n****************WRITE TO g_Q_mme_S6a_fd");
+
+	incoming_msg_p->destInstAddr = htonl(mmeAppInstanceNum_c);
+	incoming_msg_p->srcInstAddr = htonl(s6AppInstanceNum_c);
+
+	/*Send to stage2 queue*/
+	send_tipc_message(g_Q_mme_S6a_fd, mmeAppInstanceNum_c, (char*)incoming_msg_p, S6_READ_MSG_BUF_SIZE);
+}
+
+int aia_resp_callback(struct msg **buf, struct avp *_avp,
+		struct session *session, void *data,
+		enum disp_action * action)
+{
+	TRACE_ENTRY("\n****************ANJANA ******  Callback ----- >AIA recvd\n");
+	int res = SUCCESS, sess_id_len;
+	struct msg *resp = *buf;
+	struct avp *avp_ptr = NULL;
+	unsigned char *sess_id= NULL;
+	struct s6_incoming_msg_data_t s6_incoming_msgs;
+	struct avp_hdr *avp_hdr = NULL;
+
+	log_msg(LOG_INFO, "\nCallback ----- >AIA recvd\n");
+
+	dump_fd_msg(resp);
+	//TODO - workaround for dump call. Remove this.
+	{
+		char * buf = NULL;
+		size_t len = 0;
+		printf("*********AIA CALLBACK******%s\n", fd_msg_dump_treeview(&buf, &len, NULL, resp,
+					fd_g_config->cnf_dict, 0, 1));
+		free(buf);
+	}
+
+	CHECK_FCT_DO(fd_sess_getsid(session, &sess_id, (size_t*)&sess_id_len),
+		return S6A_FD_ERROR);
+
+	log_msg(LOG_INFO, "\nCallback ----- >session id=%s \n",sess_id);
+    
+	s6_incoming_msgs.msg_type = auth_info_answer;
+	/*Retrieve UE index embedded in to session ID string at AIR time*/
+	s6_incoming_msgs.ue_idx = get_ue_idx_from_fd_resp(sess_id, sess_id_len);
+
+	/*AVP: Result-Code(268)*/
+	fd_msg_search_avp(resp, g_fd_dict_objs.res_code, &avp_ptr);
+
+	if (NULL != avp_ptr) {
+		fd_msg_avp_hdr(avp_ptr, &avp_hdr);
+		res = avp_hdr->avp_value->u32;
+
+		if (DIAMETER_SUCCESS != res) {
+			log_msg(LOG_ERROR, "Diameter error with HSS\n");
+		}
+		//Vikram: chk res = SUCCESS;
+
+	} else {
+		struct fd_result fd_res;
+
+		avp_ptr = NULL;
+		fd_msg_search_avp(resp, g_fd_dict_objs.exp_res,
+			&avp_ptr);
+
+		if (NULL == avp_ptr) {
+			res = S6A_FD_ERROR;
+		} else if (parse_fd_result(avp_ptr, &fd_res) != 0) {
+			res = S6A_FD_ERROR;
+		} else res = fd_res.result_code;
+	}
+
+	if (DIAMETER_SUCCESS == res) {
+		/*AVP: Authentication-Info*/
+		fd_msg_search_avp(resp, g_fd_dict_objs.auth_info,
+			&avp_ptr);
+
+		if (NULL != avp_ptr) {
+			if (get_aia_sec_vector(avp_ptr, &s6_incoming_msgs.msg_data.aia_Q_msg_m) != SUCCESS)
+				res = S6A_FD_ERROR;
+		} else {
+			res = S6A_FD_ERROR;
+		}
+	}
+
+	/*Handled fd msg, do cleanup*/
+	fd_msg_free(*buf);
+
+	*buf = NULL;
+
+	if (DIAMETER_SUCCESS != res) s6_incoming_msgs.msg_data.aia_Q_msg_m.res = S6A_AIA_FAILED;
+
+	send_to_stage2(&s6_incoming_msgs);
+
+	return SUCCESS;
+}
+
+void
+handle_perf_hss_aia(int ue_idx, struct hss_aia_msg *aia)
+{
+    struct s6_incoming_msg_data_t msg;
+
+	msg.ue_idx = ue_idx;
+	msg.msg_type = auth_info_answer;
+	msg.msg_data.aia_Q_msg_m.res= 0;
+	memcpy(&(msg.msg_data.aia_Q_msg_m.sec.rand), &(aia->rand), sizeof(RAND));
+	memcpy(&(msg.msg_data.aia_Q_msg_m.sec.xres), &(aia->xres), sizeof(XRES));
+	memcpy(&(msg.msg_data.aia_Q_msg_m.sec.autn), &(aia->autn), sizeof(AUTN));
+	memcpy(&(msg.msg_data.aia_Q_msg_m.sec.kasme), &(aia->kasme), sizeof(KASME));
+
+	send_to_stage2(&msg);/*handle diameter error*/
+}
diff --git a/src/s6a/handlers/clr_handler.c b/src/s6a/handlers/clr_handler.c
new file mode 100644
index 0000000..b370375
--- /dev/null
+++ b/src/s6a/handlers/clr_handler.c
@@ -0,0 +1,147 @@
+/*
+ * 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 <unistd.h>
+#include <freeDiameter/freeDiameter-host.h>
+#include <freeDiameter/libfdcore.h>
+#include <freeDiameter/libfdproto.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "s6a_fd.h"
+#include "s6a.h"
+#include "msgType.h"
+//#include "detach_stage2_info.h"
+#include "hss_message.h"
+
+#define DIAMETER_SUCCESS 2001
+
+/** Global and externs**/
+extern struct fd_dict_objects g_fd_dict_objs;
+extern struct fd_dict_data g_fd_dict_data;
+extern int g_Q_mme_S6a_fd;
+/**global and externs end**/
+
+
+
+/**
+ * @brief callback handler for clr recvd from hss
+ * Parse clr, state and do cleanup for freediameter
+ * @params callback std
+ * @return error/success
+ */
+int
+clr_resp_callback(struct msg **buf, struct avp *avps, struct session *sess,
+			void *data, enum disp_action *action)
+{
+	struct msg *resp = NULL;
+	struct avp *avp_ptr = NULL;
+	struct s6_incoming_msg_data_t s6_incoming_msgs;
+	struct avp_hdr *avp_header = NULL;
+	unsigned int sess_id_len;
+	unsigned char *sess_id= NULL;
+
+	resp = *buf;
+
+	dump_fd_msg(resp);
+
+	/*read session id and extract ue index*/
+	CHECK_FCT_DO(fd_sess_getsid(sess, &sess_id, (size_t*)&sess_id_len),
+			return S6A_FD_ERROR);
+	log_msg(LOG_INFO, "\n CLR callback ----- >session id=%s \n",sess_id);
+    
+
+	/*AVP: Cancellation-Type*/
+	avp_ptr = NULL;
+	fd_msg_search_avp(resp, g_fd_dict_objs.cancellation_type, &avp_ptr);
+	if(NULL != avp_ptr) {
+		fd_msg_avp_hdr(avp_ptr, &avp_header);
+		s6_incoming_msgs.msg_data.clr_Q_msg_m.c_type = avp_header->avp_value->i32;
+	}
+	
+	fd_msg_search_avp(resp,g_fd_dict_objs.org_host, &avp_ptr);
+	if(NULL != avp_ptr) {
+		fd_msg_avp_hdr(avp_ptr, &avp_header);
+		memcpy(s6_incoming_msgs.msg_data.clr_Q_msg_m.origin_host,avp_header->avp_value->os.data,sizeof(s6_incoming_msgs.msg_data.clr_Q_msg_m.origin_host));
+		}
+
+	fd_msg_search_avp(resp, g_fd_dict_objs.org_realm, &avp_ptr);
+	if(NULL != avp_ptr) {
+		fd_msg_avp_hdr(avp_ptr, &avp_header);
+		memcpy(s6_incoming_msgs.msg_data.clr_Q_msg_m.origin_realm,avp_header->avp_value->os.data,sizeof(s6_incoming_msgs.msg_data.clr_Q_msg_m.origin_realm));
+		}
+         
+	fd_msg_search_avp(resp, g_fd_dict_objs.user_name,&avp_ptr);
+	if(NULL != avp_ptr) {
+		fd_msg_avp_hdr(avp_ptr, &avp_header);
+		memcpy(s6_incoming_msgs.msg_data.clr_Q_msg_m.imsi,avp_header->avp_value->os.data,sizeof(s6_incoming_msgs.msg_data.clr_Q_msg_m.imsi));
+		}
+       
+       /*CLA Processing*/
+
+       struct msg *ans, *qry;
+       struct avp * a;
+
+       if (buf == NULL)
+               return EINVAL;
+
+
+       /* Create answer header */
+       qry = *buf;
+       CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, buf, 0 ) );
+       ans = *buf;
+
+       /* Set the Origin-Host, Origin-Realm, Result-Code AVPs */
+       CHECK_FCT( fd_msg_rescode_set( ans, "DIAMETER_SUCCESS", NULL, NULL, 1 ) );
+
+       /* Send the answer */
+       CHECK_FCT( fd_msg_send( buf, NULL, NULL ) );
+
+	/*Do cleanup for freediameter*/
+	fd_msg_free(*buf);
+
+	*buf = NULL;
+	
+	s6_incoming_msgs.msg_type = cancel_location_request;
+
+	s6_incoming_msgs.destInstAddr = htonl(mmeAppInstanceNum_c);
+	s6_incoming_msgs.srcInstAddr = htonl(s6AppInstanceNum_c);
+
+	/*Send to stage2 queue*/
+        send_tipc_message(g_Q_mme_S6a_fd, mmeAppInstanceNum_c, (char*)&s6_incoming_msgs, S6_READ_MSG_BUF_SIZE);
+	
+	return SUCCESS;
+}
+
+/*Handler for CLR coming from built in perf HS*/
+void
+handle_perf_hss_clr(int ue_idx, struct hss_clr_msg *clr)
+{
+	struct s6_incoming_msg_data_t msg;
+    
+	msg.msg_type = cancel_location_request;
+	msg.ue_idx = ue_idx;
+	memcpy(&(msg.msg_data.clr_Q_msg_m.c_type), &(clr->cancellation_type), sizeof(clr->cancellation_type));
+	/*Send to stage2 queue*/
+	write_ipc_channel(g_Q_mme_S6a_fd, (char*)&msg, S6_READ_MSG_BUF_SIZE);
+}
diff --git a/src/s6a/handlers/detach_session_handler.c b/src/s6a/handlers/detach_session_handler.c
new file mode 100644
index 0000000..62f26f8
--- /dev/null
+++ b/src/s6a/handlers/detach_session_handler.c
@@ -0,0 +1,235 @@
+/*
+ * 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 <unistd.h>
+#include <stdbool.h>
+#include <freeDiameter/freeDiameter-host.h>
+#include <freeDiameter/libfdcore.h>
+#include <freeDiameter/libfdproto.h>
+
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "sec.h"
+#include "s6a_fd.h"
+#include "s6a.h"
+#include "s6a_config.h"
+#include "msgType.h"
+//#include "detach_stage1_info.h"
+
+/************************************************************************
+Current file : Stage  - AIR handler of S6A
+ATTACH stages :
+@@@	Stage 1 : IAM-->[stage1 handler]-->AIR, ULR
+	Stage 2 : AIA, ULA -->[stage2 handler]--> Auth req
+	Stage 3 : Auth resp-->[stage1 handler]-->Sec mode cmd
+	Stage 4 : sec mode resp-->[stage1 handler]-->esm infor req
+	Stage 5 : esm infor resp-->[stage1 handler]-->create session
+	Stage 6 : create session resp-->[stage1 handler]-->init ctx setup
+	Stage 7 : attach complete-->[stage1 handler]-->modify bearer
+**************************************************************************/
+
+/****Globals and externs ***/
+
+static int g_Q_detachread_fd;
+
+/*Making global just to avoid stack passing*/
+
+static char buf[S6A_PURGEREQ_STAGE1_BUF_SIZE];
+
+extern s6a_config g_s6a_cfg;
+extern struct fd_dict_objects g_fd_dict_objs;
+extern struct fd_dict_data g_fd_dict_data;
+/****Global and externs end***/
+
+/**
+Initialize the stage settings, Q,
+destination communication etc.
+*/
+static void
+init_stage()
+{
+	log_msg(LOG_INFO, "Waiting for session detach initialiser  from mme-app\n");
+	if ((g_Q_detachread_fd  = open_ipc_channel(S6A_DTCHREQ_STAGE1_QUEUE, IPC_READ)) == -1){
+		log_msg(LOG_ERROR, "Error in opening reader detach channel.\n");
+		pthread_exit(NULL);
+	}
+	return;
+}
+
+/**
+* Read next message from stage Q for processing.
+*/
+static int
+read_next_msg()
+{
+	int bytes_read=0;
+	memset(buf, 0, S6A_PURGEREQ_STAGE1_BUF_SIZE);
+	while (bytes_read < S6A_PURGEREQ_STAGE1_BUF_SIZE) {//TODO : Recheck condition
+		if ((bytes_read = read_ipc_channel(
+			g_Q_detachread_fd, buf, S6A_PURGEREQ_STAGE1_BUF_SIZE)) == -1) {
+			log_msg(LOG_ERROR, "Error in reading from AIR Q.\n");
+			/* TODO : Add proper error handling */
+		}
+		log_msg(LOG_INFO, "Purge msg received, len - %d\n", bytes_read);
+	}
+	return bytes_read;
+}
+
+/**
+ * @brief Prepare PUR freediameter message, dump and post to HSS
+ * @param[in] ue_idx UE indx to append to session id
+ * @param[in] imsi - IMSI
+ * @return int Sucess or failure code
+ */
+static int
+send_purge(int ue_idx, char imsi[])
+{
+	struct msg *fd_msg = NULL;
+	union avp_value val;
+	struct s6a_sess_info s6a_sess = {.sess_id="", .sess_id_len = 0};
+
+	if(SUCCESS != create_fd_sess_id(&s6a_sess, ue_idx)) return S6A_FD_ERROR;
+
+	CHECK_FCT_DO(fd_msg_new(g_fd_dict_objs.PUR, MSGFL_ALLOC_ETEID, &fd_msg),
+			return S6A_FD_ERROR);
+
+	/*AVP: Session-Id*/
+	val.os.data = (unsigned char*)s6a_sess.sess_id;
+	val.os.len = strlen(s6a_sess.sess_id);
+	add_fd_msg(&val, g_fd_dict_objs.sess_id, &fd_msg);
+
+	/*AVP: Auth-Session-State*/
+	val.i32 = 1; /*NO_STATE_MAINTAINED*/
+	val.os.len = 0;
+	add_fd_msg(&val, g_fd_dict_objs.auth_sess_state, &fd_msg);
+
+	/*AVP: Origin-Host/Realm*/
+	CHECK_FCT_DO(fd_msg_add_origin(fd_msg, 0), return S6A_FD_ERROR);
+
+	/*AVP: Destination-Host*/
+	val.os.data = (unsigned char *)g_s6a_cfg.hss_host_name;
+	val.os.len = strlen(g_s6a_cfg.hss_host_name);
+	add_fd_msg(&val, g_fd_dict_objs.dest_host, &fd_msg);
+
+	/*AVP: Destination-Realm*/
+	val.os.data = (unsigned char*)g_s6a_cfg.realm;
+	val.os.len = strlen(g_s6a_cfg.realm);
+	add_fd_msg(&val, g_fd_dict_objs.dest_realm, &fd_msg);
+
+	/*AVP: User-Name*/
+	val.os.data = (unsigned char*)imsi;
+	val.os.len = strlen(imsi);
+	add_fd_msg(&val, g_fd_dict_objs.user_name, &fd_msg);
+
+	/*AVP: PUR-Flags*/
+	val.u32 = true;
+	val.os.len = 0;
+	add_fd_msg(&val, g_fd_dict_objs.PUR_flags, &fd_msg);
+
+	dump_fd_msg(fd_msg);
+
+	/*Post message to hss*/
+	CHECK_FCT_DO(fd_msg_send(&fd_msg, NULL, NULL), return S6A_FD_ERROR);
+
+	return SUCCESS;
+}
+
+static void
+send_rpc_purge(int ue_idx, char imsi[])
+{
+	/* TODO: For builitn HSS, we are not sending purge request to HSS,
+	 * returning dummy reply. Send request to builtin HSS and
+	 * handle response.
+	 */
+	handle_perf_hss_purge_resp(ue_idx);
+	return;
+}
+
+/**
+* Stage specific message processing.
+*/
+static int
+detach_processing()
+{
+	struct s6a_purge_Q_msg *purge_msg = (struct s6a_purge_Q_msg*)buf;
+	char imsi[16] = {0};
+
+	/*Parse and validate  the buffer*/
+	imsi_bin_to_str(purge_msg->IMSI, imsi);
+	log_msg(LOG_INFO, "IMSI recvd - %s\n", imsi);
+
+	if (HSS_FD == g_s6a_cfg.hss_type)
+		send_purge(purge_msg->ue_idx, imsi);
+	else {
+		log_msg(LOG_INFO, "Sending over IPC \n");
+		send_rpc_purge(purge_msg->ue_idx, imsi);
+	}
+
+	return SUCCESS;
+}
+
+/**
+* Post message to next handler of the stage
+*/
+static int
+post_to_next()
+{
+	return SUCCESS;
+}
+
+/**
+* Thread exit function for future reference.
+*/
+void
+shutdown_detach()
+{
+	close_ipc_channel(g_Q_detachread_fd);
+	log_msg(LOG_INFO, "Shutdown detach handler \n");
+	pthread_exit(NULL);
+	return;
+}
+
+
+/**
+* Thread function for stage.
+*/
+void*
+detach_handler(void *data)
+{
+	init_stage();
+
+	sleep(5);
+
+	log_msg(LOG_INFO, "Detach Q handler ready.\n");
+
+	while(1){
+		read_next_msg();
+
+		detach_processing();
+
+		post_to_next();
+	}
+
+	return NULL;
+}
+
diff --git a/src/s6a/handlers/hss_message_delegator.c b/src/s6a/handlers/hss_message_delegator.c
new file mode 100644
index 0000000..ab7d9a0
--- /dev/null
+++ b/src/s6a/handlers/hss_message_delegator.c
@@ -0,0 +1,72 @@
+/*
+ * 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 "hss_message.h"
+#include "s6a.h"
+
+/**Global and externs **/
+
+/*Handle all messages coming from in built perf hss*/
+void
+hss_resp_handler(void *message)
+{
+	struct hss_resp_msg *msg = (struct hss_resp_msg*)message;
+
+	log_msg(LOG_INFO, "HSS response msg handler for ue_idx %d\n",
+			msg->ue_idx);
+
+	switch(msg->hdr){
+	case HSS_AIA_MSG:
+		handle_perf_hss_aia(msg->ue_idx,
+				(struct hss_aia_msg *)&(msg->data.aia));
+		break;
+
+	case HSS_ULA_MSG:
+		handle_perf_hss_ula(msg->ue_idx,
+				(struct hss_ula_msg *)&(msg->data.ula));
+		break;
+#if 0
+	case HSS_PURGE_RESP_MSG:
+		handle_perf_hss_purge_resp(msg->ue_idx);
+		break;
+#endif
+	//NI Detach
+	case HSS_CLR_MSG:
+		log_msg(LOG_INFO,"clr msg from TC\n");
+		handle_perf_hss_clr(msg->ue_idx,
+				(struct hss_clr_msg *)&(msg->data.clr));
+		break;
+
+	default:
+		log_msg(LOG_ERROR, "Unknown message received from HSS - %d\n",
+			msg->hdr);
+	}
+	return;
+
+	/*free allocated message buffer*/
+	free(message);
+}
diff --git a/src/s6a/handlers/purge_handler.c b/src/s6a/handlers/purge_handler.c
new file mode 100644
index 0000000..0ffe8e2
--- /dev/null
+++ b/src/s6a/handlers/purge_handler.c
@@ -0,0 +1,132 @@
+/*
+ * 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 <unistd.h>
+#include <freeDiameter/freeDiameter-host.h>
+#include <freeDiameter/libfdcore.h>
+#include <freeDiameter/libfdproto.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "s6a_fd.h"
+#include "s6a.h"
+#include "msgType.h"
+//#include "detach_stage2_info.h"
+#include "hss_message.h"
+
+/** Global and externs**/
+extern struct fd_dict_objects g_fd_dict_objs;
+extern struct fd_dict_data g_fd_dict_data;
+extern int g_Q_mme_S6a_fd;
+/**global and externs end**/
+
+static
+void send_to_stage2(struct s6_incoming_msg_data_t *incoming_msg_p)
+{
+	/*Send to stage2 queue*/
+	write_ipc_channel(g_Q_mme_S6a_fd, (char*)incoming_msg_p,
+			S6_READ_MSG_BUF_SIZE);
+}
+
+/**
+ * @brief callback handler for purge answer recvd from hss
+ * Parse purge answer, state and do cleanup for freediameter
+ * @params callback std
+ * @return error/success
+ */
+int
+purge_resp_callback(struct msg **buf, struct avp *avps, struct session *sess,
+			void *data, enum disp_action *action)
+{
+	struct msg *resp = NULL;
+	struct avp *avp_ptr = NULL;
+	struct s6_incoming_msg_data_t s6_incoming_msgs;
+	struct avp_hdr *avp_header = NULL;
+	unsigned int sess_id_len;
+	unsigned char *sess_id= NULL;
+
+	resp = *buf;
+
+	dump_fd_msg(resp);
+
+	/*read session id and extract ue index*/
+	CHECK_FCT_DO(fd_sess_getsid(sess, &sess_id, (size_t*)&sess_id_len),
+			return S6A_FD_ERROR);
+	log_msg(LOG_INFO, "\nPurge callback ----- >session id=%s \n",sess_id);
+    
+	s6_incoming_msgs.msg_type = purge_answser;
+	s6_incoming_msgs.ue_idx = get_ue_idx_from_fd_resp(sess_id, sess_id_len);
+
+	/*AVP: Result-Code*/
+	avp_ptr = NULL;
+	fd_msg_search_avp(resp, g_fd_dict_objs.res_code, &avp_ptr);
+
+	if(NULL != avp_ptr) {
+		fd_msg_avp_hdr(avp_ptr, &avp_header);
+		s6_incoming_msgs.msg_data.purge_resp_Q_msg_m.status = avp_header->avp_value->u32;
+
+		if (SUCCESS != s6_incoming_msgs.msg_data.purge_resp_Q_msg_m.status) {
+			s6_incoming_msgs.msg_data.purge_resp_Q_msg_m.status = S6A_FD_ERROR;
+		}
+	} else {
+		struct fd_result res;
+		avp_ptr = NULL;
+
+		fd_msg_search_avp(resp, g_fd_dict_objs.exp_res,
+			&avp_ptr);
+
+		if (NULL != avp_ptr) {
+			s6_incoming_msgs.msg_data.purge_resp_Q_msg_m.status = S6A_FD_ERROR;
+		}
+
+		if (parse_fd_result(avp_ptr, &res) != SUCCESS) {
+			s6_incoming_msgs.msg_data.purge_resp_Q_msg_m.status = S6A_FD_ERROR;
+		}
+		s6_incoming_msgs.msg_data.purge_resp_Q_msg_m.status =res.result_code;
+	}
+
+	/*Inform response to mme-app*/
+	send_to_stage2(&s6_incoming_msgs);
+
+	/*Do cleanup for freediameter*/
+	fd_msg_free(*buf);
+
+	*buf = NULL;
+
+	return SUCCESS;
+}
+
+/*Handler for AIA coming from built in perf HS*/
+void
+handle_perf_hss_purge_resp(int ue_idx)
+{
+	struct s6_incoming_msg_data_t resp;
+    
+	resp.msg_type = purge_answser;
+	resp.ue_idx = ue_idx;
+	resp.msg_data.purge_resp_Q_msg_m.status = 0;
+
+	send_to_stage2(&resp);
+}
diff --git a/src/s6a/handlers/s6a_req_handler.c b/src/s6a/handlers/s6a_req_handler.c
new file mode 100644
index 0000000..8bc361b
--- /dev/null
+++ b/src/s6a/handlers/s6a_req_handler.c
@@ -0,0 +1,346 @@
+/*
+ * 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 <unistd.h>
+#include <stdbool.h>
+#include <freeDiameter/freeDiameter-host.h>
+#include <freeDiameter/libfdcore.h>
+#include <freeDiameter/libfdproto.h>
+
+#include "s6a_config.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "sec.h"
+#include "msgType.h"
+//#include "stage1_s6a_msg.h"
+#include "s6a_fd.h"
+#include "s6a.h"
+#include "s6a_config.h"
+#include "hss_message.h"
+
+/************************************************************************
+Current file : Stage  - AIR handler of S6A
+ATTACH stages :
+@@@	Stage 1 : IAM-->[stage1 handler]-->AIR, ULR
+	Stage 2 : AIA, ULA -->[stage2 handler]--> Auth req
+	Stage 3 : Auth resp-->[stage1 handler]-->Sec mode cmd
+	Stage 4 : sec mode resp-->[stage1 handler]-->esm infor req
+	Stage 5 : esm infor resp-->[stage1 handler]-->create session
+	Stage 6 : create session resp-->[stage1 handler]-->init ctx setup
+	Stage 7 : attach complete-->[stage1 handler]-->modify bearer
+**************************************************************************/
+
+/****Globals and externs ***/
+extern int g_our_hss_fd;
+extern s6a_config  g_s6a_cfg;
+extern struct fd_dict_objects g_fd_dict_objs;
+extern struct fd_dict_data g_fd_dict_data;
+/****Global and externs end***/
+
+
+void ulr_intermediate_cb(void * data, struct msg ** msg)
+{
+	dump_fd_msg(*msg);
+}
+
+/**
+ * @brief Prepare ULR freediameter message, dump and post to HSS
+ * @param[in] aia_msg - AIA info recvd from mme-app
+ * @param[in] imsi - IMSI
+ * @return int Sucess or failure code
+ */
+static int
+send_FD_ULR(struct s6a_Q_msg *aia_msg, char imsi[])
+{
+	struct msg *fd_msg = NULL;
+	int res = SUCCESS;
+	struct s6a_sess_info s6a_sess = {.sess_id="", .sess_id_len = 0};
+	char event = 12;
+	union avp_value val;
+
+	/*Create FD header and message for update location request.*/
+	if(SUCCESS != (res = create_fd_sess_id(&s6a_sess, aia_msg->ue_idx)))
+		return res;
+
+	CHECK_FCT_DO(fd_msg_new(g_fd_dict_objs.ULR, MSGFL_ALLOC_ETEID, &fd_msg),
+			return S6A_FD_ERROR)
+
+	/*AVP: Session-Id*/
+	val.os.data = (unsigned char*)s6a_sess.sess_id;
+	val.os.len = strlen(s6a_sess.sess_id);
+	add_fd_msg(&val, g_fd_dict_objs.sess_id, &fd_msg);
+
+	/*AVP: Auth-Session-State*/
+	val.i32 = 1; /*NO_STATE_MAINTAINED*/
+	val.os.len = 0;
+	add_fd_msg(&val, g_fd_dict_objs.auth_sess_state, &fd_msg);
+
+	/*AVP: Origin-Host/Realm*/
+	CHECK_FCT_DO(fd_msg_add_origin(fd_msg, 0), return S6A_FD_ERROR);
+
+	/*AVP: Destination-Host*/
+	val.os.data = (unsigned char*)g_s6a_cfg.hss_host_name;
+	val.os.len = strlen(g_s6a_cfg.hss_host_name);
+	add_fd_msg(&val, g_fd_dict_objs.dest_host, &fd_msg);
+
+	/*AVP: Destination-Realm*/
+	val.os.data = (unsigned char*)g_s6a_cfg.realm;
+	val.os.len = strlen(g_s6a_cfg.realm);
+	add_fd_msg(&val, g_fd_dict_objs.dest_realm, &fd_msg);
+
+	/*AVP: User-Name*/
+	val.os.data = (unsigned char*)imsi;
+	val.os.len = strlen(imsi);
+	add_fd_msg(&val, g_fd_dict_objs.user_name, &fd_msg);
+
+	/*AVP: RAT-Type*/
+	val.u32 = S6A_RAT_EUTRAN;
+	val.os.len = 0;
+	add_fd_msg(&val, g_fd_dict_objs.RAT_type, &fd_msg);
+
+	/*AVP: ULR-Flags*/
+	val.u32 = ULR_FLAG_S6AS6D_IND | ULR_FLAG_INIT_ATCH_IND ;
+	add_fd_msg(&val, g_fd_dict_objs.ULR_flags, &fd_msg);
+
+	/*AVP: Visited-PLMN-Id*/
+	val.os.data = (unsigned char*)aia_msg->tai.plmn_id.idx;
+	val.os.len = 3;
+	add_fd_msg(&val, g_fd_dict_objs.visited_PLMN_id, &fd_msg);
+
+	dump_fd_msg(fd_msg);
+
+	/*Post ULR to HSS*/
+	CHECK_FCT_DO(fd_msg_send(&fd_msg, ulr_intermediate_cb, (void*)&event),
+			return S6A_FD_ERROR);
+
+	return SUCCESS;
+}
+
+
+/**
+ * @brief dump recvd aia queue msg
+ * @param[in] air_msg - IMSI
+ * @return void
+ */
+void
+dump_s6a_msg(struct s6a_Q_msg *air_msg)
+{
+	log_msg(LOG_INFO, "Received index= %d\n",air_msg->ue_idx);
+	log_msg(LOG_INFO, "Received plmn %x %x %x= %d\n",air_msg->tai.plmn_id.idx[0],
+			air_msg->tai.plmn_id.idx[1], air_msg->tai.plmn_id.idx[2]);
+}
+
+/**
+ * @brief Prepare AIR freediameter message, dump and post to HSS
+ * @param[in] aia_msg - AIA info recvd from mme-app
+ * @param[in] imsi - IMSI
+ * @return int Sucess or failure code
+ */
+static int
+send_FD_AIR(struct s6a_Q_msg *aia_msg, char imsi[])
+{
+	struct avp *avp_ptr = NULL;
+	struct msg *fd_msg = NULL;
+	int res = 0;
+	struct s6a_sess_info s6a_sess = {.sess_id="", .sess_id_len = 0};
+	union avp_value val;
+
+	log_msg(LOG_INFO, "In Send AIR:\n");
+	dump_s6a_msg(aia_msg);
+
+	/*Create FD header and message for authentication info request.*/
+	if(SUCCESS != (res = create_fd_sess_id(&s6a_sess, aia_msg->ue_idx)))
+		return res;
+
+	CHECK_FCT_DO(fd_msg_new(g_fd_dict_objs.AIR, MSGFL_ALLOC_ETEID, &fd_msg),
+			return S6A_FD_ERROR);
+
+	/*AVP: Session-Id*/
+	val.os.data = (unsigned char*)s6a_sess.sess_id;
+	val.os.len = strlen(s6a_sess.sess_id);
+	add_fd_msg(&val, g_fd_dict_objs.sess_id, &fd_msg);
+
+	/*AVP: Auth-Session-State*/
+	val.i32 = 1; /*NO_STATE_MAINTAINED*/
+	val.os.len = 0;
+	add_fd_msg(&val, g_fd_dict_objs.auth_sess_state, &fd_msg);
+
+	/*AVP: Origin-Host/Realm*/
+	CHECK_FCT_DO(fd_msg_add_origin(fd_msg, 0), return S6A_FD_ERROR)
+
+	/*AVP: Destination-Host*/
+	val.os.data = (unsigned char*)g_s6a_cfg.hss_host_name;
+	val.os.len = strlen(g_s6a_cfg.hss_host_name);
+	add_fd_msg(&val, g_fd_dict_objs.dest_host, &fd_msg);
+
+	/*AVP: Destination-Realm*/
+	val.os.data = (unsigned char*)g_s6a_cfg.realm;
+	val.os.len = strlen(g_s6a_cfg.realm);
+	add_fd_msg(&val, g_fd_dict_objs.dest_realm, &fd_msg);
+
+	/*AVP: User-Name*/
+	val.os.data = (unsigned char*)imsi;
+	val.os.len = strlen(imsi);
+	add_fd_msg(&val, g_fd_dict_objs.user_name, &fd_msg);
+
+	/*AVP: Visited-PLMN-Id*/
+	val.os.data = (unsigned char*)aia_msg->tai.plmn_id.idx;
+	val.os.len = 3;
+	add_fd_msg(&val, g_fd_dict_objs.visited_PLMN_id, &fd_msg);
+
+	/*TODO: recheck. Can it be grouped better way*/
+	/*AVP: Requested-EUTRAN-Authentication-Info*/
+	CHECK_FCT_DO(fd_msg_avp_new(
+		g_fd_dict_objs.req_EUTRAN_auth_info, 0, &avp_ptr),
+		return -1);
+	CHECK_FCT_DO(fd_msg_avp_add(fd_msg, MSG_BRW_LAST_CHILD, avp_ptr), return -1);
+
+	/*AVP: Requested-EUTRAN-Authentication-Info
+	 *----->AVP: Number-Of-Requested-Vectors(1410)*/
+	val.i32 = 1;
+	val.os.len = 0;
+	add_fd_msg(&val, g_fd_dict_objs.no_of_req_vectors,
+			(struct msg**)&avp_ptr);
+
+	/*AVP: Requested-EUTRAN-Authentication-Info
+	 * ------>AVP: Immediate-Response-Preferred*/
+	val.u32 = 0;
+	add_fd_msg(&val, g_fd_dict_objs.immediate_resp_pref,
+			(struct msg**)&avp_ptr);
+
+	dump_fd_msg(fd_msg);
+
+	/*post AIR to hss */
+	CHECK_FCT_DO(fd_msg_send(&fd_msg, NULL, NULL), return S6A_FD_ERROR);
+
+	return SUCCESS;
+}
+
+/**
+API to send AIR oto customer HSS over custome RPC
+*/
+static void
+send_rpc_AIR(struct s6a_Q_msg *air_msg, char imsi[])
+{
+	struct hss_req_msg msg;
+
+	msg.hdr = HSS_AIR_MSG;
+	msg.ue_idx = air_msg->ue_idx;
+
+	strncpy(msg.data.air.imsi, imsi, IMSI_STR_LEN);
+	memcpy(msg.data.air.plmn_id, air_msg->tai.plmn_id.idx, 3);
+
+	if (write(g_our_hss_fd, &msg, HSS_REQ_MSG_SIZE) < 0) {
+		log_msg(LOG_ERROR, "HSS AIR msg send failed.\n");
+		 		perror("writing on stream socket");
+	}
+	log_msg(LOG_INFO, "AIR msg send to hss for ue_idx %d\n",
+		air_msg->ue_idx);
+}
+
+/**
+API to send ULR oto customer HSS over custome RPC
+*/
+static void
+send_rpc_ULR(struct s6a_Q_msg *ulr_msg, char imsi[])
+{
+	struct hss_req_msg msg;
+
+	msg.hdr = HSS_ULR_MSG;
+	msg.ue_idx = ulr_msg->ue_idx;
+
+	strncpy(msg.data.air.imsi, imsi, IMSI_STR_LEN);
+	memcpy(msg.data.air.plmn_id, ulr_msg->tai.plmn_id.idx, 3);
+
+	if (write(g_our_hss_fd, &msg, HSS_REQ_MSG_SIZE) < 0) {
+		log_msg(LOG_ERROR, "HSS ULR msg send failed.\n");
+		perror("writing on stream socket");
+	}
+	log_msg(LOG_INFO, "ULR msg send to hss\n");
+}
+
+
+/**
+ * @brief convert binary imsi to string imsi
+ * Binary imsi is stored in 8 bytes, each nibble representing each imsi char.
+ * char imsi stroes each char in 1 byte.
+ * @param[in] b_imsi : Binary imsi
+ * @param[out] s_imsi : Converted string imsi
+ * @return void
+ */
+void
+imsi_bin_to_str(unsigned char *b_imsi, char *s_imsi)
+{
+	if(NULL == b_imsi || NULL == s_imsi) return;
+
+	memset(s_imsi, 0, STR_IMSI_LEN);
+
+	/* Byte 'AB' in b_imsi, is converted to two bytes 'A', 'B' in s_imsi*/
+	s_imsi[0] = '0' + ((b_imsi[0]>>4) & 0x0F);
+
+	for(int i=1; i < BINARY_IMSI_LEN; ++i) {
+		s_imsi[(i*2)-1] = '0' + (b_imsi[i] & 0x0F);
+		s_imsi[(i*2)] = '0' + ((b_imsi[i]>>4) & 0x0F);
+	}
+	s_imsi[(BINARY_IMSI_LEN*2)-1] = '\0';
+}
+
+/**
+ * @brief Post AIR and ULR messsages simultaneously to HSS.
+ * @return int Sucess or failure code
+ */
+static int
+AIR_processing(struct s6a_Q_msg * air_msg)
+{
+	log_msg(LOG_INFO, "IMSI recvd - %s\n %d \n", air_msg->imsi, air_msg->msg_type);
+
+	if(HSS_FD == g_s6a_cfg.hss_type) {
+		if(air_msg->msg_type == auth_info_request)
+			/*post to next processing*/
+			send_FD_AIR(air_msg, air_msg->imsi);
+		else if(air_msg->msg_type == update_loc_request)
+			send_FD_ULR(air_msg, air_msg->imsi);
+
+	} else {
+		log_msg(LOG_INFO, "Sending over IPC\n");
+		send_rpc_AIR(air_msg, air_msg->imsi);
+		send_rpc_ULR(air_msg, air_msg->imsi);
+	}
+
+	return SUCCESS;
+}
+
+/*
+* Thread function for stage.
+*/
+void*
+S6Req_handler(void *data)
+{
+	log_msg(LOG_INFO, "AIR Q handler ready.\n");
+
+	char *msg = ((char *) data) + ((sizeof(uint32_t)) * 2);
+	
+	AIR_processing((struct s6a_Q_msg *)msg);
+	
+	return NULL;
+}
diff --git a/src/s6a/handlers/ula_handler.c b/src/s6a/handlers/ula_handler.c
new file mode 100644
index 0000000..e9a49b8
--- /dev/null
+++ b/src/s6a/handlers/ula_handler.c
@@ -0,0 +1,240 @@
+/*
+ * 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 <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <ctype.h>
+#include <freeDiameter/freeDiameter-host.h>
+#include <freeDiameter/libfdproto.h>
+#include <freeDiameter/libfdcore.h>
+
+#include "log.h"
+#include "err_codes.h"
+#include "message_queues.h"
+#include "ipc_api.h"
+#include "s6a_fd.h"
+#include "s6a.h"
+#include "msgType.h"
+//#include "stage1_s6a_msg.h"
+#include "hss_message.h"
+
+extern int g_Q_mme_S6a_fd;
+extern struct fd_dict_objects g_fd_dict_objs;
+extern struct fd_dict_data g_fd_dict_data;
+
+/**
+ * @brief Parse subscription information for UE
+ * @param[in] avp_ptr - POinter to subscription data avp
+ * @param[out] ula information filled with parsed data
+ * @return void
+ */
+static void
+parse_ula_subscription_data(struct avp *avp_ptr, struct ula_Q_msg *ula)
+{
+	struct avp *next = NULL;
+	struct avp_hdr *element = NULL;
+
+    CHECK_FCT_DO(fd_msg_avp_hdr(avp_ptr, &element),
+                        return);
+
+    if ((NULL == element) ||
+           (element->avp_code != g_fd_dict_data.subscription_data.avp_code))
+        return;
+
+    CHECK_FCT_DO(fd_msg_browse(avp_ptr, MSG_BRW_FIRST_CHILD, &next, NULL),
+                    return);
+
+	for(;
+		NULL != next;
+		fd_msg_browse(next, MSG_BRW_NEXT, &next, NULL)) {
+
+		fd_msg_avp_hdr (next, &element);
+
+		if(NULL == element) return;
+
+		/*AVP: Access-Restriction-Data(1426)*/
+		if(g_fd_dict_data.access_restriction_data.avp_code ==
+				element->avp_code) {
+			ula->access_restriction_data = element->avp_value->u32;
+			continue;
+		}
+
+		/*AVP: Subscriber-Status(1424)*/
+		if(g_fd_dict_data.subscriber_status.avp_code == element->avp_code) {
+			ula->subscription_status = element->avp_value->i32;
+			continue;
+		}
+
+		/*AVP: Network-Access-Mode(1417)*/
+		if(g_fd_dict_data.net_access_mode.avp_code == element->avp_code) {
+			ula->net_access_mode = element->avp_value->i32;
+			continue;
+		}
+
+		/*AVP: Regional-Subscription-Zone-Code(1446)*/
+		if(g_fd_dict_data.reg_subs_zone_code.avp_code ==
+				element->avp_code) {
+			//element->avp_value : 10 string values of len 4
+			continue;
+		}
+
+		/*AVP: MSISDN(701)*/
+		if(g_fd_dict_data.MSISDN.avp_code == element->avp_code) {
+			memcpy(ula->MSISDN, element->avp_value->os.data, element->avp_value->os.len);
+			continue;
+		}
+
+		/*AVP: AMBR(1435)*/
+			/*AVP: Max-Requested-Bandwidth-UL(516)
+			  AVP: Max-Requested-Bandwidth-DL(515*/
+		if(g_fd_dict_data.AMBR.avp_code == element->avp_code) {
+			/*AMBR has its own child elements, iterate through those*/
+			struct avp *ambr_itr = NULL;
+			struct avp_hdr *ambr_element = NULL;
+
+			CHECK_FCT_DO(fd_msg_browse(next, MSG_BRW_FIRST_CHILD,
+						&ambr_itr, NULL), return);
+
+			/*Iterate through subscription data child avps*/
+			while(NULL != ambr_itr) {
+				fd_msg_avp_hdr(ambr_itr, &ambr_element);
+
+				if(g_fd_dict_data.max_req_bandwidth_UL.avp_code ==
+						ambr_element->avp_code) {
+					ula->max_requested_bw_ul = ambr_element->avp_value->u32;
+				}
+
+				if(g_fd_dict_data.max_req_bandwidth_DL.avp_code ==
+						ambr_element->avp_code) {
+					ula->max_requested_bw_dl = ambr_element->avp_value->u32;
+				}
+
+				CHECK_FCT_DO(fd_msg_browse(ambr_itr, MSG_BRW_NEXT,
+						&ambr_itr, NULL), return);
+			}
+			continue;
+		}
+
+		/*AVP: APN-Configuration-Profile(1429)*/
+			/*AVP: Context-Identifier(1423)
+			AVP: All-APN-Configurations-Included-Indicator(1428)
+			AVP: APN-Configuration(1430)*/
+		if(g_fd_dict_data.APN_config_profile.avp_code == element->avp_code) {
+			/*APN profile has its own child elements, iterate through
+			 * those*/
+			struct avp *apn_cfg_prof_itr = NULL;
+			struct avp_hdr *apn_cfg_element = NULL;
+
+			CHECK_FCT_DO(fd_msg_browse(next, MSG_BRW_FIRST_CHILD,
+						&apn_cfg_prof_itr, NULL), return);
+
+			/*Iterate through subscription data child avps*/
+			while(NULL != apn_cfg_prof_itr) {
+				fd_msg_avp_hdr(apn_cfg_prof_itr, &apn_cfg_element);
+
+				if(g_fd_dict_data.ctx_id.avp_code ==
+						apn_cfg_element->avp_code) {
+					ula->apn_config_profile_ctx_id =
+						apn_cfg_element->avp_value->u32;
+				} else
+				if(g_fd_dict_data.all_APN_configs_included_ind.avp_code ==
+						apn_cfg_element->avp_code) {
+					ula->all_APN_cfg_included_ind =
+						apn_cfg_element->avp_value->i32;
+				} else
+				if(g_fd_dict_data.APN_config.avp_code ==
+						apn_cfg_element->avp_code){
+					//APN configuration list : There is list of elements to read
+					//TODO : Write function to read all elements
+
+				}
+
+				CHECK_FCT_DO(fd_msg_browse(apn_cfg_prof_itr, MSG_BRW_NEXT,
+						&apn_cfg_prof_itr, NULL), return);
+			}
+			continue;
+		}
+
+		/*AVP: Subscribed-Periodic-RAU-TAU-Timer(1619)*/
+		if(g_fd_dict_data.subsc_periodic_RAU_TAU_tmr.avp_code
+				== element->avp_code) {
+			ula->RAU_TAU_timer = element->avp_value->u32;
+			continue;
+		}
+
+	}
+}
+
+/**
+ * @brief Call back registered to handle ULA from hss
+ * @param callback requiremd arguments
+ * @return int error code as success or failure
+ */
+int
+ula_resp_callback(struct msg **buf, struct avp *avp_ptr, struct session *sess,
+			void *data, enum disp_action *action)
+{
+	int sess_id_len, ue_idx;
+	unsigned char *sess_id= NULL;
+	struct s6_incoming_msg_data_t s6_incoming_msgs;
+	struct avp *subsc_ptr = NULL;
+
+	CHECK_FCT_DO(fd_sess_getsid(sess, &sess_id, (size_t*)&sess_id_len),
+		return S6A_FD_ERROR);
+
+	log_msg(LOG_INFO, "\nCallback ----- >session id=%s \n", sess_id);
+
+	s6_incoming_msgs.msg_data.ula_Q_msg_m.res = SUCCESS;
+	ue_idx = get_ue_idx_from_fd_resp(sess_id, sess_id_len);
+
+	/*AVP: Subscription-Data(1400)*/
+	fd_msg_search_avp(*buf, g_fd_dict_objs.subscription_data, &subsc_ptr);
+
+	/*Parse fd message and extract ula information*/
+	if(NULL != subsc_ptr) parse_ula_subscription_data(subsc_ptr, &s6_incoming_msgs.msg_data.ula_Q_msg_m);
+
+	fd_msg_free(*buf);
+	*buf = NULL;
+    
+	s6_incoming_msgs.msg_type = update_loc_answer;
+	s6_incoming_msgs.ue_idx = ue_idx;
+
+	s6_incoming_msgs.destInstAddr = htonl(mmeAppInstanceNum_c);
+	s6_incoming_msgs.srcInstAddr = htonl(s6AppInstanceNum_c);
+
+	/*Send to stage2 queue*/
+	send_tipc_message(g_Q_mme_S6a_fd, mmeAppInstanceNum_c, (char*)&s6_incoming_msgs, S6_READ_MSG_BUF_SIZE);
+
+	return SUCCESS;
+}
+
+/*Handler for ULA coming from built in perf HS*/
+void
+handle_perf_hss_ula(int ue_idx, struct hss_ula_msg *ula)
+{
+	struct s6_incoming_msg_data_t msg;
+    
+	msg.msg_type = update_loc_answer;
+	msg.ue_idx = ue_idx;
+	msg.msg_data.ula_Q_msg_m.res = ula->subscription_state;
+	/*Send to stage2 queue*/
+	write_ipc_channel(g_Q_mme_S6a_fd, (char*)&msg, S6_READ_MSG_BUF_SIZE);
+}