From ad5024dfd52e4d837aa50a850d9ea892482e3e8b Mon Sep 17 00:00:00 2001 From: eduazocar Date: Thu, 2 Jun 2022 16:13:17 -0400 Subject: [PATCH] firmware: border_router setup() integration --- firmware/network/border_router/Makefile.dep | 56 ++++--------------- .../network/border_router/border_router.c | 55 ++++++++---------- .../border_router/include/border_router.h | 34 +++-------- .../network/net_tools/include/net_tools.h | 26 ++++++++- firmware/network/net_tools/net_tools.c | 38 ++++++++++++- 5 files changed, 103 insertions(+), 106 deletions(-) diff --git a/firmware/network/border_router/Makefile.dep b/firmware/network/border_router/Makefile.dep index 00409ce2b..9d57c850c 100644 --- a/firmware/network/border_router/Makefile.dep +++ b/firmware/network/border_router/Makefile.dep @@ -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 diff --git a/firmware/network/border_router/border_router.c b/firmware/network/border_router/border_router.c index 71f32679a..4f81a2fa0 100644 --- a/firmware/network/border_router/border_router.c +++ b/firmware/network/border_router/border_router.c @@ -14,9 +14,10 @@ * limitations under the License. */ /** - * @brief Border Radio. + * @brief Border Router Module * * @author RocioRojas + * @author eduazocar */ #include #include @@ -38,21 +39,21 @@ 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; } @@ -60,29 +61,19 @@ int border_router_add_ipv6(int cast_type, ipv6_addr_t *addr, uint8_t iface_type) 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; } diff --git a/firmware/network/border_router/include/border_router.h b/firmware/network/border_router/include/border_router.h index 32d11be4c..2ea030c03 100644 --- a/firmware/network/border_router/include/border_router.h +++ b/firmware/network/border_router/include/border_router.h @@ -16,11 +16,11 @@ /** * @{ - * @ingroup network + * @ingroup border_router * @file border_router.h * @brief this module content all functions of Border Router * @author RocioRojas - * + * @author eduazocar */ #ifndef BORDER_ROUTER_H @@ -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 @@ -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 } diff --git a/firmware/network/net_tools/include/net_tools.h b/firmware/network/net_tools/include/net_tools.h index ddbcb6e23..eed9b4793 100644 --- a/firmware/network/net_tools/include/net_tools.h +++ b/firmware/network/net_tools/include/net_tools.h @@ -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 * @@ -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 diff --git a/firmware/network/net_tools/net_tools.c b/firmware/network/net_tools/net_tools.c index 11a2b22c8..380fcf88c 100644 --- a/firmware/network/net_tools/net_tools.c +++ b/firmware/network/net_tools/net_tools.c @@ -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++) { @@ -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 */