mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
gnrc_netif: add gnrc_netif_ipv6_add_prefix()
This commit is contained in:
parent
184501b242
commit
b90c6ed373
@ -716,6 +716,26 @@ static inline int gnrc_netif_ipv6_group_to_l2_group(gnrc_netif_t *netif,
|
||||
return l2util_ipv6_group_to_l2_group(netif->device_type, ipv6_group,
|
||||
l2_group);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configures a prefix on a network interface.
|
||||
*
|
||||
* If the interface is a 6LoWPAN interface, this will also
|
||||
* take care of setting up a compression context.
|
||||
*
|
||||
* @param[in] netif Network interface the prefix should be added to
|
||||
* @param[in] pfx Prefix to configure
|
||||
* @param[in] pfx_len Length of @p pfx in bits
|
||||
* @param[in] valid Valid lifetime of the prefix in seconds
|
||||
* @param[in] pref Preferred lifetime of the prefix in seconds
|
||||
*
|
||||
* @return >= 0, on success
|
||||
* @return -ENOMEM, when no space for new addresses (or its solicited nodes
|
||||
* multicast address) is left on the interface
|
||||
*/
|
||||
int gnrc_netif_ipv6_add_prefix(gnrc_netif_t *netif,
|
||||
const ipv6_addr_t *pfx, uint8_t pfx_len,
|
||||
uint32_t valid, uint32_t pref);
|
||||
#else /* IS_USED(MODULE_GNRC_NETIF_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)
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "net/ipv6/addr.h"
|
||||
#include "timex.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -113,7 +114,6 @@ gnrc_sixlowpan_ctx_t *gnrc_sixlowpan_ctx_update(uint8_t id, const ipv6_addr_t *p
|
||||
uint8_t prefix_len, uint16_t ltime,
|
||||
bool comp);
|
||||
|
||||
#ifdef MODULE_GNRC_SIXLOWPAN_CTX
|
||||
/**
|
||||
* @brief Removes context.
|
||||
*
|
||||
@ -121,9 +121,72 @@ gnrc_sixlowpan_ctx_t *gnrc_sixlowpan_ctx_update(uint8_t id, const ipv6_addr_t *p
|
||||
*/
|
||||
static inline void gnrc_sixlowpan_ctx_remove(uint8_t id)
|
||||
{
|
||||
gnrc_sixlowpan_ctx_lookup_id(id)->prefix_len = 0;
|
||||
if (IS_USED(MODULE_GNRC_SIXLOWPAN_CTX)) {
|
||||
gnrc_sixlowpan_ctx_lookup_id(id)->prefix_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if a prefix matches a compression context
|
||||
*
|
||||
* @param[in] ctx The compression context
|
||||
* @param[in] prefix IPv6 prefix
|
||||
* @param[in] prefix_len Length of the IPv6 prefix
|
||||
*
|
||||
* @return true if the prefix matches the compression context.
|
||||
*/
|
||||
static inline bool gnrc_sixlowpan_ctx_match(const gnrc_sixlowpan_ctx_t *ctx,
|
||||
const ipv6_addr_t *prefix, uint8_t prefix_len)
|
||||
{
|
||||
return (ctx != NULL) &&
|
||||
(ctx->prefix_len == prefix_len) &&
|
||||
(ipv6_addr_match_prefix(&ctx->prefix, prefix) >= prefix_len);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create or update a compression context
|
||||
*
|
||||
* @param[in] prefix IPv6 prefix of the compression context
|
||||
* @param[in] prefix_len Length of the IPv6 prefix
|
||||
* @param[in] valid Lifetime of the prefix in seconds
|
||||
*
|
||||
* @return true if a new compression context was created or an existing context
|
||||
* was updated.
|
||||
* false if no new context could be added
|
||||
*/
|
||||
static inline bool gnrc_sixlowpan_ctx_update_6ctx(const ipv6_addr_t *prefix, uint8_t prefix_len,
|
||||
uint32_t valid)
|
||||
{
|
||||
if (!IS_USED(MODULE_GNRC_SIXLOWPAN_CTX)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
gnrc_sixlowpan_ctx_t *ctx = gnrc_sixlowpan_ctx_lookup_addr(prefix);
|
||||
uint8_t cid = 0;
|
||||
|
||||
if (!gnrc_sixlowpan_ctx_match(ctx, prefix, prefix_len)) {
|
||||
/* While the context is a prefix match, the defined prefix within the
|
||||
* context does not match => use new context */
|
||||
ctx = NULL;
|
||||
}
|
||||
else {
|
||||
cid = ctx->flags_id & GNRC_SIXLOWPAN_CTX_FLAGS_CID_MASK;
|
||||
}
|
||||
/* find first free context ID */
|
||||
if (ctx == NULL) {
|
||||
while (((ctx = gnrc_sixlowpan_ctx_lookup_id(cid)) != NULL) &&
|
||||
!gnrc_sixlowpan_ctx_match(ctx, prefix, prefix_len)) {
|
||||
cid++;
|
||||
}
|
||||
}
|
||||
if (cid < GNRC_SIXLOWPAN_CTX_SIZE) {
|
||||
return gnrc_sixlowpan_ctx_update(cid, (ipv6_addr_t *)prefix, prefix_len,
|
||||
valid / (60 * MS_PER_SEC),
|
||||
true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef TEST_SUITES
|
||||
/**
|
||||
|
@ -32,6 +32,7 @@
|
||||
#if IS_USED(MODULE_GNRC_NETIF_PKTQ)
|
||||
#include "net/gnrc/netif/pktq.h"
|
||||
#endif /* IS_USED(MODULE_GNRC_NETIF_PKTQ) */
|
||||
#include "net/gnrc/sixlowpan/ctx.h"
|
||||
#if IS_USED(MODULE_GNRC_SIXLOWPAN_FRAG_SFR)
|
||||
#include "net/gnrc/sixlowpan/frag/sfr.h"
|
||||
#endif /* IS_USED(MODULE_GNRC_SIXLOWPAN_FRAG_SFR) */
|
||||
@ -1235,6 +1236,68 @@ static ipv6_addr_t *_src_addr_selection(gnrc_netif_t *netif,
|
||||
return &netif->ipv6.addrs[idx];
|
||||
}
|
||||
}
|
||||
|
||||
int gnrc_netif_ipv6_add_prefix(gnrc_netif_t *netif,
|
||||
const ipv6_addr_t *pfx, uint8_t pfx_len,
|
||||
uint32_t valid, uint32_t pref)
|
||||
{
|
||||
int res;
|
||||
eui64_t iid;
|
||||
ipv6_addr_t addr;
|
||||
|
||||
assert(netif != NULL);
|
||||
DEBUG("gnrc_netif: (re-)configure prefix %s/%d\n",
|
||||
ipv6_addr_to_str(addr_str, pfx, sizeof(addr_str)), pfx_len);
|
||||
if (gnrc_netapi_get(netif->pid, NETOPT_IPV6_IID, 0, &iid,
|
||||
sizeof(eui64_t)) >= 0) {
|
||||
ipv6_addr_set_aiid(&addr, iid.uint8);
|
||||
}
|
||||
else {
|
||||
LOG_WARNING("gnrc_netif: cannot get IID of netif %u\n", netif->pid);
|
||||
return -ENODEV;
|
||||
}
|
||||
ipv6_addr_init_prefix(&addr, pfx, pfx_len);
|
||||
|
||||
/* add address as valid */
|
||||
res = gnrc_netif_ipv6_addr_add_internal(netif, &addr, pfx_len,
|
||||
GNRC_NETIF_IPV6_ADDRS_FLAGS_STATE_VALID);
|
||||
if (res < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* update lifetime */
|
||||
if (valid < UINT32_MAX) { /* UINT32_MAX means infinite lifetime */
|
||||
/* the valid lifetime is given in seconds, but the NIB's timers work
|
||||
* in microseconds, so we have to scale down to the smallest
|
||||
* possible value (UINT32_MAX - 1). */
|
||||
valid = (valid > (UINT32_MAX / MS_PER_SEC))
|
||||
? (UINT32_MAX - 1) : valid * MS_PER_SEC;
|
||||
}
|
||||
if (pref < UINT32_MAX) { /* UINT32_MAX means infinite lifetime */
|
||||
/* same treatment for pref */
|
||||
pref = (pref > (UINT32_MAX / MS_PER_SEC))
|
||||
? (UINT32_MAX - 1) : pref * MS_PER_SEC;
|
||||
}
|
||||
gnrc_ipv6_nib_pl_set(netif->pid, pfx, pfx_len, valid, pref);
|
||||
|
||||
/* configure 6LoWPAN specific options */
|
||||
if (IS_USED(MODULE_GNRC_IPV6_NIB) &&
|
||||
IS_ACTIVE(CONFIG_GNRC_IPV6_NIB_6LBR) &&
|
||||
IS_ACTIVE(CONFIG_GNRC_IPV6_NIB_MULTIHOP_P6C) &&
|
||||
gnrc_netif_is_6ln(netif)) {
|
||||
|
||||
/* configure compression context */
|
||||
if (gnrc_sixlowpan_ctx_update_6ctx(pfx, pfx_len, valid)) {
|
||||
DEBUG("gnrc_netif: add compression context for prefix %s/%u\n",
|
||||
ipv6_addr_to_str(addr_str, pfx, sizeof(addr_str)), pfx_len);
|
||||
}
|
||||
|
||||
(void)gnrc_ipv6_nib_abr_add(&addr);
|
||||
}
|
||||
|
||||
out:
|
||||
return res;
|
||||
}
|
||||
#endif /* IS_USED(MODULE_GNRC_NETIF_IPV6) */
|
||||
|
||||
static void _update_l2addr_from_dev(gnrc_netif_t *netif)
|
||||
|
Loading…
Reference in New Issue
Block a user