nr_pdcp_entity.c 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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
 */

#include "nr_pdcp_entity.h"

Cedric Roux's avatar
Cedric Roux committed
24 25 26 27
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

28
#include "nr_pdcp_security_nea2.h"
29
#include "nr_pdcp_integrity_nia2.h"
Cedric Roux's avatar
Cedric Roux committed
30
#include "nr_pdcp_sdu.h"
31 32 33

#include "LOG/log.h"

Cedric Roux's avatar
Cedric Roux committed
34 35
static void nr_pdcp_entity_recv_pdu(nr_pdcp_entity_t *entity,
                                    char *_buffer, int size)
36
{
Cedric Roux's avatar
Cedric Roux committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  unsigned char    *buffer = (unsigned char *)_buffer;
  nr_pdcp_sdu_t    *sdu;
  int              rcvd_sn;
  uint32_t         rcvd_hfn;
  uint32_t         rcvd_count;
  int              header_size;
  int              integrity_size;
  int              rx_deliv_sn;
  uint32_t         rx_deliv_hfn;

  if (size < 1) {
    LOG_E(PDCP, "bad PDU received (size = %d)\n", size);
    return;
  }

52 53
  if (entity->type != NR_PDCP_SRB &&
      !(buffer[0] & 0x80)) {
Cedric Roux's avatar
Cedric Roux committed
54 55 56 57 58
    LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }

  if (entity->sn_size == 12) {
59 60
    rcvd_sn = ((buffer[0] & 0xf) <<  8) |
                buffer[1];
Cedric Roux's avatar
Cedric Roux committed
61 62
    header_size = 2;
  } else {
63 64 65
    rcvd_sn = ((buffer[0] & 0x3) << 16) |
               (buffer[1]        <<  8) |
                buffer[2];
Cedric Roux's avatar
Cedric Roux committed
66 67
    header_size = 3;
  }
68 69 70
  /* SRBs always have MAC-I, even if integrity is not active */
  if (entity->has_integrity
      || entity->type == NR_PDCP_SRB) {
71 72 73 74
    integrity_size = 4;
  } else {
    integrity_size = 0;
  }
Cedric Roux's avatar
Cedric Roux committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

  if (size < header_size + integrity_size + 1) {
    LOG_E(PDCP, "bad PDU received (size = %d)\n", size);
    return;
  }

  rx_deliv_sn  = entity->rx_deliv & entity->sn_max;
  rx_deliv_hfn = (entity->rx_deliv >> entity->sn_size) & ~entity->sn_max;

  if (rcvd_sn < rx_deliv_sn - entity->window_size) {
    rcvd_hfn = rx_deliv_hfn + 1;
  } else if (rcvd_sn >= rx_deliv_sn + entity->window_size) {
    rcvd_hfn = rx_deliv_hfn - 1;
  } else {
    rcvd_hfn = rx_deliv_hfn;
  }

  rcvd_count = (rcvd_hfn << entity->sn_size) | rcvd_sn;

  if (entity->has_ciphering)
    entity->cipher(entity->security_context,
96
                   buffer+header_size, size-header_size,
Cedric Roux's avatar
Cedric Roux committed
97 98
                   entity->rb_id, rcvd_count, entity->is_gnb ? 0 : 1);

99 100 101 102 103 104 105 106 107 108 109
  if (entity->has_integrity) {
    unsigned char integrity[4];
    entity->integrity(entity->integrity_context, integrity,
                      buffer, size - integrity_size,
                      entity->rb_id, rcvd_count, entity->is_gnb ? 0 : 1);
    if (memcmp(integrity, buffer, 4) != 0) {
      LOG_E(PDCP, "discard NR PDU, integrity failed\n");
      return;
    }
  }

Cedric Roux's avatar
Cedric Roux committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  if (rcvd_count < entity->rx_deliv
      || nr_pdcp_sdu_in_list(entity->rx_list, rcvd_count)) {
    LOG_D(PDCP, "discard NR PDU rcvd_count=%d\n", rcvd_count);
    return;
  }

  sdu = nr_pdcp_new_sdu(rcvd_count,
                        (char *)buffer + header_size,
                        size - header_size - integrity_size);
  entity->rx_list = nr_pdcp_sdu_list_add(entity->rx_list, sdu);
  entity->rx_size += size-header_size;

  if (rcvd_count >= entity->rx_next) {
    entity->rx_next = rcvd_count + 1;
  }

  /* TODO(?): out of order delivery */

  if (rcvd_count == entity->rx_deliv) {
    /* deliver all SDUs starting from rx_deliv up to discontinuity or end of list */
    uint32_t count = entity->rx_deliv;
    while (entity->rx_list != NULL && count == entity->rx_list->count) {
      nr_pdcp_sdu_t *cur = entity->rx_list;
      entity->deliver_sdu(entity->deliver_sdu_data, entity,
                          cur->buffer, cur->size);
      entity->rx_list = cur->next;
      entity->rx_size -= cur->size;
      nr_pdcp_free_sdu(cur);
      count++;
    }
    entity->rx_deliv = count;
  }

  if (entity->t_reordering_start != 0 && entity->rx_deliv > entity->rx_reord) {
    /* stop and reset t-Reordering */
    entity->t_reordering_start = 0;
  }

  if (entity->t_reordering_start == 0 && entity->rx_deliv < entity->rx_next) {
    entity->rx_reord = entity->rx_next;
    entity->t_reordering_start = entity->t_current;
  }
152 153
}

Cedric Roux's avatar
Cedric Roux committed
154 155 156 157 158 159
static void nr_pdcp_entity_recv_sdu(nr_pdcp_entity_t *entity,
                                    char *buffer, int size, int sdu_id)
{
  uint32_t count;
  int      sn;
  int      header_size;
160 161
  int      integrity_size;
  char     buf[size + 3 + 4];
162
  int      dc_bit;
Cedric Roux's avatar
Cedric Roux committed
163 164 165 166

  count = entity->tx_next;
  sn = entity->tx_next & entity->sn_max;

167 168 169 170 171 172 173
  /* D/C bit is only to be set for DRBs */
  if (entity->type == NR_PDCP_DRB_AM || entity->type == NR_PDCP_DRB_UM) {
    dc_bit = 0x80;
  } else {
    dc_bit = 0;
  }

Cedric Roux's avatar
Cedric Roux committed
174
  if (entity->sn_size == 12) {
175
    buf[0] = dc_bit | ((sn >> 8) & 0xf);
Cedric Roux's avatar
Cedric Roux committed
176 177 178
    buf[1] = sn & 0xff;
    header_size = 2;
  } else {
179
    buf[0] = dc_bit | ((sn >> 16) & 0x3);
Cedric Roux's avatar
Cedric Roux committed
180 181 182 183 184
    buf[1] = (sn >> 8) & 0xff;
    buf[2] = sn & 0xff;
    header_size = 3;
  }

Cedric Roux's avatar
Cedric Roux committed
185
  /* SRBs always have MAC-I, even if integrity is not active */
186 187
  if (entity->has_integrity
      || entity->type == NR_PDCP_SRB) {
188 189 190 191 192 193 194 195 196 197 198 199
    integrity_size = 4;
  } else {
    integrity_size = 0;
  }

  memcpy(buf + header_size, buffer, size);

  if (entity->has_integrity)
    entity->integrity(entity->integrity_context,
                      (unsigned char *)buf + header_size + size,
                      (unsigned char *)buf, header_size + size,
                      entity->rb_id, count, entity->is_gnb ? 1 : 0);
200 201 202
  else if (integrity_size == 4)
    /* set MAC-I to 0 for SRBs with integrity not active */
    memset(buf + header_size + size, 0, 4);
Cedric Roux's avatar
Cedric Roux committed
203 204 205

  if (entity->has_ciphering)
    entity->cipher(entity->security_context,
206
                   (unsigned char *)buf + header_size, size + integrity_size,
Cedric Roux's avatar
Cedric Roux committed
207 208 209 210 211
                   entity->rb_id, count, entity->is_gnb ? 1 : 0);

  entity->tx_next++;

  entity->deliver_pdu(entity->deliver_pdu_data, entity, buf,
212
                      header_size + size + integrity_size, sdu_id);
Cedric Roux's avatar
Cedric Roux committed
213 214
}

215 216 217 218 219
static void nr_pdcp_entity_set_security(nr_pdcp_entity_t *entity,
                                        int integrity_algorithm,
                                        char *integrity_key,
                                        int ciphering_algorithm,
                                        char *ciphering_key)
Cedric Roux's avatar
Cedric Roux committed
220
{
221 222 223 224 225 226 227 228
  if (integrity_algorithm != -1)
    entity->integrity_algorithm = integrity_algorithm;
  if (ciphering_algorithm != -1)
    entity->ciphering_algorithm = ciphering_algorithm;
  if (integrity_key != NULL)
    memcpy(entity->integrity_key, integrity_key, 16);
  if (ciphering_key != NULL)
    memcpy(entity->ciphering_key, ciphering_key, 16);
Cedric Roux's avatar
Cedric Roux committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
}

static void check_t_reordering(nr_pdcp_entity_t *entity)
{
  uint32_t count;

  if (entity->t_reordering_start == 0
      || entity->t_current <= entity->t_reordering_start + entity->t_reordering)
    return;

  /* stop timer */
  entity->t_reordering_start = 0;

  /* deliver all SDUs with count < rx_reord */
  while (entity->rx_list != NULL && entity->rx_list->count < entity->rx_reord) {
    nr_pdcp_sdu_t *cur = entity->rx_list;
    entity->deliver_sdu(entity->deliver_sdu_data, entity,
                        cur->buffer, cur->size);
    entity->rx_list = cur->next;
    entity->rx_size -= cur->size;
    nr_pdcp_free_sdu(cur);
  }

  /* deliver all SDUs starting from rx_reord up to discontinuity or end of list */
  count = entity->rx_reord;
  while (entity->rx_list != NULL && count == entity->rx_list->count) {
    nr_pdcp_sdu_t *cur = entity->rx_list;
    entity->deliver_sdu(entity->deliver_sdu_data, entity,
                        cur->buffer, cur->size);
    entity->rx_list = cur->next;
    entity->rx_size -= cur->size;
    nr_pdcp_free_sdu(cur);
    count++;
  }

  entity->rx_deliv = count;

  if (entity->rx_deliv < entity->rx_next) {
    entity->rx_reord = entity->rx_next;
    entity->t_reordering_start = entity->t_current;
  }
}

void nr_pdcp_entity_set_time(struct nr_pdcp_entity_t *entity, uint64_t now)
{
  entity->t_current = now;

  check_t_reordering(entity);
}

void nr_pdcp_entity_delete(nr_pdcp_entity_t *entity)
{
  nr_pdcp_sdu_t *cur = entity->rx_list;
  while (cur != NULL) {
    nr_pdcp_sdu_t *next = cur->next;
    nr_pdcp_free_sdu(cur);
    cur = next;
  }
  if (entity->free_security != NULL)
    entity->free_security(entity->security_context);
289 290
  if (entity->free_integrity != NULL)
    entity->free_integrity(entity->integrity_context);
Cedric Roux's avatar
Cedric Roux committed
291 292 293 294 295
  free(entity);
}

nr_pdcp_entity_t *new_nr_pdcp_entity(
    nr_pdcp_entity_type_t type,
296
    int is_gnb, int rb_id,
297 298 299 300 301
    void (*deliver_sdu)(void *deliver_sdu_data, struct nr_pdcp_entity_t *entity,
                        char *buf, int size),
    void *deliver_sdu_data,
    void (*deliver_pdu)(void *deliver_pdu_data, struct nr_pdcp_entity_t *entity,
                        char *buf, int size, int sdu_id),
Cedric Roux's avatar
Cedric Roux committed
302 303 304
    void *deliver_pdu_data,
    int sn_size,
    int t_reordering,
305 306 307 308 309
    int discard_timer,
    int ciphering_algorithm,
    int integrity_algorithm,
    unsigned char *ciphering_key,
    unsigned char *integrity_key)
310
{
Cedric Roux's avatar
Cedric Roux committed
311
  nr_pdcp_entity_t *ret;
312

Cedric Roux's avatar
Cedric Roux committed
313
  ret = calloc(1, sizeof(nr_pdcp_entity_t));
314 315 316 317 318
  if (ret == NULL) {
    LOG_E(PDCP, "%s:%d:%s: out of memory\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }

Cedric Roux's avatar
Cedric Roux committed
319 320
  ret->type = type;

321 322 323 324
  ret->recv_pdu     = nr_pdcp_entity_recv_pdu;
  ret->recv_sdu     = nr_pdcp_entity_recv_sdu;
  ret->set_security = nr_pdcp_entity_set_security;
  ret->set_time     = nr_pdcp_entity_set_time;
325

Cedric Roux's avatar
Cedric Roux committed
326
  ret->delete = nr_pdcp_entity_delete;
327

Cedric Roux's avatar
Cedric Roux committed
328 329
  ret->deliver_sdu = deliver_sdu;
  ret->deliver_sdu_data = deliver_sdu_data;
330

Cedric Roux's avatar
Cedric Roux committed
331 332
  ret->deliver_pdu = deliver_pdu;
  ret->deliver_pdu_data = deliver_pdu_data;
333

Cedric Roux's avatar
Cedric Roux committed
334 335 336 337
  ret->rb_id         = rb_id;
  ret->sn_size       = sn_size;
  ret->t_reordering  = t_reordering;
  ret->discard_timer = discard_timer;
338

Cedric Roux's avatar
Cedric Roux committed
339 340
  ret->sn_max        = (1 << sn_size) - 1;
  ret->window_size   = 1 << (sn_size - 1);
341

342 343 344 345 346
  if (ciphering_key != NULL && ciphering_algorithm != 0) {
    if (ciphering_algorithm != 2) {
      LOG_E(PDCP, "FATAL: only nea2 supported for the moment\n");
      exit(1);
    }
Cedric Roux's avatar
Cedric Roux committed
347 348 349
    ret->has_ciphering = 1;
    ret->ciphering_algorithm = ciphering_algorithm;
    memcpy(ret->ciphering_key, ciphering_key, 16);
350

Cedric Roux's avatar
Cedric Roux committed
351 352 353
    ret->security_context = nr_pdcp_security_nea2_init(ciphering_key);
    ret->cipher = nr_pdcp_security_nea2_cipher;
    ret->free_security = nr_pdcp_security_nea2_free_security;
354
  }
Cedric Roux's avatar
Cedric Roux committed
355
  ret->is_gnb = is_gnb;
356

357 358 359 360 361 362 363 364 365 366 367 368
  if (integrity_key != NULL && integrity_algorithm != 0) {
    if (integrity_algorithm != 2) {
      LOG_E(PDCP, "FATAL: only nia2 supported for the moment\n");
      exit(1);
    }
    ret->has_integrity = 1;
    ret->integrity_algorithm = integrity_algorithm;
    memcpy(ret->integrity_key, integrity_key, 16);

    ret->integrity_context = nr_pdcp_integrity_nia2_init(integrity_key);
    ret->integrity = nr_pdcp_integrity_nia2_integrity;
    ret->free_integrity = nr_pdcp_integrity_nia2_free_integrity;
369 370
  }

Cedric Roux's avatar
Cedric Roux committed
371
  return ret;
372
}