blob: 6ecd11c5b6fe991cc07dab5045480bbf1675443d [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 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <pthread.h>
21#include <string.h>
22#include <unistd.h>
23#include <stdint.h>
24
25#include "log.h"
26#include "err_codes.h"
27#include "message_queues.h"
28#include "ipc_api.h"
29#include "s1ap.h"
30#include "s1ap_config.h"
31#include "main.h"
32#include "msgType.h"
33
34static int
35get_ni_detach_request_protoie_value(struct proto_IE *value, struct ni_detach_request_Q_msg *g_acptReqInfo)
36{
37 uint8_t ieCnt = 0;
38
39 value->no_of_IEs = NI_DTCH_REQUEST_NO_OF_IES;
40
41 value->data = (proto_IEs *) malloc(NI_DTCH_REQUEST_NO_OF_IES *
42 sizeof(proto_IEs));
43
44 value->data[ieCnt].val.mme_ue_s1ap_id = g_acptReqInfo->ue_idx;
45 ieCnt++;
46
47 value->data[ieCnt].val.enb_ue_s1ap_id = g_acptReqInfo->enb_s1ap_ue_id;
48 ieCnt++;
49
50 struct nasPDU *nas = &(value->data[ieCnt].val.nas);
51 nas->header.security_header_type = IntegrityProtectedCiphered;
52 nas->header.proto_discriminator = EPSMobilityManagementMessages;
53
54 /* placeholder for mac. mac value will be calculated later */
55 uint8_t mac[MAC_SIZE] = {0};
56 memcpy(nas->header.mac, mac, MAC_SIZE);
57
58 nas->header.seq_no = g_acptReqInfo->dl_seq_no;
59 nas->header.message_type = DetachRequest;
60 nas->header.detach_type = 00000002;
61 log_msg(LOG_DEBUG,"NAS Msg Type: %x\n",nas->header.message_type);
62
63 ieCnt++;
64
65 return SUCCESS;
66}
67
68/**
69* Stage specific message processing.
70*/
71static int
72ni_detach_request_processing(struct ni_detach_request_Q_msg *g_acptReqInfo)
73{
74 unsigned char tmpStr[4];
75 struct s1ap_PDU s1apPDU= {0};
76 uint16_t protocolIe_Id;
77 uint8_t protocolIe_criticality = CRITICALITY_REJECT;
78 uint8_t initiating_msg = 0;
79 uint8_t datalen = 0;
80 uint8_t s1ap_len_pos;
81 uint8_t nas_len_pos;
82 uint8_t u8value = 0;
83 uint8_t mac_data_pos;
84
85 Buffer g_acpt_buffer;
86
87 s1apPDU.procedurecode = id_downlinkNASTransport;
88 s1apPDU.criticality = CRITICALITY_IGNORE;
89
90 get_ni_detach_request_protoie_value(&s1apPDU.value, g_acptReqInfo);
91
92 g_acpt_buffer.pos = 0;
93
94 buffer_copy(&g_acpt_buffer, &initiating_msg,
95 sizeof(initiating_msg));
96
97 buffer_copy(&g_acpt_buffer, &s1apPDU.procedurecode,
98 sizeof(s1apPDU.procedurecode));
99
100 buffer_copy(&g_acpt_buffer, &s1apPDU.criticality,
101 sizeof(s1apPDU.criticality));
102
103 s1ap_len_pos = g_acpt_buffer.pos;
104
105 /* length of NI Detach Request */
106 u8value = 0;
107 buffer_copy(&g_acpt_buffer, &u8value, sizeof(u8value));
108
109 /* TODO remove hardcoded values */
110 uint8_t chProtoIENo[3] = {0,0,3};
111 buffer_copy(&g_acpt_buffer, chProtoIENo, 3);
112
113 /* id-MME-UE-S1AP-ID */
114 protocolIe_Id = id_MME_UE_S1AP_ID;
115 copyU16(tmpStr, protocolIe_Id);
116 buffer_copy(&g_acpt_buffer, tmpStr, sizeof(protocolIe_Id));
117 buffer_copy(&g_acpt_buffer, &protocolIe_criticality,
118 sizeof(protocolIe_criticality));
119 datalen = 2;
120 /* TODO need to add proper handling*/
121 unsigned char mme_ue_id[3];
122 datalen = copyU16(mme_ue_id, s1apPDU.value.data[0].val.mme_ue_s1ap_id);
123 buffer_copy(&g_acpt_buffer, &datalen, sizeof(datalen));
124 buffer_copy(&g_acpt_buffer, mme_ue_id, datalen);
125
126 /* id-eNB-UE-S1AP-ID */
127 protocolIe_Id = id_eNB_UE_S1AP_ID;
128 copyU16(tmpStr, protocolIe_Id);
129 buffer_copy(&g_acpt_buffer, tmpStr, sizeof(protocolIe_Id));
130 buffer_copy(&g_acpt_buffer, &protocolIe_criticality,
131 sizeof(protocolIe_criticality));
132 /* TODO needs proper handling*/
133 unsigned char enb_ue_id[3];
134 datalen = copyU16(enb_ue_id, s1apPDU.value.data[1].val.enb_ue_s1ap_id);
135 buffer_copy(&g_acpt_buffer, &datalen, sizeof(datalen));
136 buffer_copy(&g_acpt_buffer, enb_ue_id, datalen);
137
138 /* NAS PDU start */
139 protocolIe_Id = id_NAS_PDU;
140 copyU16(tmpStr, protocolIe_Id);
141 buffer_copy(&g_acpt_buffer, tmpStr, sizeof(protocolIe_Id));
142 buffer_copy(&g_acpt_buffer, &protocolIe_criticality,
143 sizeof(protocolIe_criticality));
144
145 nas_len_pos = g_acpt_buffer.pos;
146
147 datalen = 0;
148 buffer_copy(&g_acpt_buffer, &datalen, sizeof(datalen));
149
150 buffer_copy(&g_acpt_buffer, &datalen, sizeof(datalen));
151
152 nas_pdu_header *nas_hdr = &(s1apPDU.value.data[2].val.nas.header);
153
154 /* security header and protocol discriminator */
155 u8value = (nas_hdr->security_header_type << 4 |
156 nas_hdr->proto_discriminator);
157 buffer_copy(&g_acpt_buffer, &u8value, sizeof(u8value));
158
159 /* mac */
160 /* placeholder for mac. mac value will be calculated later */
161 buffer_copy(&g_acpt_buffer, nas_hdr->mac, MAC_SIZE);
162 mac_data_pos = g_acpt_buffer.pos;
163
164 /* sequence number */
165 buffer_copy(&g_acpt_buffer, &(nas_hdr->seq_no),
166 sizeof(nas_hdr->seq_no));
167
168 /* security header and protocol discriminator */
169 nas_hdr->security_header_type = Plain;
170 u8value = (nas_hdr->security_header_type << 4 |
171 nas_hdr->proto_discriminator);
172 buffer_copy(&g_acpt_buffer, &u8value, sizeof(u8value));
173
174 /* message type */
175 buffer_copy(&g_acpt_buffer, &(nas_hdr->message_type),
176 sizeof(nas_hdr->message_type));
177 /* detach type */
178 buffer_copy(&g_acpt_buffer, &(nas_hdr->detach_type),
179 sizeof(nas_hdr->detach_type));
180
181 /* NAS PDU end */
182
183 /* Calculate mac */
184 uint8_t direction = 1;
185 uint8_t bearer = 0;
186
187 calculate_mac(g_acptReqInfo->int_key, nas_hdr->seq_no,
188 direction, bearer, &g_acpt_buffer.buf[mac_data_pos],
189 g_acpt_buffer.pos - mac_data_pos,
190 &g_acpt_buffer.buf[mac_data_pos - MAC_SIZE]);
191
192 /* Copy nas length to nas length field */
193 datalen = g_acpt_buffer.pos - nas_len_pos -1;
194 memcpy(&(g_acpt_buffer.buf[nas_len_pos]), &datalen, sizeof(datalen));
195
196 /* Copy nas length to nas length field */
197 datalen = g_acpt_buffer.pos - nas_len_pos - 2;
198 memcpy(&(g_acpt_buffer.buf[nas_len_pos + 1]), &datalen, sizeof(datalen));
199
200 /* Copy length to s1ap length field */
201 datalen = g_acpt_buffer.pos - s1ap_len_pos - 1;
202 memcpy(g_acpt_buffer.buf + s1ap_len_pos, &datalen, sizeof(datalen));
203
204 /* TODO: temp fix */
205 //g_ics_buffer.buf[1] = 0x09;
206 send_sctp_msg(g_acptReqInfo->enb_fd, g_acpt_buffer.buf, g_acpt_buffer.pos,1);
207
208 log_msg(LOG_INFO, "NI Detach Request sent to UE.");
209
210 return SUCCESS;
211}
212
213
214
215/**
216* Thread function for stage.
217*/
218void*
219ni_detach_request_handler(void *data)
220{
221 log_msg(LOG_INFO, "NI detach request handler ready.\n");
222 struct ni_detach_request_Q_msg *msg = (struct ni_detach_request_Q_msg *)data;
223
224 ni_detach_request_processing(msg);
225
226 return NULL;
227}
228