anjana_sreekumar@infosys.com | 991c206 | 2020-01-08 11:42:57 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd. |
| 3 | * Copyright (c) 2017 Intel Corporation |
| 4 | * Copyright (c) 2019, Infosys Ltd. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <pthread.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "log.h" |
| 26 | #include "err_codes.h" |
| 27 | #include "s1ap.h" |
| 28 | #include "message_queues.h" |
| 29 | #include "ipc_api.h" |
| 30 | #include "main.h" |
| 31 | #include "sctp_conn.h" |
| 32 | #include "msgType.h" |
| 33 | |
| 34 | void |
| 35 | buffer_copy(struct Buffer *buffer, void *value, size_t size) |
| 36 | { |
| 37 | memcpy(buffer->buf + buffer->pos , value, size); |
| 38 | buffer->pos += size; |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get ProtocolIE value for Auth Request sent by mme-app |
| 44 | */ |
| 45 | static int |
| 46 | get_authreq_protoie_value(struct proto_IE *value, struct authreq_info *g_authreqInfo) |
| 47 | { |
| 48 | value->no_of_IEs = AUTH_REQ_NO_OF_IES; |
| 49 | |
| 50 | value->data = (proto_IEs *) malloc(SEC_MODE_NO_OF_IES * |
| 51 | sizeof(proto_IEs)); |
| 52 | |
| 53 | value->data[0].val.mme_ue_s1ap_id = g_authreqInfo->ue_idx; |
| 54 | value->data[1].val.enb_ue_s1ap_id = g_authreqInfo->enb_s1ap_ue_id; |
| 55 | |
| 56 | log_msg(LOG_INFO, "mme_ue_s1ap_id %d and enb_ue_s1ap_id %d\n", |
| 57 | g_authreqInfo->ue_idx, g_authreqInfo->enb_s1ap_ue_id); |
| 58 | |
| 59 | /* TODO: Add enum for security header type */ |
| 60 | value->data[2].val.nas.header.security_header_type = 0; |
| 61 | value->data[2].val.nas.header.proto_discriminator = EPSMobilityManagementMessages; |
| 62 | value->data[2].val.nas.header.message_type = AuthenticationRequest; |
| 63 | value->data[2].val.nas.header.nas_security_param = AUTHREQ_NAS_SECURITY_PARAM; |
| 64 | |
| 65 | value->data[2].val.nas.elements = (nas_pdu_elements *) |
| 66 | malloc(AUTH_REQ_NO_OF_NAS_IES * sizeof(nas_pdu_elements)); |
| 67 | |
| 68 | memcpy(value->data[2].val.nas.elements[0].pduElement.rand, |
| 69 | g_authreqInfo->rand, NAS_RAND_SIZE); |
| 70 | memcpy(value->data[2].val.nas.elements[1].pduElement.autn, |
| 71 | g_authreqInfo->autn, NAS_AUTN_SIZE); |
| 72 | |
| 73 | |
| 74 | return SUCCESS; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * Stage specific message processing. |
| 80 | */ |
| 81 | static int |
| 82 | authreq_processing(struct authreq_info *g_authreqInfo) |
| 83 | { |
| 84 | struct Buffer g_buffer; |
| 85 | struct Buffer g_value_buffer; |
| 86 | struct Buffer g_nas_buffer; |
| 87 | |
| 88 | struct s1ap_PDU s1apPDU= {0}; |
| 89 | |
| 90 | /* Assigning values to s1apPDU */ |
| 91 | s1apPDU.procedurecode = id_downlinkNASTransport; |
| 92 | s1apPDU.criticality = CRITICALITY_IGNORE; |
| 93 | |
| 94 | get_authreq_protoie_value(&s1apPDU.value, g_authreqInfo); |
| 95 | |
| 96 | /* Copy values to buffer from s1apPDU */ |
| 97 | |
| 98 | g_buffer.pos = 0; |
| 99 | |
| 100 | uint8_t initiating_message = 0; /* TODO: Add enum */ |
| 101 | buffer_copy(&g_buffer, &initiating_message, |
| 102 | sizeof(initiating_message)); |
| 103 | |
| 104 | buffer_copy(&g_buffer, &s1apPDU.procedurecode, |
| 105 | sizeof(s1apPDU.procedurecode)); |
| 106 | |
| 107 | buffer_copy(&g_buffer, &s1apPDU.criticality, |
| 108 | sizeof(s1apPDU.criticality)); |
| 109 | |
| 110 | /* Copy values in g_value_buffer */ |
| 111 | g_value_buffer.pos = 0; |
| 112 | |
| 113 | /* TODO remove hardcoded values */ |
| 114 | unsigned char chProtoIENo[3] = {0,0,3}; |
| 115 | |
| 116 | buffer_copy(&g_value_buffer, chProtoIENo, 3); |
| 117 | |
| 118 | unsigned char tmpStr[4]; |
| 119 | /* id-MME-UE-S1AP-ID */ |
| 120 | uint16_t protocolIe_Id = id_MME_UE_S1AP_ID; |
| 121 | copyU16(tmpStr, protocolIe_Id); |
| 122 | buffer_copy(&g_value_buffer, tmpStr, |
| 123 | sizeof(protocolIe_Id)); |
| 124 | |
| 125 | uint8_t protocolIe_criticality = CRITICALITY_REJECT; |
| 126 | buffer_copy(&g_value_buffer, &protocolIe_criticality, |
| 127 | sizeof(protocolIe_criticality)); |
| 128 | |
| 129 | uint8_t datalen = 2; |
| 130 | |
| 131 | /* TODO needs proper handling*/ |
| 132 | unsigned char mme_ue_id[3]; |
| 133 | datalen = copyU16(mme_ue_id, |
| 134 | s1apPDU.value.data[0].val.mme_ue_s1ap_id); |
| 135 | buffer_copy(&g_value_buffer, &datalen, sizeof(datalen)); |
| 136 | buffer_copy(&g_value_buffer, mme_ue_id, datalen); |
| 137 | |
| 138 | /* id-eNB-UE-S1AP-ID */ |
| 139 | |
| 140 | protocolIe_Id = id_eNB_UE_S1AP_ID; |
| 141 | copyU16(tmpStr, protocolIe_Id); |
| 142 | buffer_copy(&g_value_buffer, tmpStr, |
| 143 | sizeof(protocolIe_Id)); |
| 144 | |
| 145 | buffer_copy(&g_value_buffer, &protocolIe_criticality, |
| 146 | sizeof(protocolIe_criticality)); |
| 147 | |
| 148 | |
| 149 | /* TODO needs proper handling*/ |
| 150 | unsigned char enb_ue_id[3]; |
| 151 | datalen = copyU16(enb_ue_id, |
| 152 | s1apPDU.value.data[1].val.enb_ue_s1ap_id); |
| 153 | buffer_copy(&g_value_buffer, &datalen, sizeof(datalen)); |
| 154 | buffer_copy(&g_value_buffer, enb_ue_id, datalen); |
| 155 | //STIMER_GET_CURRENT_TP(g_attach_stats[s1apPDU.value.enb_ue_s1ap_id].esm_in); |
| 156 | |
| 157 | /* id-NAS-PDU */ |
| 158 | protocolIe_Id = id_NAS_PDU; |
| 159 | copyU16(tmpStr, protocolIe_Id); |
| 160 | buffer_copy(&g_value_buffer, tmpStr, |
| 161 | sizeof(protocolIe_Id)); |
| 162 | |
| 163 | buffer_copy(&g_value_buffer, &protocolIe_criticality, |
| 164 | sizeof(protocolIe_criticality)); |
| 165 | |
| 166 | struct nasPDU *nas = &(s1apPDU.value.data[2].val.nas); |
| 167 | uint8_t value = (nas->header.security_header_type) | |
| 168 | nas->header.proto_discriminator; |
| 169 | |
| 170 | g_nas_buffer.pos = 0; |
| 171 | |
| 172 | buffer_copy(&g_nas_buffer, &value, sizeof(value)); |
| 173 | |
| 174 | buffer_copy(&g_nas_buffer, &nas->header.message_type, |
| 175 | sizeof(nas->header.message_type)); |
| 176 | |
| 177 | buffer_copy(&g_nas_buffer, &nas->header.nas_security_param, |
| 178 | sizeof(nas->header.nas_security_param)); |
| 179 | |
| 180 | buffer_copy(&g_nas_buffer, |
| 181 | &nas->elements[0].pduElement.rand, |
| 182 | sizeof(nas->elements[0].pduElement.rand)); |
| 183 | |
| 184 | datalen = 16; |
| 185 | buffer_copy(&g_nas_buffer, &datalen, sizeof(datalen)); |
| 186 | |
| 187 | buffer_copy(&g_nas_buffer, |
| 188 | &nas->elements[1].pduElement.autn, |
| 189 | sizeof(nas->elements[1].pduElement.autn)); |
| 190 | |
| 191 | datalen = g_nas_buffer.pos + 1; |
| 192 | buffer_copy(&g_value_buffer, &datalen, |
| 193 | sizeof(datalen)); |
| 194 | |
| 195 | buffer_copy(&g_value_buffer, &g_nas_buffer.pos, |
| 196 | sizeof(g_nas_buffer.pos)); |
| 197 | |
| 198 | |
| 199 | buffer_copy(&g_value_buffer, &g_nas_buffer, |
| 200 | g_nas_buffer.pos); |
| 201 | |
| 202 | buffer_copy(&g_buffer, &g_value_buffer.pos, |
| 203 | sizeof(g_value_buffer.pos)); |
| 204 | |
| 205 | buffer_copy(&g_buffer, &g_value_buffer, |
| 206 | g_value_buffer.pos); |
| 207 | |
| 208 | free(s1apPDU.value.data[2].val.nas.elements); |
| 209 | free(s1apPDU.value.data); |
| 210 | |
| 211 | send_sctp_msg(g_authreqInfo->enb_fd, g_buffer.buf, g_buffer.pos, 1); |
| 212 | |
| 213 | return SUCCESS; |
| 214 | } |
| 215 | |
| 216 | void* |
| 217 | authreq_handler(void *data) |
| 218 | { |
| 219 | log_msg(LOG_INFO, "AuthReq handler.\n"); |
| 220 | |
| 221 | authreq_processing((struct authreq_info *)data); |
| 222 | |
| 223 | return NULL; |
| 224 | } |