Skip to content

Commit

Permalink
gnrc_ipv6_nib: add neighbor cache component
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Oct 6, 2017
1 parent 0318700 commit 4df88c8
Show file tree
Hide file tree
Showing 7 changed files with 474 additions and 14 deletions.
1 change: 1 addition & 0 deletions Makefile.dep
Expand Up @@ -316,6 +316,7 @@ ifneq (,$(filter gnrc_ipv6_nc,$(USEMODULE)))
endif

ifneq (,$(filter gnrc_ipv6_nib,$(USEMODULE)))
USEMODULE += evtimer
USEMODULE += ipv6_addr
USEMODULE += random
endif
Expand Down
2 changes: 2 additions & 0 deletions sys/include/net/gnrc/ipv6.h
Expand Up @@ -36,7 +36,9 @@
#include "net/ipv6.h"
#include "net/gnrc/ipv6/ext.h"
#include "net/gnrc/ipv6/hdr.h"
#ifndef MODULE_GNRC_IPV6_NIB
#include "net/gnrc/ipv6/nc.h"
#endif
#include "net/gnrc/ipv6/netif.h"

#ifdef MODULE_FIB
Expand Down
2 changes: 2 additions & 0 deletions sys/include/net/gnrc/ipv6/nib.h
Expand Up @@ -22,6 +22,8 @@
#ifndef NET_GNRC_IPV6_NIB_H
#define NET_GNRC_IPV6_NIB_H

#include "net/gnrc/ipv6/nib/nc.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
198 changes: 189 additions & 9 deletions sys/include/net/gnrc/ipv6/nib/nc.h
Expand Up @@ -47,22 +47,22 @@ extern "C" {
#define GNRC_IPV6_NIB_NC_INFO_NUD_STATE_MASK (0x0007)

/**
* @brief not managed by NUD
* @brief Not managed by NUD
*/
#define GNRC_IPV6_NIB_NC_INFO_NUD_STATE_UNMANAGED (0x0000)

/**
* @brief entry is not reachable
* @brief Entry is not reachable
*/
#define GNRC_IPV6_NIB_NC_INFO_NUD_STATE_UNREACHABLE (0x0001)

/**
* @brief address resolution is currently performed
* @brief Address resolution is currently performed
*/
#define GNRC_IPV6_NIB_NC_INFO_NUD_STATE_INCOMPLETE (0x0002)

/**
* @brief address might not be reachable
* @brief Address might not be reachable
*/
#define GNRC_IPV6_NIB_NC_INFO_NUD_STATE_STALE (0x0003)

Expand All @@ -77,7 +77,7 @@ extern "C" {
#define GNRC_IPV6_NIB_NC_INFO_NUD_STATE_PROBE (0x0005)

/**
* @brief entry is reachable
* @brief Entry is reachable
*/
#define GNRC_IPV6_NIB_NC_INFO_NUD_STATE_REACHABLE (0x0006)

Expand Down Expand Up @@ -111,21 +111,201 @@ extern "C" {
#define GNRC_IPV6_NIB_NC_INFO_AR_STATE_MASK (0x0600)

/**
* @brief not managed by 6Lo-AR (address can be removed when memory is low
* @brief Shift position of address registration states
*/
#define GNRC_IPV6_NIB_NC_INFO_AR_STATE_POS (9)

/**
* @brief Not managed by 6Lo-AR (address can be removed when memory is low
*/
#define GNRC_IPV6_NIB_NC_INFO_AR_STATE_GC (0x0000)

/**
* @brief address registration still pending at upstream router
* @brief Address registration still pending at upstream router
*/
#define GNRC_IPV6_NIB_NC_INFO_AR_STATE_TENTATIVE (0x0200)

/**
* @brief address is registered
* @brief Address is registered
*/
#define GNRC_IPV6_NIB_NC_INFO_AR_STATE_REGISTERED (0x0600)
#define GNRC_IPV6_NIB_NC_INFO_AR_STATE_REGISTERED (0x0400)

/**
* @brief Address was added manually
*/
#define GNRC_IPV6_NIB_NC_INFO_AR_STATE_MANUAL (0x0600)
/** @} */

/**
* @brief Neighbor cache entry view on NIB
*/
typedef struct {
ipv6_addr_t ipv6; /**< Neighbor's IPv6 address */
/**
* @brief Neighbor's link-layer address
*/
uint8_t l2addr[GNRC_IPV6_NIB_L2ADDR_MAX_LEN];
/**
* @brief Neighbor information as defined in
* @ref net_gnrc_ipv6_nib_nc_info "info values"
*/
uint16_t info;
uint8_t l2addr_len; /**< Length of gnrc_ipv6_nib_nc_t::l2addr in bytes */
} gnrc_ipv6_nib_nc_t;

/**
* @brief Gets neighbor unreachability state from entry
*
* @param[in] entry A neighbor cache entry.
*
* @return The neighbor unreachability state of @p entry.
*/
static inline unsigned gnrc_ipv6_nib_nc_get_nud_state(const gnrc_ipv6_nib_nc_t *entry)
{
return (entry->info & GNRC_IPV6_NIB_NC_INFO_NUD_STATE_MASK);
}

/**
* @brief Gets router flag of a neighbor.
*
* @param[in] entry A neighbor cache entry.
*
* @return true, if @p entry is a router.
* @return false, if @p entry is not a router.
*/
static inline bool gnrc_ipv6_nib_nc_is_router(const gnrc_ipv6_nib_nc_t *entry)
{
return (entry->info & GNRC_IPV6_NIB_NC_INFO_IS_ROUTER);
}

/**
* @brief Gets interface from entry
*
* @param[in] entry A neighbor cache entry
*
* @return The interface identifier of @p entry.
* @return 0 if no interface is identified for @p entry.
*/
static inline unsigned gnrc_ipv6_nib_nc_get_iface(const gnrc_ipv6_nib_nc_t *entry)
{
return (entry->info & GNRC_IPV6_NIB_NC_INFO_IFACE_MASK) >>
GNRC_IPV6_NIB_NC_INFO_IFACE_POS;
}

/**
* @brief Gets address registration state of an entry
*
* @param[in] entry A neighbor cache entry
*
* @return The address registration state of @p entry.
*/
static inline unsigned gnrc_ipv6_nib_nc_get_ar_state(const gnrc_ipv6_nib_nc_t *entry)
{
return (entry->info & GNRC_IPV6_NIB_NC_INFO_AR_STATE_MASK);
}

/**
* @brief Adds an unmanaged neighbor entry to NIB
*
* @pre `(ipv6 != NULL) && (l2addr != NULL)`
* @pre `l2addr_len <= GNRC_IPV6_NIB_L2ADDR_MAX_LEN`
* @pre `(iface > KERNEL_PID_UNDEF) && (iface <= KERNEL_PID_LAST)`
*
* @param[in] ipv6 The neighbor's IPv6 address.
* @param[in] iface The interface to the neighbor.
* @param[in] l2addr The neighbor's L2 address.
* @param[in] l2addr_len Length of @p l2addr.
*
* A neighbor cache entry created this way is marked as persistent.
* Also, a non-persistent neighbor or destination cache entry already in the
* NIB might be removed to make room for the new entry.
* If an entry pointing to the same IPv6 address as @p ipv6 exists already it
* will be overwritten and marked as unmanaged.
*
* If @ref GNRC_IPV6_NIB_CONF_ARSM != 0 @p l2addr and @p l2addr_len won't be set.
*
* @return 0 on success.
* @return -ENOMEM, if no space is left in neighbor cache.
*/
int gnrc_ipv6_nib_nc_set(const ipv6_addr_t *ipv6, unsigned iface,
const uint8_t *l2addr, size_t l2addr_len);

/**
* @brief Deletes neighbor with address @p ipv6 from NIB
*
* @pre `ipv6 != NULL`
*
* @param[in] ipv6 The neighbor's IPv6 address.
*
* If the @p ipv6 can't be found for a neighbor in the NIB nothing happens.
*/
void gnrc_ipv6_nib_nc_del(const ipv6_addr_t *ipv6);

/**
* @brief Mark neighbor with address @p ipv6 as reachable
*
* @pre `ipv6 != NULL`
*
* @param[in] ipv6 A neighbor's IPv6 address. May not be NULL.
*
* This function shall be called if an upper layer gets reachability
* confirmation via its own means (e.g. a TCP connection build-up or
* confirmation). Unmanaged neighbor cache entries (i.e. entries created using
* @ref gnrc_ipv6_nib_nc_set()) or entries whose next-hop are not yet in the
* neighbor cache are ignored.
*
* Entries in state @ref GNRC_IPV6_NIB_NC_INFO_NUD_STATE_UNMANAGED are not
* affected by this, since they are assumed to always be reachable and kept out
* of the NUD state-machine
*/
void gnrc_ipv6_nib_nc_mark_reachable(const ipv6_addr_t *ipv6);

/**
* @brief Iterates over all neighbor cache entries in the NIB
*
* @pre `(state != NULL) && (nce != NULL)`
*
* @param[in] iface Restrict iteration to entries on this interface.
* 0 for any interface.
* @param[in,out] state Iteration state of the neighbor cache. Must point to
* a NULL pointer to start iteration.
* @param[out] nce The next neighbor cache entry.
*
* Usage example:
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c}
* #include "net/gnrc/ipv6/nib/nc.h"
*
* int main(void) {
* void *state = NULL;
* gnrc_ipv6_nib_nc_t nce;
*
* puts("My neighbors:");
* while (gnrc_ipv6_nib_nc_iter(0, &state, &nce)) {
* gnrc_ipv6_nib_nc_print(&nce);
* }
* return 0;
* }
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* @note The list may change during iteration, but no duplicate of already
* traversed entries must be returned.
*
* @return true, if iteration can be continued.
* @return false, if @p nce is the last neighbor cache entry in the NIB.
*/
bool gnrc_ipv6_nib_nc_iter(unsigned iface, void **state,
gnrc_ipv6_nib_nc_t *nce);

/**
* @brief Prints a neighbor cache entry
*
* @pre `nce != NULL`
*
* @param[in] nce A neighbor cache entry.
*/
void gnrc_ipv6_nib_nc_print(gnrc_ipv6_nib_nc_t *nce);

#ifdef __cplusplus
}
#endif
Expand Down
70 changes: 65 additions & 5 deletions sys/net/gnrc/network_layer/ipv6/nib/_nib-internal.c
Expand Up @@ -41,6 +41,7 @@ static char addr_str[IPV6_ADDR_MAX_STR_LEN];
#endif

mutex_t _nib_mutex = MUTEX_INIT;
evtimer_msg_t _nib_evtimer;

static void _override_node(const ipv6_addr_t *addr, unsigned iface,
_nib_onl_entry_t *node);
Expand All @@ -55,6 +56,7 @@ void _nib_init(void)
memset(_def_routers, 0, sizeof(_def_routers));
memset(_nis, 0, sizeof(_nis));
#endif
evtimer_init_msg(&_nib_evtimer);
/* TODO: load ABR information from persistent memory */
}

Expand Down Expand Up @@ -120,8 +122,9 @@ static inline _nib_onl_entry_t *_cache_out_onl_entry(const ipv6_addr_t *addr,
DEBUG("for (addr = %s, iface = %u)\n",
ipv6_addr_to_str(addr_str, addr, sizeof(addr_str)),
iface);
/* call _nib_nc_remove to remove timers from _evtimer */
_nib_nc_remove(tmp);
res = tmp;
res->mode = _EMPTY;
_override_node(addr, iface, res);
/* cstate masked in _nib_nc_add() already */
res->info |= cstate;
Expand Down Expand Up @@ -204,15 +207,19 @@ _nib_onl_entry_t *_nib_onl_get(const ipv6_addr_t *addr, unsigned iface)

void _nib_nc_set_reachable(_nib_onl_entry_t *node)
{
#if GNRC_IPV6_NIB_CONF_ARSM
_nib_iface_t *iface = _nib_iface_get(_nib_onl_get_if(node));

DEBUG("nib: set %s%%%u reachable (reachable time = %u)\n",
ipv6_addr_to_str(addr_str, &node->ipv6, sizeof(addr_str)),
_nib_onl_get_if(node), iface->reach_time);
node->info &= ~GNRC_IPV6_NIB_NC_INFO_NUD_STATE_MASK;
node->info |= GNRC_IPV6_NIB_NC_INFO_NUD_STATE_REACHABLE;
/* TODO add event for state change to STALE to event timer*/
(void)iface;
_evtimer_add(node, GNRC_IPV6_NIB_REACH_TIMEOUT, &node->nud_timeout,
iface->reach_time);
#else
(void)node;
#endif
}

void _nib_nc_remove(_nib_onl_entry_t *node)
Expand All @@ -221,10 +228,46 @@ void _nib_nc_remove(_nib_onl_entry_t *node)
ipv6_addr_to_str(addr_str, &node->ipv6, sizeof(addr_str)),
_nib_onl_get_if(node));
node->mode &= ~(_NC);
/* TODO: remove NC related timers */
#if GNRC_IPV6_NIB_CONF_ARSM
evtimer_del((evtimer_t *)&_nib_evtimer, &node->nud_timeout.event);
#endif
_nib_onl_clear(node);
}

static inline void _get_l2addr_from_ipv6(uint8_t *l2addr,
const ipv6_addr_t *ipv6)
{
memcpy(l2addr, &ipv6->u64[1], sizeof(uint64_t));
l2addr[0] ^= 0x02;
}

void _nib_nc_get(const _nib_onl_entry_t *node, gnrc_ipv6_nib_nc_t *nce)
{
assert((node != NULL) && (nce != NULL));
memcpy(&nce->ipv6, &node->ipv6, sizeof(nce->ipv6));
nce->info = node->info;
#if GNRC_IPV6_NIB_CONF_ARSM
#if GNRC_IPV6_NIB_CONF_6LN
if (ipv6_addr_is_link_local(&nce->ipv6)) {
gnrc_ipv6_netif_t *netif = gnrc_ipv6_netif_get(_nib_onl_get_if(node));
assert(netif != NULL);
if ((netif->flags & GNRC_IPV6_NETIF_FLAGS_SIXLOWPAN) &&
!(netif->flags & GNRC_IPV6_NETIF_FLAGS_ROUTER)) {
_get_l2addr_from_ipv6(nce->l2addr, &node->ipv6);
nce->l2addr_len = sizeof(uint64_t);
return;
}
}
#endif
nce->l2addr_len = node->l2addr_len;
memcpy(&nce->l2addr, &node->l2addr, node->l2addr_len);
#else
assert(ipv6_addr_is_link_local(&nce->ipv6));
_get_l2addr_from_ipv6(nce->l2addr, &node->ipv6);
nce->l2addr_len = sizeof(uint64_t);
#endif
}

_nib_dr_entry_t *_nib_drl_add(const ipv6_addr_t *router_addr, unsigned iface)
{
_nib_dr_entry_t *def_router = NULL;
Expand Down Expand Up @@ -312,7 +355,7 @@ _nib_dr_entry_t *_nib_drl_get_dr(void)
/* if there is already a default router selected or
* its reachability is not suspect */
if (!((_prime_def_router == NULL) ||
(_node_unreachable(_prime_def_router->next_hop)))) {
(_node_unreachable(_prime_def_router->next_hop)))) {
/* take it */
return _prime_def_router;
}
Expand Down Expand Up @@ -383,4 +426,21 @@ static inline bool _node_unreachable(_nib_onl_entry_t *node)
}
}

uint32_t _evtimer_lookup(const void *ctx, uint16_t type)
{
evtimer_msg_event_t *event = (evtimer_msg_event_t *)_nib_evtimer.events;
uint32_t offset = 0;

DEBUG("nib: lookup ctx = %p, type = %u\n", (void *)ctx, type);
while (event != NULL) {
offset += event->event.offset;
if ((event->msg.type == type) &&
((ctx == NULL) || (event->msg.content.ptr == ctx))) {
return offset;
}
event = (evtimer_msg_event_t *)event->event.next;
}
return UINT32_MAX;
}

/** @} */

0 comments on commit 4df88c8

Please sign in to comment.