From 39fe2a30e43297c0c55425d690d458fe495b794b Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Mon, 10 Dec 2018 17:23:14 +0100 Subject: [PATCH] gnrc_netif: use l2util for some functions The following functions can now be wrapped around the more generalized approach: - gnrc_netif_ipv6_iid_from_addr() - gnrc_netif_ipv6_iid_to_addr() - gnrc_netif_ndp_addr_len_from_l2ao() - gnrc_netif_eui64_from_addr() --- sys/include/net/gnrc/netif/internal.h | 17 +- sys/net/gnrc/netif/gnrc_netif_device_type.c | 189 +------------------- 2 files changed, 22 insertions(+), 184 deletions(-) diff --git a/sys/include/net/gnrc/netif/internal.h b/sys/include/net/gnrc/netif/internal.h index 2b07074613..fcbd6ec50d 100644 --- a/sys/include/net/gnrc/netif/internal.h +++ b/sys/include/net/gnrc/netif/internal.h @@ -22,6 +22,7 @@ #define NET_GNRC_NETIF_INTERNAL_H #include "net/gnrc/netif.h" +#include "net/l2util.h" #include "net/netopt.h" #ifdef MODULE_GNRC_IPV6_NIB @@ -539,8 +540,12 @@ int gnrc_netif_ipv6_iid_from_addr(const gnrc_netif_t *netif, * @return `-ENOTSUP`, when gnrc_netif_t::device_type of @p netif does not * support reverse IID conversion. */ -int gnrc_netif_ipv6_iid_to_addr(const gnrc_netif_t *netif, const eui64_t *iid, - uint8_t *addr); +static inline int gnrc_netif_ipv6_iid_to_addr(const gnrc_netif_t *netif, + const eui64_t *iid, uint8_t *addr) +{ + assert(netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR); + return l2util_ipv6_iid_to_addr(netif->device_type, iid, addr); +} /** * @brief Converts an interface IID of an interface's hardware address @@ -602,8 +607,12 @@ static inline int gnrc_netif_ipv6_get_iid(gnrc_netif_t *netif, eui64_t *iid) * @return `-EINVAL` if `opt->len` was an invalid value for the given * gnrc_netif_t::device_type of @p netif. */ -int gnrc_netif_ndp_addr_len_from_l2ao(gnrc_netif_t *netif, - const ndp_opt_t *opt); +static inline int gnrc_netif_ndp_addr_len_from_l2ao(gnrc_netif_t *netif, + const ndp_opt_t *opt) +{ + assert(netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR); + return l2util_ndp_addr_len_from_l2ao(netif->device_type, opt); +} #else /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */ #define gnrc_netif_ipv6_init_mtu(netif) (void)netif #define gnrc_netif_ipv6_iid_from_addr(netif, addr, addr_len, iid) (-ENOTSUP) diff --git a/sys/net/gnrc/netif/gnrc_netif_device_type.c b/sys/net/gnrc/netif/gnrc_netif_device_type.c index b4575b2094..119ba8fe66 100644 --- a/sys/net/gnrc/netif/gnrc_netif_device_type.c +++ b/sys/net/gnrc/netif/gnrc_netif_device_type.c @@ -24,6 +24,7 @@ #include "net/eui48.h" #include "net/ethernet.h" #include "net/ieee802154.h" +#include "net/l2util.h" netopt_t gnrc_netif_get_l2addr_opt(const gnrc_netif_t *netif) { @@ -54,19 +55,6 @@ netopt_t gnrc_netif_get_l2addr_opt(const gnrc_netif_t *netif) return res; } -#if defined(MODULE_CC110X) || defined(MODULE_NRFMIN) -static void _create_eui64_from_short(const uint8_t *addr, size_t addr_len, - eui64_t *eui64) -{ - const unsigned offset = sizeof(eui64_t) - addr_len; - - memset(eui64->uint8, 0, sizeof(eui64->uint8)); - eui64->uint8[3] = 0xff; - eui64->uint8[4] = 0xfe; - memcpy(&eui64->uint8[offset], addr, addr_len); -} -#endif /* defined(MODULE_CC110X) || defined(MODULE_NRFMIN) */ - int gnrc_netif_eui64_from_addr(const gnrc_netif_t *netif, const uint8_t *addr, size_t addr_len, eui64_t *eui64) @@ -74,55 +62,24 @@ int gnrc_netif_eui64_from_addr(const gnrc_netif_t *netif, #if GNRC_NETIF_L2ADDR_MAXLEN > 0 if (netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR) { switch (netif->device_type) { -#if defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) || \ - defined(MODULE_NORDIC_SOFTDEVICE_BLE) - case NETDEV_TYPE_ETHERNET: - case NETDEV_TYPE_ESP_NOW: - case NETDEV_TYPE_BLE: - if (addr_len == sizeof(eui48_t)) { - eui48_to_eui64(eui64, (const eui48_t *)addr); - return sizeof(eui64_t); - } - else { - return -EINVAL; - } -#endif /* defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) */ #if defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) case NETDEV_TYPE_IEEE802154: + /* try getting EUI-64 from device if short address is + * provided */ switch (addr_len) { case IEEE802154_SHORT_ADDRESS_LEN: { netdev_t *dev = netif->dev; return dev->driver->get(dev, NETOPT_ADDRESS_LONG, eui64, sizeof(eui64_t)); } - case IEEE802154_LONG_ADDRESS_LEN: - memcpy(eui64, addr, addr_len); - return sizeof(eui64_t); default: - return -EINVAL; + break; } + /* Intentionally falls through */ #endif /* defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) */ -#if defined(MODULE_CC110X) || defined(MODULE_NRFMIN) - case NETDEV_TYPE_CC110X: - case NETDEV_TYPE_NRFMIN: - if (addr_len <= 3) { - _create_eui64_from_short(addr, addr_len, eui64); - return sizeof(eui64_t); - } - else { - return -EINVAL; - } -#endif /* defined(MODULE_CC110X) || defined(MODULE_NRFMIN) */ default: - (void)addr; - (void)addr_len; - (void)eui64; -#ifdef DEVELHELP - LOG_ERROR("gnrc_netif: can't convert hardware address to EUI-64" - " on interface %u\n", netif->pid); -#endif /* DEVELHELP */ - assert(false); - break; + return l2util_eui64_from_addr(netif->device_type, addr, + addr_len, eui64); } } #endif /* GNRC_NETIF_L2ADDR_MAXLEN > 0 */ @@ -204,141 +161,13 @@ int gnrc_netif_ipv6_iid_from_addr(const gnrc_netif_t *netif, { #if GNRC_NETIF_L2ADDR_MAXLEN > 0 if (netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR) { - switch (netif->device_type) { -#if defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) - case NETDEV_TYPE_IEEE802154: - if (ieee802154_get_iid(iid, addr, addr_len) != NULL) { - return sizeof(eui64_t); - } - else { - return -EINVAL; - } -#endif /* defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) */ -#if defined(MODULE_CC110X) || defined(MODULE_NRFMIN) - case NETDEV_TYPE_CC110X: - case NETDEV_TYPE_NRFMIN: - if (addr_len <= 3) { - _create_eui64_from_short(addr, addr_len, iid); - /* since this address conversion is based on the IEEE - * 802.15.4 address conversion for short addresses, the - * U/L bit doesn't need to be flipped. - * see https://tools.ietf.org/html/rfc6282#section-3.2.2 */ - return sizeof(eui64_t); - } - else { - return -EINVAL; - } -#endif /* defined(MODULE_CC110X) || defined(MODULE_NRFMIN) */ - default: { - int res = gnrc_netif_eui64_from_addr(netif, addr, addr_len, - iid); - if (res == sizeof(eui64_t)) { - iid->uint8[0] ^= 0x02; - } - return res; - } - } + return l2util_ipv6_iid_from_addr(netif->device_type, + addr, addr_len, iid); } #endif /* GNRC_NETIF_L2ADDR_MAXLEN > 0 */ return -ENOTSUP; } -int gnrc_netif_ipv6_iid_to_addr(const gnrc_netif_t *netif, const eui64_t *iid, - uint8_t *addr) -{ - assert(netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR); - switch (netif->device_type) { -#if defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) || \ - defined(MODULE_NORDIC_SOFTDEVICE_BLE) - case NETDEV_TYPE_ETHERNET: - case NETDEV_TYPE_ESP_NOW: - case NETDEV_TYPE_BLE: - eui48_from_ipv6_iid((eui48_t *)addr, iid); - return sizeof(eui48_t); -#endif /* defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) */ -#if defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) - case NETDEV_TYPE_IEEE802154: - /* assume address was based on EUI-64 - * (see https://tools.ietf.org/html/rfc6775#section-5.2) */ - memcpy(addr, iid, sizeof(eui64_t)); - addr[0] ^= 0x02; - return sizeof(eui64_t); -#endif /* defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) */ -#ifdef MODULE_NRFMIN - case NETDEV_TYPE_NRFMIN: - addr[0] = iid->uint8[6]; - addr[1] = iid->uint8[7]; - return sizeof(uint16_t); -#endif /* MODULE_NETDEV_IEEE802154 */ -#ifdef MODULE_CC110X - case NETDEV_TYPE_CC110X: - addr[0] = iid->uint8[7]; - return sizeof(uint8_t); -#endif /* MODULE_CC110X */ - default: - (void)iid; - (void)addr; -#ifdef DEVELHELP - LOG_ERROR("gnrc_netif: can't convert IID to hardware address " - "on interface %u\n", netif->pid); -#endif /* DEVELHELP */ - assert(false); - break; - } - return -ENOTSUP; -} - -int gnrc_netif_ndp_addr_len_from_l2ao(gnrc_netif_t *netif, - const ndp_opt_t *opt) -{ - assert(netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR); - switch (netif->device_type) { -#ifdef MODULE_CC110X - case NETDEV_TYPE_CC110X: - (void)opt; - return sizeof(uint8_t); -#endif /* MODULE_CC110X */ -#if defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) || \ - defined(MODULE_NORDIC_SOFTDEVICE_BLE) - case NETDEV_TYPE_ETHERNET: - case NETDEV_TYPE_ESP_NOW: - case NETDEV_TYPE_BLE: - /* see https://tools.ietf.org/html/rfc2464#section-6*/ - if (opt->len == 1U) { - return ETHERNET_ADDR_LEN; - } - else { - return -EINVAL; - } -#endif /* defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) */ -#ifdef MODULE_NRFMIN - case NETDEV_TYPE_NRFMIN: - (void)opt; - return sizeof(uint16_t); -#endif /* MODULE_NRFMIN */ -#if defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) - case NETDEV_TYPE_IEEE802154: - /* see https://tools.ietf.org/html/rfc4944#section-8 */ - switch (opt->len) { - case 1U: - return IEEE802154_SHORT_ADDRESS_LEN; - case 2U: - return IEEE802154_LONG_ADDRESS_LEN; - default: - return -EINVAL; - } -#endif /* defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) */ - default: - (void)opt; -#ifdef DEVELHELP - LOG_ERROR("gnrc_netif: can't get address length from NDP link-layer " - "address option on interface %u\n", netif->pid); -#endif - assert(false); - break; - } - return -ENOTSUP; -} #endif /* MODULE_GNRC_IPV6 */ /** @} */