Skip to content

Commit

Permalink
Merge pull request #353 from eduazocar/feature/uniqueid
Browse files Browse the repository at this point in the history
firmware/sys/uniqueid: get_uid_seed and net_tools set_ipv6_by_uid
  • Loading branch information
CW-75 authored Sep 24, 2022
2 parents 104ada2 + e46f67a commit 476e0c1
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 98 deletions.
22 changes: 19 additions & 3 deletions firmware/network/net_tools/include/net_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
extern "C" {
#endif

#define PREFIX_LSHIFTTED_BITS (8)
#define BITS_IN_A_BYTE (8)
#define PREFIX_LSHIFTTED_BITS (8)

/**
* @brief Function to check if exist a wired interface
Expand All @@ -70,7 +71,7 @@ int8_t get_ipv6_global(kernel_pid_t iface_pid, ipv6_addr_t *addr);
*
* @param[in] iface_index index that will be save the ipv6 address.
* @param[in] ip Address ipv6 global to set.
* @param[in] prefix Networks prefix,express the subnet size.
* @param[in] prefix Networks prefix represent the subnet size.
* @retval 0 Correctly set up of the @p ip in the interface.
* @retval -1 Couldn't set the @p ip address in the interface.
*/
Expand All @@ -81,12 +82,27 @@ int8_t set_ipv6_global(kernel_pid_t iface_index, ipv6_addr_t ip, uint8_t prefix)
*
* @param[in] iface_index index that will be save the ipv6 address.
* @param[in] ip Address ipv6 multicast to set
* @param[in] prefix Networks prefix,express the subnet size.
* @param[in] prefix Networks prefix represent the subnet size.
* @retval 0 Correctly set up of the @p ip in the interface.
* @retval -1 Couldn't set the @p ip address in the interface.
*/
int8_t set_ipv6_multicast(kernel_pid_t iface_index, ipv6_addr_t ip, uint8_t prefix);

#if (MODULE_UNIQUEID) || (DOXYGEN)
/**
* @brief Function to set an ipv6 global address in an iface.
*
* @param[in] iface_index index that will be save the ipv6 address.
* @param[inout] ip Address ipv6 to set. the octets of the required IPv6 address should be
* previously defined (depending on the network prefixes)
* @param[in] prefix Networks prefix represent the subnet size.
* @param uid_mode define if the uniqueid generate random or static ipv6 address
* @retval 0 Correctly set up of the @p ip in the interface.
* @retval -1 Couldn't set the @p ip address in the interface.
*/
int8_t set_ipv6_by_uid(const kernel_pid_t iface_index, ipv6_addr_t *ip, const uint8_t prefix,
const uint8_t uid_mode);
#endif
#ifdef __cplusplus
}
#endif
Expand Down
35 changes: 33 additions & 2 deletions firmware/network/net_tools/net_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#include "net_tools.h"
#include "net/ipv6/addr.h"
#include "net/gnrc/netif.h"
#if MODULE_UNIQUEID
#include "uniqueid.h"
#endif

#if (CONFIG_DEBUG_NET_TOOLS) || (DOXYGEN)
/**
Expand Down Expand Up @@ -83,6 +86,34 @@ int8_t get_ipv6_global(kernel_pid_t iface_pid, ipv6_addr_t *addr) {
return -1;
}

#if MODULE_UNIQUEID
int8_t set_ipv6_by_uid(const kernel_pid_t iface_index, ipv6_addr_t *ip, const uint8_t prefix,
const uint8_t uid_mode) {
uint8_t pfx_pos = prefix / BITS_IN_A_BYTE;
uint8_t pfx_bytes = sizeof(ipv6_addr_t) - pfx_pos;
if (prefix > 128) {
DEBUG("Wrong prefix length\n"
"File: %s, Function: %s, Line: %d\n",
__FILE__, __func__, __LINE__);
}
switch (uid_mode) {
case UNIQUEID_STATIC_MODE:
DEBUG("Setting ipv6 via uniqueid static mode\n");
get_uid_seed(ip->u8 + pfx_pos, pfx_bytes, uid_mode);
break;
case UNIQUEID_RANDOM_MODE:
DEBUG("Setting ipv6 via uniqueid random mode\n");
get_uid_seed(ip->u8 + pfx_pos, pfx_bytes, uid_mode);
break;
default:
return -1;
}
if (set_ipv6_global(iface_index, *ip, prefix) < 0) {
return -1;
}
return 0;
}
#endif
int8_t set_ipv6_global(kernel_pid_t iface_index, ipv6_addr_t ip, uint8_t prefix) {
if (prefix > 128) {
DEBUG("Wrong prefix length\n"
Expand All @@ -100,8 +131,8 @@ int8_t set_ipv6_global(kernel_pid_t iface_index, ipv6_addr_t ip, uint8_t prefix)
if (netif_set_opt(iface, NETOPT_IPV6_ADDR, flags, &ip, sizeof(ipv6_addr_t)) <
PREFIX_LSHIFTTED_BITS) {
DEBUG("error: unable to add IPv6 address\n"
"File: %s, Function: %s, Line: %d\n",
__FILE__, __func__, __LINE__);
"File: %s, Function: %s, Line: %d\n",
__FILE__, __func__, __LINE__);
return -1;
}
return 0;
Expand Down
2 changes: 0 additions & 2 deletions firmware/network/rpl_protocol/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
USEMODULE += radio
USEMODULE += gnrc_rpl
USEMODULE += auto_init_gnrc_rpl
USEMODULE += uniqueid
USEMODULE += gnrc_ipv6_router_default
USEMODULE += net_tools

ifneq (,$(filter 6lpan_node,$(USEMODULE)))
USEMODULE += gnrc_sixlowpan_router_default
Expand Down
11 changes: 0 additions & 11 deletions firmware/network/rpl_protocol/rpl_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include "net/gnrc/rpl.h"
#include "rpl_protocol.h"
#include "radio.h"
#include "net_tools.h"
#include "uniqueid.h"

#if (CONFIG_DEBUG_RPL_PROTOCOL) || (DOXYGEN)
/**
Expand Down Expand Up @@ -102,15 +100,6 @@ int8_t rpl_setup(uint8_t mode) {
return -1;
}
if (mode == DODAG || CONFIG_IS_DODAG == DODAG) {
err = get_ipv6_global(iface_index, &ip);
if (err < 0) {
get_uid_ipv6(&ip, KCONFIG_UID_OPTION || UNIQUEID_STATIC_MODE);
if (gnrc_netif_ipv6_addr_add(gnrc_netif_get_by_pid(iface_index), &ip, 64,
GNRC_NETIF_IPV6_ADDRS_FLAGS_STATE_VALID) < 0) {
DEBUG("Error: Couldn't add IPv6 global address\n");
return -1;
}
}
err = gnrc_rpl_dodag_root(CONFIG_DODAG_INSTANCE, &ip);
if (err < 0) {
DEBUG("Error: Rpl root node ipv6 couldn't set ipv6 global address, File: %s line: %d\n",
Expand Down
8 changes: 0 additions & 8 deletions firmware/sys/uniqueid/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,3 @@ USEMODULE += ipv6_addr
USEMODULE += netdev_default
USEMODULE += gnrc_ipv6_default
USEMODULE += auto_init_gnrc_netif

ifneq (,$(filter at86rf2xx at86rf215,$(USEMODULE)))
USEMODULE += radio
else
ifneq (,$(filter periph_hwrng,$(HAS_PERIPH_HWRNG)))
USEMODULE += periph_hwrng
endif
endif
8 changes: 5 additions & 3 deletions firmware/sys/uniqueid/include/uniqueid.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ union random_buff {
};
/**
* @brief This function get ipv6 address (mode: static (default), random, manual)
* @param [out] addr Address in ipv6 format
* @param [out] val any value type where will saved the seed.
* @param [in] len size in number of bytes of @p val.
* @param [in] uid_mode defines the uniqueid mode, refers random or static mode.
*
*/
void get_uid_ipv6(ipv6_addr_t *addr, uniqueid_mode_t mode);
void get_uid_seed(void *val, uint8_t len, uint8_t uid_mode);

uint32_t get_uid_seed(void *val, const uint8_t len);
#ifdef __cplusplus
}
#endif
Expand Down
75 changes: 37 additions & 38 deletions firmware/sys/uniqueid/uniqueid.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
#include <string.h>
#include "uniqueid.h"
#include "random.h"
#if (MODULE_AT86RF2XX || MODULE_AT86RF215)
#include "net/netdev/ieee802154.h"
#include "net/gnrc.h"
#if MODULE_AT86RF2XX || MODULE_AT86RF215
#include "radio.h"
#elif MODULE_PERIPH_HWRNG
#endif
#if MODULE_PERIPH_HWRNG
#include "periph/hwrng.h"
#endif

Expand All @@ -44,45 +44,44 @@
#endif
#include "debug.h"

void get_uid_ipv6(ipv6_addr_t *addr, uniqueid_mode_t mode) {
ipv6_addr_t header = {
.u8 = {0},
};
switch (mode) {
void get_uid_seed(void *val, uint8_t len, uint8_t uid_mode) {
uint32_t rval;
uint8_t cpuid_bytes = CPUID_LEN / 2;
char addr_cpu[CPUID_LEN];
CPUID(addr_cpu);
switch (uid_mode) {
case UNIQUEID_STATIC_MODE:
ipv6_addr_from_str(&header, CONFIG_HEADER_ADDRESS_ID);
memcpy(addr->u8, header.u8, 8);
char addr_cpu[CPUID_LEN];
CPUID(addr_cpu);
memcpy(addr->u8 + LAST_OCTECTS, addr_cpu, CPUID_LEN <= 8? CPUID_LEN : OCTETS_BYTE_SIZE );
break;
if (len > cpuid_bytes) {
memcpy(val, addr_cpu, cpuid_bytes);
len = len - cpuid_bytes;
return get_uid_seed((uint8_t *)val + cpuid_bytes, len, UNIQUEID_RANDOM_MODE);
} else {
memcpy(val, addr_cpu, len);
break;
}
case UNIQUEID_RANDOM_MODE:
ipv6_addr_from_str(&header, CONFIG_HEADER_ADDRESS_ID);
memcpy(addr->u8, header.u8, OCTETS_BYTE_SIZE);
union random_buff random_number = {0};
get_uid_seed(&random_number, OCTETS_BYTE_SIZE);
memcpy(addr->u8 + LAST_OCTECTS, random_number.u8, OCTETS_BYTE_SIZE);
break;
default:
break;
}
}

uint32_t get_uid_seed(void *val, const uint8_t len) {
uint32_t rval;
DEBUG("rand mode\n");
#if (MODULE_AT86RF2XX || MODULE_AT86RF215)
int index = get_ieee802154_iface();
netif_t *iface = netif_get_by_id(index);
netif_get_opt(iface, NETOPT_RANDOM, 0, &rval, sizeof(uint32_t));
netdev_type_t at_ifaces[] = {NETDEV_AT86RF2XX, NETDEV_AT86RF215};
gnrc_netif_t *iface = NULL;
for (uint8_t i = 0; i < sizeof(at_ifaces); i++) {
if ((iface = gnrc_netif_get_by_type(at_ifaces[i], NETDEV_INDEX_ANY)) != NULL) {
break;
}
}
netif_get_opt(&iface->netif, NETOPT_RANDOM, 0, &rval, sizeof(uint32_t));
#else
hwrng_init();
hwrng_read(&rval, sizeof(rval));
hwrng_init();
hwrng_read(&rval, sizeof(rval));
#endif
for (uint8_t i = 0; i < len; i++) {
random_init(rval);
rval = random_uint32();
memcpy(((uint8_t*)val + i), &rval, sizeof(uint8_t));
for (uint8_t i = 0; i < len; i++) {
random_init(rval);
rval = random_uint32();
memcpy(((uint8_t *)val + i), &rval, sizeof(uint8_t));
}
break;
default:
break;
}

return 0;
return;
}
1 change: 1 addition & 0 deletions tests/net_rpl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include ../Makefile.tests_common
USEMODULE += embunit
USEMODULE += uniqueid
USEMODULE += rpl_protocol
USEMODULE += net_tools
USEMODULE += radio

include $(RIOTBASE)/Makefile.include
14 changes: 6 additions & 8 deletions tests/net_rpl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,28 @@
#include "embUnit.h"
#include "uniqueid.h"
#include "rpl_protocol.h"
#include "net_tools.h"
#include "radio.h"

#define IFACE_ID 4
#define DODAG_INSTANCE 1

void test_init_rpl(void) {
int err = rpl_init(IFACE_ID);
TEST_ASSERT_EQUAL_INT(0, err);
int err = rpl_init(IFACE_ID);
TEST_ASSERT_EQUAL_INT(0, err);
}

void test_add_dodag_node(void) {
ipv6_addr_t ipv6 = {
.u8 = {0},
.u8 = {0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x01},
};

subnet_to_ipv6(&ipv6);
uint8_t iface_index = get_ieee802154_iface();
gnrc_netif_ipv6_addr_add(gnrc_netif_get_by_pid(iface_index), &ipv6, 64,
GNRC_NETIF_IPV6_ADDRS_FLAGS_STATE_VALID);
set_ipv6_by_uid(iface_index, &ipv6, 64, UNIQUEID_STATIC_MODE);
int err = gnrc_rpl_dodag_root(DODAG_INSTANCE, &ipv6);
TEST_ASSERT_EQUAL_INT(0, err);
}

void test_remove_dodag_node (void) {
void test_remove_dodag_node(void) {
int err = rpl_dodag_remove(DODAG_INSTANCE);
TEST_ASSERT_EQUAL_INT(0, err);
}
Expand Down
1 change: 1 addition & 0 deletions tests/system_uniqueid/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ include ../Makefile.tests_common

USEMODULE += embunit
USEMODULE += uniqueid
USEMODULE += net_tools

include $(RIOTBASE)/Makefile.include
47 changes: 38 additions & 9 deletions tests/system_uniqueid/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,58 @@
#include "embUnit.h"
#include "uniqueid.h"
#include "unique_random.h"
#include "net_tools.h"

ipv6_addr_t addr;

void test_get_ipv6_static(void){
get_uid_ipv6(&addr, UNIQUEID_STATIC_MODE);
ipv6_addr_print(&addr);
void test_get_uid_seed_static(void) {
uint8_t val[CPUID_LEN / 2];
get_uid_seed(val, sizeof(val), UNIQUEID_STATIC_MODE);
printf("CPUID seed in hex: ");
for (uint8_t i = 0; i < sizeof(val); i++) {
printf("%02X ", val[i]);
}
printf("\n");
printf("CPUID seed in uint32: %" PRIu32 "\n", (uint32_t)val);
}

void test_get_ipv6_random(void){
get_uid_ipv6(&addr, UNIQUEID_RANDOM_MODE);
void test_get_uid_seed_random(void) {
uint32_t val = 0;
get_uid_seed(&val, sizeof(val), UNIQUEID_RANDOM_MODE);
printf("Random seed in hex: ");
for (uint8_t i = sizeof(val); i > 0; i--) {
printf("%02X ", (uint8_t)((val >> (i - 1) * BITS_IN_A_BYTE)));
}
printf("\n");
printf("random seed in uint32: %" PRIu32 "\n", val);
}

void test_get_ipv6_static(void) {
uint8_t iface = get_wired_iface();
ipv6_addr_from_str(&addr, "2001:db8:2:1::");
set_ipv6_by_uid(iface, &addr, 64, UNIQUEID_STATIC_MODE);
ipv6_addr_print(&addr);
printf("\n");
}

void test_uid_random_blocks(void){
random_generator(64);
void test_get_ipv6_random(void) {
uint8_t iface = get_wired_iface();
ipv6_addr_from_str(&addr, "2001:db8:2:1::");
set_ipv6_by_uid(iface, &addr, 64, UNIQUEID_RANDOM_MODE);
ipv6_addr_print(&addr);
printf("\n");
}

void test_randomblocks(void) { random_generator(8); }

void test_uid_random_blocks(void) { random_generator(64); }

Test *tests_get_unique_id(void) {
EMB_UNIT_TESTFIXTURES(fixtures){
new_TestFixture(test_get_ipv6_static),
new_TestFixture(test_get_ipv6_random),
new_TestFixture(test_get_uid_seed_static), new_TestFixture(test_get_uid_seed_random),
new_TestFixture(test_get_ipv6_static), new_TestFixture(test_get_ipv6_random),
new_TestFixture(test_randomblocks),

};

EMB_UNIT_TESTCALLER(tests_get_unique_id, NULL, NULL, fixtures);
Expand Down
Loading

0 comments on commit 476e0c1

Please sign in to comment.