Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firmware: Network module net_tools and border_router modifications #261

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 11 additions & 45 deletions firmware/network/border_router/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,47 +1,13 @@
ifeq (,$(filter usbus_cdc_ecm,$(USEMODULE)))
USEMODULE += usbus_cdc_ecm
endif

ifneq (,$(filter netdev_default,$(USEMODULE)))
USEMODULE += netdev_default
endif

ifneq (,$(filter auto_init_usbus,$(USEMODULE)))
USEMODULE += auto_init_usbus
endif

ifeq (,$(filter auto_init_gnrc_netif,$(USEMODULE)))
USEMODULE += auto_init_gnrc_netif
endif

ifneq (,$(filter gnrc_icmpv6_error,$(USEMODULE)))
USEMODULE += gnrc_icmpv6_error
endif

ifeq (,$(filter gnrc_sixlowpan_border_router_default,$(USEMODULE)))
USEMODULE += gnrc_sixlowpan_border_router_default
endif

ifneq (,$(filter gnrc_udp,$(USEMODULE)))
USEMODULE += gnrc_udp
endif

ifeq (,$(filter gnrc_icmpv6_echo,$(USEMODULE)))
USEMODULE += gnrc_icmpv6_echo
endif

ifneq (,$(filter gnrc_ipv6_default,$(USEMODULE)))
USEMODULE += gnrc_ipv6_default
endif

ifneq (,$(filter gnrc_netif_ieee802154,$(USEMODULE)))
USEMODULE += gnrc_netif_ieee802154
endif

ifeq (,$(filter radio,$(USEMODULE)))
USEMODULE += radio
endif

ifeq (,$(filter net_tools,$(USEMODULE)))
USEMODULE += net_tools
endif
USEMODULE += auto_init_gnrc_netif
USEMODULE += auto_init_usbus
USEMODULE += gnrc_icmpv6_echo
USEMODULE += gnrc_icmpv6_error
USEMODULE += gnrc_netif_ieee802154
USEMODULE += gnrc_sixlowpan_border_router_default
USEMODULE += gnrc_udp
USEMODULE += net_tools
USEMODULE += netdev_default
USEMODULE += radio
USEMODULE += usbus_cdc_ecm
55 changes: 23 additions & 32 deletions firmware/network/border_router/border_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/
/**
* @brief Border Radio.
* @brief Border Router Module
*
* @author RocioRojas <[email protected]>
* @author eduazocar <[email protected]>
*/
#include <stdio.h>
#include <string.h>
Expand All @@ -38,51 +39,41 @@ int8_t get_wired_iface(void) {
return -1;
}

int border_router_add_ipv6(int cast_type, ipv6_addr_t *addr, uint8_t iface_type) {
uint16_t flags = GNRC_NETIF_IPV6_ADDRS_FLAGS_STATE_VALID;
uint8_t prefix_len = _IPV6_DEFAULT_PREFIX_LEN;
netif_t *iface = NULL;
int8_t border_router_setup(ipv6_addr_t addr, uint8_t iface_type) {
ipv6_addr_t ip;
int8_t index;
if (iface_type == WIRED_INTERFACE) {
switch (iface_type) {
case WIRED_INTERFACE:
index = get_wired_iface();
} else if (iface_type == WIRELESS_INTERFACE) {
break;
case WIRELESS_INTERFACE:
index = get_ieee802154_iface();
} else {
break;
default:
printf("Error: Type of Interface doesn't exists File: %s, line: %d\n", __FILE__, __LINE__);
return -1;
}
if (index == -1) {
if (index < 0) {
printf("Error: Expected interface wasn't found. File: %s, line %d\n", __FILE__, __LINE__);
return -1;
}
if (get_ipv6_global(index, &ip) == 0) {
printf("Error: Already exists an ipv6 Address File: %s, line: %d\n", __FILE__, __LINE__);
return -1;
}
iface = netif_get_by_id(index);
if (cast_type == _ANYCAST || cast_type == _UNICAST || cast_type == _MULTICAST) {
if (cast_type == _MULTICAST) {
if (ipv6_addr_is_multicast(addr)) {
if (netif_set_opt(iface, NETOPT_IPV6_GROUP, 0, addr, sizeof(ipv6_addr_t)) < 0) {
printf("error: unable to join IPv6 multicast group\n");
return -1;
}
} else {
return -1;
}
} else {
if (cast_type == _ANYCAST) {
flags |= GNRC_NETIF_IPV6_ADDRS_FLAGS_ANYCAST;
}
flags |= (prefix_len << 8U);
if (netif_set_opt(iface, NETOPT_IPV6_ADDR, flags, addr, sizeof(ipv6_addr_t)) < 0) {
printf("error: unable to add IPv6 address\n");
return -1;
}
if (ipv6_addr_is_global(&addr)) {
if (set_ipv6_global(index, addr) < 0) {
return -1;
}
} else {
return -1;
return 0;
}
return 0;
if (ipv6_addr_is_multicast(&addr)) {
if (set_ipv6_multicast(index, addr) < 0) {
return -1;
}
return 0;
}
printf("Error: Only can be processed Unicast and Multicast Addresses. File: %s, line: %d\n",
__FILE__, __LINE__);
return -1;
}
34 changes: 9 additions & 25 deletions firmware/network/border_router/include/border_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

/**
* @{
* @ingroup network
* @ingroup border_router
* @file border_router.h
* @brief this module content all functions of Border Router
* @author RocioRojas <[email protected]>
*
* @author eduazocar <[email protected]>
*/

#ifndef BORDER_ROUTER_H
Expand All @@ -32,23 +32,6 @@
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief The default IPv6 prefix length if not specified.
*/
#define _IPV6_DEFAULT_PREFIX_LEN (64U)

/**
* @brief List of cast types to the ipv6 address
*
*/
enum cast_t {
_UNICAST = 0, /*!< Sets a process by which a packet is sent
from one host to an individual host*/
_ANYCAST, /*!< Sets a method forwards messages to a single
device of a specific group of devices. */
_MULTICAST /*!< Sets multicasting addresses messages for a
specific group of devices in a network */
};

/**
* @enum type_iface_t List of all types of Interface
Expand All @@ -62,14 +45,15 @@ enum type_iface_t {
/**@}*/

/**
* @brief This function it's set to border router to host.
* @brief This function init the border router. sets ipv6 in an interface.
*
* @param[in] addr ipv6 address
* @param[in] iface_type refers to if is used a WIRED or WIRELESS interface.
*
* @param [in] cast_type cast_type you want to set
* @param [in] addr ipv6 address
* @param [in] iface_type refers to if is used a WIRED or WIRELESS interface.
* @return int
* @retval 0 Setup Success
* @retval -1 Setup Failed
*/
int border_router_add_ipv6(int cast_type, ipv6_addr_t *addr, uint8_t iface_type);
int8_t border_router_setup(ipv6_addr_t addr, uint8_t iface_type);

#ifdef __cplusplus
}
Expand Down
26 changes: 23 additions & 3 deletions firmware/network/net_tools/include/net_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

/**
* @defgroup net_tools
* @defgroup net_tools Network tools
* @ingroup network
* @brief group of generic functions that could be used in various modules
*
Expand Down Expand Up @@ -50,11 +50,31 @@ extern "C" {
* @param[in] iface_pid it's the id of an interface
* @param[out] addr variable ipv6 where will be saved the found unicast global address
*
* @returns 0 if exist an ipv6 address and saves the located address in @p addr
* @returns -1 when the interface doesn't have an unicast global address
* @retval 0 if exist an ipv6 address and saves the located address in @p addr
* @retval -1 when the interface doesn't have an unicast global address
*/
int8_t get_ipv6_global(kernel_pid_t iface_pid, ipv6_addr_t *addr);

/**
* @brief Function to set an ipv6 global address in an iface.
*
* @param[in] iface_index index that will be save the ipv6 address.
* @param[in] ip Address ipv6 global to set
* @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_global(kernel_pid_t iface_index, ipv6_addr_t ip);

/**
* @brief Function to set an ipv6 global address in an iface.
*
* @param[in] iface_index index that will be save the ipv6 address.
* @param[in] ip Address ipv6 multicast to set
* @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);

#ifdef __cplusplus
}
#endif
Expand Down
38 changes: 37 additions & 1 deletion firmware/network/net_tools/net_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ int8_t get_ipv6_global(kernel_pid_t iface_pid, ipv6_addr_t *addr) {
int8_t num_of_addr = netif_get_opt(iface, NETOPT_IPV6_ADDR, 0, ipv6,
CONFIG_GNRC_NETIF_IPV6_ADDRS_NUMOF * sizeof(ipv6_addr_t));
if (num_of_addr < 0) {
printf("Error: Not ipv6 address found. File: %s, Line: %d\n", __FILE__, __LINE__);
printf("Error: Not ipv6 address found. File: %s, Function: %s, Line: %d\n", __FILE__,
__func__, __LINE__);
return -1;
}
for (unsigned i = 0; i < (num_of_addr / sizeof(ipv6_addr_t)); i++) {
Expand All @@ -55,4 +56,39 @@ int8_t get_ipv6_global(kernel_pid_t iface_pid, ipv6_addr_t *addr) {
return -1;
}

int8_t set_ipv6_global(kernel_pid_t iface_index, ipv6_addr_t ip) {
netif_t *iface = netif_get_by_id(iface_index);
if (!ipv6_addr_is_global(&ip)) {
printf("The ipv6 address isn't a ipv6 global address format accepted\n"
"File: %s, Function: %s, Line: %d\n",
__FILE__, __func__, __LINE__);
return -1;
}
if (netif_set_opt(iface, NETOPT_IPV6_ADDR, GNRC_NETIF_IPV6_ADDRS_FLAGS_STATE_VALID, &ip,
sizeof(ipv6_addr_t)) < 0) {
printf("error: unable to add IPv6 address\n"
"File: %s, Function: %s, Line: %d\n",
__FILE__, __func__, __LINE__);
return -1;
}
return 0;
}

int8_t set_ipv6_multicast(kernel_pid_t iface_index, ipv6_addr_t ip) {
netif_t *iface = netif_get_by_id(iface_index);
if (ipv6_addr_is_multicast(&ip)) {
printf("The ipv6 address isn't a ipv6 multicast address format accepted\n"
"File: %s, Function: %s, Line: %d\n",
__FILE__, __func__, __LINE__);
return -1;
}
if (netif_set_opt(iface, NETOPT_IPV6_GROUP, 0, &ip, sizeof(ipv6_addr_t)) < 0) {
printf("error: unable to join IPv6 multicast group\n"
"File: %s, Function: %s, Line: %d\n",
__FILE__, __func__, __LINE__);
return -1;
}
return 0;
}

/* Implementation of the module */