gnrc_dhcpv6_client: make use of gnrc_netif_ipv6_add_prefix()

This commit is contained in:
Benjamin Valentin 2021-07-22 17:00:21 +02:00 committed by Benjamin Valentin
parent d99972791a
commit a18f123ff1

View File

@ -31,8 +31,6 @@
#define ENABLE_DEBUG 0 #define ENABLE_DEBUG 0
#include "debug.h" #include "debug.h"
static char addr_str[IPV6_ADDR_MAX_STR_LEN];
unsigned dhcpv6_client_get_duid_l2(unsigned iface, dhcpv6_duid_l2_t *duid) unsigned dhcpv6_client_get_duid_l2(unsigned iface, dhcpv6_duid_l2_t *duid)
{ {
gnrc_netif_t *netif; gnrc_netif_t *netif;
@ -79,100 +77,14 @@ unsigned dhcpv6_client_get_duid_l2(unsigned iface, dhcpv6_duid_l2_t *duid)
return (uint8_t)res + sizeof(dhcpv6_duid_l2_t); return (uint8_t)res + sizeof(dhcpv6_duid_l2_t);
} }
static bool _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);
}
static void _update_6ctx(const ipv6_addr_t *prefix, uint8_t prefix_len)
{
gnrc_sixlowpan_ctx_t *ctx = gnrc_sixlowpan_ctx_lookup_addr(prefix);
uint8_t cid = 0;
if (!_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) &&
!_ctx_match(ctx, prefix, prefix_len)) {
cid++;
}
}
if (cid < GNRC_SIXLOWPAN_CTX_SIZE) {
DEBUG("DHCP client: add compression context %u for prefix %s/%u\n", cid,
ipv6_addr_to_str(addr_str, prefix, sizeof(addr_str)),
prefix_len);
gnrc_sixlowpan_ctx_update(cid, (ipv6_addr_t *)prefix, prefix_len,
CONFIG_GNRC_DHCPV6_CLIENT_6LBR_6LO_CTX_MIN,
true);
}
}
void dhcpv6_client_conf_prefix(unsigned iface, const ipv6_addr_t *pfx, void dhcpv6_client_conf_prefix(unsigned iface, const ipv6_addr_t *pfx,
unsigned pfx_len, uint32_t valid, unsigned pfx_len, uint32_t valid,
uint32_t pref) uint32_t pref)
{ {
gnrc_netif_t *netif = gnrc_netif_get_by_pid(iface); gnrc_netif_t *netif = gnrc_netif_get_by_pid(iface);
eui64_t iid; int idx = gnrc_netif_ipv6_add_prefix(netif, pfx, pfx_len, valid, pref);
ipv6_addr_t addr; if (idx >= 0) {
gnrc_rpl_configure_root(netif, &netif->ipv6.addrs[idx]);
assert(netif != NULL);
DEBUG("GNRC DHCPv6 client: (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 DHCPv6 client: cannot get IID of netif %u\n", netif->pid);
return;
}
ipv6_addr_init_prefix(&addr, pfx, pfx_len);
/* add address as tentative */
if (gnrc_netif_ipv6_addr_add(netif, &addr, pfx_len,
GNRC_NETIF_IPV6_ADDRS_FLAGS_STATE_TENTATIVE & 0x1) > 0) {
/* 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);
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)) {
if (IS_USED(MODULE_GNRC_SIXLOWPAN_CTX)) {
_update_6ctx(pfx, pfx_len);
}
(void)gnrc_ipv6_nib_abr_add(&addr);
}
if (IS_USED(MODULE_GNRC_RPL)) {
gnrc_rpl_init(netif->pid);
gnrc_rpl_instance_t *inst = gnrc_rpl_instance_get(
CONFIG_GNRC_RPL_DEFAULT_INSTANCE
);
if (inst) {
gnrc_rpl_instance_remove(inst);
}
gnrc_rpl_root_init(CONFIG_GNRC_RPL_DEFAULT_INSTANCE, &addr, false, false);
}
} }
} }