nas_nrue_task.c 12.1 KB
Newer Older
xuyue's avatar
xuyue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (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.openairinterface.org/?page_id=698
 *
 * 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.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

cucengineer's avatar
cucengineer committed
22
#define NAS_BUILT_IN_UE 1 //QUES: #undef
Chenyu's avatar
Chenyu committed
23
// #define __LITTLE_ENDIAN_BITFIELD 1
cucengineer's avatar
cucengineer committed
24

xuyue's avatar
xuyue committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "utils.h"
# include "assertions.h"
# include "intertask_interface.h"
# include "nas_nrue_task.h"
# include "common/utils/LOG/log.h"

# include "user_defs.h"
# include "user_api.h"
# include "nas_parser.h"
# include "nas_proc.h"
# include "msc.h"
# include "memory.h"

#include "nas_user.h"

// FIXME make command line option for NAS_UE_AUTOSTART
# define NAS_UE_AUTOSTART 1

// FIXME review these externs
extern unsigned char NB_eNB_INST;
extern uint16_t NB_UE_INST;

Chenyu's avatar
Chenyu committed
47 48 49
static int _nas_mm_msg_decode_header(mm_msg_header_t *header, const uint8_t *buffer, uint32_t len);
static int _nas_mm_msg_encode_header(const mm_msg_header_t *header, uint8_t *buffer, uint32_t len);

xuyue's avatar
xuyue committed
50 51 52 53 54 55 56 57 58
void *nas_ue_task(void *args_p)
{
  int                   nb_events;
  struct epoll_event   *events;
  MessageDef           *msg_p;
  instance_t            instance;
  unsigned int          Mod_id;
  int                   result;
  nas_user_container_t *users=args_p;
Chenyu's avatar
Chenyu committed
59
  UENAS_msg nrue_msg;
xuyue's avatar
xuyue committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

  itti_mark_task_ready (TASK_NAS_UE);
  MSC_START_USE();
  
  while(1) {
    // Wait for a message or an event
    itti_receive_msg (TASK_NAS_UE, &msg_p);

    if (msg_p != NULL) {
      instance = ITTI_MSG_INSTANCE (msg_p);
      Mod_id = instance - NB_eNB_INST;
      if (instance == INSTANCE_DEFAULT) {
        printf("%s:%d: FATAL: instance is INSTANCE_DEFAULT, should not happen.\n",
               __FILE__, __LINE__);
        exit_fun("exit... \n");
      }

      switch (ITTI_MSG_ID(msg_p)) {
      case INITIALIZE_MESSAGE:
        LOG_I(NAS, "[UE %d] Received %s\n", Mod_id,  ITTI_MSG_NAME (msg_p));

        break;

      case TERMINATE_MESSAGE:
        itti_exit_task ();
        break;

      case MESSAGE_TEST:
        LOG_I(NAS, "[UE %d] Received %s\n", Mod_id,  ITTI_MSG_NAME (msg_p));
        break;

cucengineer's avatar
cucengineer committed
91
      case NAS_CELL_SELECTION_CNF:  //CUC:NAS_CELL_SELECTION_CNF √
xuyue's avatar
xuyue committed
92 93
        LOG_I(NAS, "[UE %d] Received %s: errCode %u, cellID %u, tac %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
              NAS_CELL_SELECTION_CNF (msg_p).errCode, NAS_CELL_SELECTION_CNF (msg_p).cellID, NAS_CELL_SELECTION_CNF (msg_p).tac);
cucengineer's avatar
cucengineer committed
94 95 96 97
        as_stmsi_t s_tmsi={0,0};
        plmn_t plmnID={0, 0, 0, 0};
        Byte_t  data = 0;
        nas_itti_nas_establish_req(0, AS_TYPE_ORIGINATING_SIGNAL, s_tmsi, plmnID, &data, 0, 0);
xuyue's avatar
xuyue committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        break;

      case NAS_CELL_SELECTION_IND:
        LOG_I(NAS, "[UE %d] Received %s: cellID %u, tac %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
              NAS_CELL_SELECTION_IND (msg_p).cellID, NAS_CELL_SELECTION_IND (msg_p).tac);

        /* TODO not processed by NAS currently */
        break;

      case NAS_PAGING_IND:
        LOG_I(NAS, "[UE %d] Received %s: cause %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
              NAS_PAGING_IND (msg_p).cause);

        /* TODO not processed by NAS currently */
        break;

      case NAS_CONN_ESTABLI_CNF:
        LOG_I(NAS, "[UE %d] Received %s: errCode %u, length %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
              NAS_CONN_ESTABLI_CNF (msg_p).errCode, NAS_CONN_ESTABLI_CNF (msg_p).nasMsg.length);

        break;

      case NAS_CONN_RELEASE_IND:
        LOG_I(NAS, "[UE %d] Received %s: cause %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
              NAS_CONN_RELEASE_IND (msg_p).cause);

        break;

      case NAS_UPLINK_DATA_CNF:
        LOG_I(NAS, "[UE %d] Received %s: UEid %u, errCode %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
              NAS_UPLINK_DATA_CNF (msg_p).UEid, NAS_UPLINK_DATA_CNF (msg_p).errCode);

        break;

cucengineer's avatar
cucengineer committed
132
      case NAS_DOWNLINK_DATA_IND: //CUC:NAS_DOWNLINK_DATA_IND √
xuyue's avatar
xuyue committed
133 134
        LOG_I(NAS, "[UE %d] Received %s: UEid %u, length %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
              NAS_DOWNLINK_DATA_IND (msg_p).UEid, NAS_DOWNLINK_DATA_IND (msg_p).nasMsg.length);
Chenyu's avatar
Chenyu committed
135
        nr_nas_proc_dl_transfer_ind (&nrue_msg, NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data, NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.length); //handle dl info NAS mesaages.
xuyue's avatar
xuyue committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        break;

      default:
        LOG_E(NAS, "[UE %d] Received unexpected message %s\n", Mod_id,  ITTI_MSG_NAME (msg_p));
        break;
      }

      result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
      AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
      msg_p = NULL;
    }


  }

  free(users);
  return NULL;
}
cucengineer's avatar
cucengineer committed
154 155

void nr_nas_proc_dl_transfer_ind (UENAS_msg *msg,  Byte_t *data, uint32_t len) { //QUES: 解出的msg干什么
Chenyu's avatar
Chenyu committed
156
  uint8_t buffer[100];
cucengineer's avatar
cucengineer committed
157
  UENAS_msg *msg1;
Chenyu's avatar
Chenyu committed
158
  uint32_t len1=0;
cucengineer's avatar
cucengineer committed
159 160 161 162
  nr_user_nas_t UErrc= {0};//QUES:user
  int size;
  decodeNasMsg(msg,data,len);
  switch (msg->header.message_type) {
Chenyu's avatar
Chenyu committed
163 164 165 166 167 168 169 170 171 172 173
    // case IDENTITY_REQUEST: {     //send identityResponse in NAS_UPLINK_DATA_REQ
    //   msg1->header.ex_protocol_discriminator=0;
    //   msg1->header.security_header_type=0;
    //   len1 += sizeof(uint8_t);
    //   msg1->header.message_type = IDENTITY_RESPONSE;
    //   len1 += sizeof(uint8_t);
    //   len1 += identityResponse((void **)&msg1->identity_response, &UErrc);
    //   size = encodeNasMsg(msg1, buffer, len1);
    //   nas_itti_ul_data_req(0,buffer,size,0); 
    //   break;
    //   }
cucengineer's avatar
cucengineer committed
174 175 176 177 178 179 180
      
    case AUTHENTICATION_REQUEST: { //send authenticationResponse
      len1 += authenticationResponse((void **)&msg1->identity_response, &UErrc);
      size = encodeNasMsg(msg1, buffer, len1);
      nas_itti_ul_data_req(0,buffer,size,0);
      break;
      }
Chenyu's avatar
Chenyu committed
181

182 183 184 185 186 187 188 189 190 191 192 193 194
    case SECURITY_MODE_COMMAND: { 
      len1 += securityModeComplete5g((void **)&msg1->securitymode_complete);
      size = encodeNasMsg(msg1, buffer, len1);
      nas_itti_ul_data_req(0,buffer,size,0);
      break;
      }
    
    case REGISTRATION_ACCEPT: { 
      len1 += registrationComplete5g((void **)&msg1->registration_complete);
      size = encodeNasMsg(msg1, buffer, len1);
      nas_itti_ul_data_req(0,buffer,size,0);
      break;
      }
Chenyu's avatar
Chenyu committed
195 196 197 198
    
  }
  printf("aaaaaaaaaaaaa: ");
  printf("%d\n",size);
Chenyu's avatar
Chenyu committed
199 200
  for (int i = 0; i < size; i++)
  {
Chenyu's avatar
Chenyu committed
201 202
    printf("%02x ",*(buffer+i));

cucengineer's avatar
cucengineer committed
203
  }
Chenyu's avatar
Chenyu committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  printf("aaaaaaaaaaaaa \n ");

}

#define CHAR_TO_UINT8(input) ((input & 0xf) + 9*(input>>6))
//function to convert string to byte array
int string2ByteArray(char* input,uint8_t* output)
{
    int loop;
    int i;
    
    loop = 0;
    i = 0;
    
    while(input[loop] != '\0')
    {
        output[i++] = (CHAR_TO_UINT8(input[loop]))<<4 |  CHAR_TO_UINT8(input[loop+1]);
        loop += 2;
    }
    return i;
cucengineer's avatar
cucengineer committed
224 225
}

Chenyu's avatar
Chenyu committed
226 227 228 229 230 231 232 233 234 235 236 237
void tesths(void)
{
  
  UENAS_msg msg;
  char name[] = "7e005601020000217d003b4a2e3bb80403de19020f57b16a2010583f0d352eb89001539b2cb2cbf1da5c";
  uint32_t len=42;
  Byte_t *data= (uint8_t *)malloc(sizeof(uint8_t)*len);
  string2ByteArray(name, data);
  nr_nas_proc_dl_transfer_ind(&msg,data,len);

  
}
cucengineer's avatar
cucengineer committed
238 239 240 241 242
int decodeNasMsg(UENAS_msg *msg, uint8_t *buffer, uint32_t len) {
  int header_result;
  int decode_result=0;

  /* First decode the EMM message header */
Chenyu's avatar
Chenyu committed
243
  header_result = _nas_mm_msg_decode_header(&msg->header, buffer, len);
cucengineer's avatar
cucengineer committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

  if (header_result < 0) {
    LOG_TRACE(ERROR, "NR_UE   - Failed to decode EMM message header "
              "(%d)", header_result);
    LOG_FUNC_RETURN(header_result);
  }

  buffer += header_result;
  len -= header_result;
  LOG_TRACE(INFO, "NR_UE   - Message Type 0x%02x", msg->header.message_type);

  switch(msg->header.message_type) { 

  }

  LOG_FUNC_RETURN (header_result + decode_result);
}

int encodeNasMsg(UENAS_msg *msg, uint8_t *buffer, uint32_t len) { //QUES:UENAS_msg *msg
  int header_result;
  int encode_result=0;

  /* First encode the EMM message header */
Chenyu's avatar
Chenyu committed
267
  header_result = _nas_mm_msg_encode_header(&msg->header, buffer, len);
cucengineer's avatar
cucengineer committed
268 269 270 271 272 273 274 275 276 277 278

  if (header_result < 0) {
    LOG_TRACE(ERROR, "NR_UE   - Failed to encode EMM message header "
              "(%d)", header_result);
    LOG_FUNC_RETURN(header_result);
  }

  buffer += header_result;
  len -= header_result;

  switch(msg->header.message_type) { 
Chenyu's avatar
Chenyu committed
279 280 281 282
    // case IDENTITY_RESPONSE: {
    //   encode_result = encode_identity_response(&msg->identity_response, buffer, len);
    //   break;
    // }
cucengineer's avatar
cucengineer committed
283
    case AUTHENTICATION_RESPONSE: {
Chenyu's avatar
Chenyu committed
284
      encode_result = encode_authentication_response5g(&msg->authentication_response, buffer, len);
cucengineer's avatar
cucengineer committed
285 286
      break;
    }
287 288 289 290 291 292 293 294 295 296

    case SECURITY_MODE_COMPLETE: {
      encode_result = encode_security_mode_complete5g(&msg->securitymode_complete, buffer, len);//TODO:encode_security_mode_complete5g
      break;
    }

    case REGISTRATION_COMPLETE: {
      encode_result = encode_registration_complete5g(&msg->registration_complete, buffer, len);//TODO:encode_security_mode_complete5g
      break;
    }
cucengineer's avatar
cucengineer committed
297 298 299 300
  }
  LOG_FUNC_RETURN (header_result + encode_result);
}

Chenyu's avatar
Chenyu committed
301
static int _nas_mm_msg_decode_header(mm_msg_header_t *header, const uint8_t *buffer, uint32_t len) {  //QUES: 静态函数在哪声明?
cucengineer's avatar
cucengineer committed
302 303 304 305
  int size = 0;

  /* Check the buffer length */

Chenyu's avatar
Chenyu committed
306 307 308 309 310 311

  /* Encode the extendedprotocol discriminator */
  DECODE_U8(buffer + size, header->ex_protocol_discriminator, size);
  /* Encode the security header type */
  DECODE_U8(buffer + size, header->security_header_type, size);
  /* Encode the message type */
cucengineer's avatar
cucengineer committed
312 313 314 315
  DECODE_U8(buffer + size, header->message_type, size);

  /* Check the protocol discriminator */

Chenyu's avatar
Chenyu committed
316

cucengineer's avatar
cucengineer committed
317 318 319
  return (size);
}

Chenyu's avatar
Chenyu committed
320
static int _nas_mm_msg_encode_header(const mm_msg_header_t *header, uint8_t *buffer, uint32_t len) {
cucengineer's avatar
cucengineer committed
321 322 323
  int size = 0;

  /* Check the buffer length */
Chenyu's avatar
Chenyu committed
324 325 326
  if (len < sizeof(mm_msg_header_t)) {
    return (TLV_ENCODE_BUFFER_TOO_SHORT);
  }
cucengineer's avatar
cucengineer committed
327 328

  /* Check the protocol discriminator */
Chenyu's avatar
Chenyu committed
329 330 331 332 333
  if (header->ex_protocol_discriminator != FGS_MOBILITY_MANAGEMENT_MESSAGE) {
    LOG_TRACE(ERROR, "ESM-MSG   - Unexpected extened protocol discriminator: 0x%x",
              header->ex_protocol_discriminator);
    return (TLV_ENCODE_PROTOCOL_NOT_SUPPORTED);
  }
cucengineer's avatar
cucengineer committed
334

Chenyu's avatar
Chenyu committed
335 336 337 338
  /* Encode the extendedprotocol discriminator */
  ENCODE_U8(buffer + size, header->ex_protocol_discriminator, size);
  /* Encode the security header type */
  ENCODE_U8(buffer + size, (header->security_header_type & 0xf), size);
cucengineer's avatar
cucengineer committed
339 340 341 342
  /* Encode the message type */
  ENCODE_U8(buffer + size, header->message_type, size);
  return (size);
}
Chenyu's avatar
Chenyu committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362


int encode_authentication_response5g(authenticationresponse_t *authentication_response, uint8_t *buffer, uint32_t len)//QUES:AuthenticationResponse.c 中函数同名先编译哪个?
{
  int encoded = 0;

  if (authentication_response->iei > 0) {
    *buffer = authentication_response->iei;
    encoded++;
  }

  *(buffer + encoded) = authentication_response->RESlen;
  encoded++;
  for (int i = 0; i < authentication_response->RESlen; i++)
  {
    *(buffer + encoded) = authentication_response->RES[i];
    encoded++;
  }
  
  return encoded;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
}

int encode_security_mode_complete5g(securityModeComplete_t *securitymodecomplete, uint8_t *buffer, uint32_t len)
{
  int encoded = 0;

  return encoded;
}

int encode_registration_complete5g(registrationcomplete_t *registrationcomplete, uint8_t *buffer, uint32_t len)
{
  int encoded = 0;

  return encoded;
}

int securityModeComplete5g(void **msg) {
  myCalloc(resp, securityModeComplete_t);
  resp->epd=SGSmobilitymanagementmessages;
  resp->sh=0;
  resp->mt=Registrationcomplete;
  *msg=resp;
  return sizeof(securityModeComplete_t);
}
Chenyu's avatar
Chenyu committed
387

388 389 390 391 392 393 394
int registrationComplete5g(void **msg) {
  myCalloc(resp, registrationcomplete_t);
  resp->epd=SGSmobilitymanagementmessages;
  resp->sh=0;
  resp->mt=Securitymodecomplete;
  *msg=resp;
  return sizeof(registrationcomplete_t);
Chenyu's avatar
Chenyu committed
395
}