Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mle_service_frame_counter_table.c Source File

mle_service_frame_counter_table.c

00001 /*
00002  * Copyright (c) 2018-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "nsconfig.h"
00019 #include <string.h>
00020 #include <ns_types.h>
00021 #include "ns_trace.h"
00022 #include "common_functions.h"
00023 #include "ccmLIB.h"
00024 #include "nsdynmemLIB.h"
00025 #include "Core/include/ns_address_internal.h"
00026 #include "Core/include/ns_buffer.h"
00027 #include "MLE/mle.h"
00028 #include "mac_common_defines.h"
00029 #include "Service_Libs/mle_service/mle_service_api.h"
00030 #include "Service_Libs/mle_service/mle_service_frame_counter_table.h"
00031 
00032 
00033 
00034 typedef struct {
00035     mle_neighbor_security_counter_info_t *security_counter_list;
00036     uint8_t table_size;
00037     int8_t interface_id;
00038     ns_list_link_t link;
00039 } mle_service_framecounter_instance_list_t;
00040 
00041 typedef NS_LIST_HEAD (mle_service_framecounter_instance_list_t, link) mle_service_counter_list_t;
00042 
00043 static mle_service_counter_list_t srv_framecounter_instance_list;
00044 
00045 
00046 static mle_service_framecounter_instance_list_t *mle_service_framecounter_params_get(int8_t interface_id)
00047 {
00048     ns_list_foreach(mle_service_framecounter_instance_list_t, cur_ptr, &srv_framecounter_instance_list) {
00049         if (cur_ptr->interface_id == interface_id) {
00050             return cur_ptr;
00051         }
00052     }
00053     return NULL;
00054 }
00055 
00056 static void mle_service_framecounter_delete(mle_neighbor_security_counter_info_t *cur_ptr)
00057 {
00058     cur_ptr->last_key_sequence = 0;
00059     cur_ptr->mle_frame_counter = 0;
00060     cur_ptr->new_key_pending = false;
00061 }
00062 
00063 
00064 mle_neighbor_security_counter_info_t *mle_service_counter_info_get(int8_t interface_id, uint8_t attribute_index)
00065 {
00066     mle_service_framecounter_instance_list_t *srv_ptr = mle_service_framecounter_params_get(interface_id);
00067 
00068     if (!srv_ptr || !srv_ptr->security_counter_list || attribute_index >= srv_ptr->table_size) {
00069         return NULL;
00070     }
00071     mle_neighbor_security_counter_info_t *entry = srv_ptr->security_counter_list + attribute_index;
00072     return entry;
00073 }
00074 
00075 int mle_service_frame_counter_table_free(int8_t interface_id)
00076 {
00077     mle_service_framecounter_instance_list_t *srv_ptr = mle_service_framecounter_params_get(interface_id);
00078     if (!srv_ptr) {
00079         return -1;
00080     }
00081     ns_list_remove(&srv_framecounter_instance_list, srv_ptr);
00082     ns_dyn_mem_free(srv_ptr->security_counter_list);
00083     ns_dyn_mem_free(srv_ptr);
00084     return 0;
00085 }
00086 
00087 int mle_service_frame_counter_table_allocate(int8_t interface_id, uint8_t table_size)
00088 {
00089 
00090     mle_service_framecounter_instance_list_t *srv_ptr = mle_service_framecounter_params_get(interface_id);
00091     if (!srv_ptr) {
00092         srv_ptr = ns_dyn_mem_alloc(sizeof(mle_service_framecounter_instance_list_t));
00093         if (!srv_ptr) {
00094             return -1;
00095         }
00096         ns_list_add_to_start(&srv_framecounter_instance_list, srv_ptr);
00097         srv_ptr->interface_id = interface_id;
00098         srv_ptr->table_size = 0;
00099         srv_ptr->security_counter_list = NULL;
00100     }
00101 
00102     if (srv_ptr->table_size != table_size) {
00103         ns_dyn_mem_free(srv_ptr->security_counter_list);
00104         srv_ptr->security_counter_list = ns_dyn_mem_alloc(sizeof(mle_neighbor_security_counter_info_t) * table_size);
00105     }
00106 
00107     if (!srv_ptr->security_counter_list) {
00108         mle_service_frame_counter_table_free(interface_id);
00109         return -1;
00110     }
00111     srv_ptr->table_size = table_size;
00112 
00113     mle_neighbor_security_counter_info_t *cur_ptr = srv_ptr->security_counter_list;
00114     for (uint8_t i = 0; i < table_size; i++) {
00115         mle_service_framecounter_delete(cur_ptr);
00116         cur_ptr++;
00117     }
00118     return 0;
00119 }
00120 
00121 void mle_service_frame_counter_entry_add(int8_t interface_id, uint8_t attribute_index, uint32_t frame_counter)
00122 {
00123     mle_neighbor_security_counter_info_t *entry = mle_service_counter_info_get(interface_id, attribute_index);
00124     if (entry) {
00125         entry->mle_frame_counter = frame_counter;
00126     }
00127 }
00128 
00129 void mle_service_frame_counter_entry_new_key_pending_set(int8_t interface_id, uint8_t attribute_index)
00130 {
00131     mle_neighbor_security_counter_info_t *entry = mle_service_counter_info_get(interface_id, attribute_index);
00132     if (entry) {
00133         entry->new_key_pending = true;
00134     }
00135 }
00136 
00137 void mle_service_frame_counter_entry_delete(int8_t interface_id, uint8_t attribute_index)
00138 {
00139     mle_neighbor_security_counter_info_t *entry = mle_service_counter_info_get(interface_id, attribute_index);
00140     if (entry) {
00141         mle_service_framecounter_delete(entry);
00142     }
00143 
00144 }
00145 
00146 uint32_t mle_service_neighbor_frame_counter_get(int8_t interface_id, uint8_t attribute_index)
00147 {
00148     mle_neighbor_security_counter_info_t *entry = mle_service_counter_info_get(interface_id, attribute_index);
00149     if (!entry) {
00150         return 0;
00151     }
00152     return entry->mle_frame_counter;
00153 
00154 }
00155