Skip to content

Commit

Permalink
gnrc_netif: make _get_iid() just wrapper around _iid_from_addr()
Browse files Browse the repository at this point in the history
This way the IID generation gets much simplified and code duplication
is avoided since it removes GNRC's requirement for NETOPT_IPV6_IID.
  • Loading branch information
miri64 committed Jan 14, 2019
1 parent eead606 commit 7ae9056
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
43 changes: 31 additions & 12 deletions sys/include/net/gnrc/netif/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,6 @@ void gnrc_netif_ipv6_group_leave_internal(gnrc_netif_t *netif,
*/
int gnrc_netif_ipv6_group_idx(gnrc_netif_t *netif,
const ipv6_addr_t *addr);

/**
* @brief Gets interface identifier (IID) of an interface's link-layer address
*
* @param[in] netif the network interface
* @param[out] eui64 the IID
*
* @return 0, on success
* @return -ENOTSUP, if interface has no link-layer address or if
* gnrc_netif_t::device_type is not supported.
*/
int gnrc_netif_ipv6_get_iid(gnrc_netif_t *netif, eui64_t *eui64);
#endif /* MODULE_GNRC_IPV6 */

/**
Expand Down Expand Up @@ -466,9 +454,40 @@ int gnrc_netif_ipv6_iid_from_addr(const gnrc_netif_t *netif,
*/
int gnrc_netif_ipv6_iid_to_addr(const gnrc_netif_t *netif, const eui64_t *iid,
uint8_t *addr);

/**
* @brief Converts an interface IID of an interface's hardware address
*
* @param[in] netif The network interface @p iid came from
* @param[out] iid The IID based on gnrc_netif_t::device_type
*
* @note This wraps around @ref gnrc_netif_ipv6_iid_from_addr by using
* by using gnrc_netif_t::l2addr and gnrc_netif_t::l2addr_len of
* @p netif.
*
* @return `sizeof(eui64_t)` on success.
* @return `-ENOTSUP`, if interface has no link-layer address or if
* gnrc_netif_t::device_type is not supported.
* @return `-EINVAL`, when gnrc_netif_t::l2addr_len of @p netif is invalid for
* the gnrc_netif_t::device_type of @p netif.
*/
static inline int gnrc_netif_ipv6_get_iid(gnrc_netif_t *netif, eui64_t *iid)
{
#if GNRC_NETIF_L2ADDR_MAXLEN > 0
if (netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR) {
return gnrc_netif_ipv6_iid_from_addr(netif,
netif->l2addr, netif->l2addr_len,
iid);
}
#endif /* GNRC_NETIF_L2ADDR_MAXLEN > 0 */
(void)netif;
(void)iid;
return -ENOTSUP;
}
#else /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
#define gnrc_netif_ipv6_iid_from_addr(netif, addr, addr_len, iid) (-ENOTSUP)
#define gnrc_netif_ipv6_iid_to_addr(netif, iid, addr) (-ENOTSUP)
#define gnrc_netif_ipv6_get_iid(netif, iid) (-ENOTSUP)
#endif /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */

#ifdef __cplusplus
Expand Down
26 changes: 1 addition & 25 deletions sys/net/gnrc/netif/gnrc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ int gnrc_netif_get_from_netdev(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt)
break;
case NETOPT_IPV6_IID:
assert(opt->data_len >= sizeof(eui64_t));
if (gnrc_netif_ipv6_get_iid(netif, opt->data) == 0) {
res = sizeof(eui64_t);
}
res = gnrc_netif_ipv6_get_iid(netif, opt->data);
break;
case NETOPT_MAX_PACKET_SIZE:
if (opt->context == GNRC_NETTYPE_IPV6) {
Expand Down Expand Up @@ -807,28 +805,6 @@ int gnrc_netif_ipv6_group_idx(gnrc_netif_t *netif, const ipv6_addr_t *addr)
return idx;
}

int gnrc_netif_ipv6_get_iid(gnrc_netif_t *netif, eui64_t *eui64)
{
#if GNRC_NETIF_L2ADDR_MAXLEN > 0
if (netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR) {
/* the device driver abstraction should be able to provide us with the
* IPV6_IID, so we try this first */
int res = netif->dev->driver->get(netif->dev, NETOPT_IPV6_IID,
eui64, sizeof(eui64_t));
if (res == sizeof(eui64_t)) {
return 0;
}
res = gnrc_netif_ipv6_iid_from_addr(netif,
netif->l2addr, netif->l2addr_len,
eui64);
if (res > 0) {
return 0;
}
}
#endif /* GNRC_NETIF_L2ADDR_MAXLEN > 0 */
return -ENOTSUP;
}

static inline bool _addr_anycast(const gnrc_netif_t *netif, unsigned idx)
{
return (netif->ipv6.addrs_flags[idx] & GNRC_NETIF_IPV6_ADDRS_FLAGS_ANYCAST);
Expand Down
2 changes: 1 addition & 1 deletion sys/net/gnrc/network_layer/ipv6/nib/_nib-slaac.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static bool _try_addr_reconfiguration(gnrc_netif_t *netif)
eui64_t orig_iid;
bool remove_old = false, hwaddr_reconf;

if (gnrc_netif_ipv6_get_iid(netif, &orig_iid) == 0) {
if (gnrc_netif_ipv6_get_iid(netif, &orig_iid) > 0) {
remove_old = true;
}
/* seize netif to netif thread since _try_l2addr_reconfiguration uses
Expand Down
9 changes: 6 additions & 3 deletions tests/gnrc_netif/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,18 +651,21 @@ static void test_ipv6_get_iid(void)
eui64_t res;
uint16_t ieee802154_l2addr_len = 2U;

TEST_ASSERT_EQUAL_INT(0, gnrc_netif_ipv6_get_iid(ethernet_netif, &res));
TEST_ASSERT_EQUAL_INT(sizeof(eui64_t),
gnrc_netif_ipv6_get_iid(ethernet_netif, &res));
TEST_ASSERT_EQUAL_INT(0, memcmp(&res, &ethernet_ipv6_ll.u64[1],
sizeof(res)));
TEST_ASSERT_EQUAL_INT(0, gnrc_netif_ipv6_get_iid(ieee802154_netif, &res));
TEST_ASSERT_EQUAL_INT(sizeof(eui64_t),
gnrc_netif_ipv6_get_iid(ieee802154_netif, &res));
TEST_ASSERT_EQUAL_INT(0, memcmp(&res, &ieee802154_ipv6_ll_long.u64[1],
sizeof(res)));
TEST_ASSERT_EQUAL_INT(sizeof(ieee802154_l2addr_len),
gnrc_netapi_set(ieee802154_netif->pid,
NETOPT_SRC_LEN, 0,
&ieee802154_l2addr_len,
sizeof(ieee802154_l2addr_len)));
TEST_ASSERT_EQUAL_INT(0, gnrc_netif_ipv6_get_iid(ieee802154_netif, &res));
TEST_ASSERT_EQUAL_INT(sizeof(eui64_t),
gnrc_netif_ipv6_get_iid(ieee802154_netif, &res));
TEST_ASSERT_EQUAL_INT(0, memcmp(&res, &ieee802154_eui64_short, sizeof(res)));
/* reset to source length 8 */
ieee802154_l2addr_len = 8U;
Expand Down

0 comments on commit 7ae9056

Please sign in to comment.