mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
145 lines
4.8 KiB
C
145 lines
4.8 KiB
C
/*
|
|
* Copyright (C) 2018 Freie Universität Berlin
|
|
*
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
* directory for more details.
|
|
*/
|
|
|
|
/**
|
|
* @{
|
|
*
|
|
* @file
|
|
* @author Martine Lenders <m.lenders@fu-berlin.de>
|
|
*/
|
|
|
|
#include "log.h"
|
|
#include "net/arp.h"
|
|
#include "net/dhcpv6.h"
|
|
#include "net/gnrc/ipv6/nib/pl.h"
|
|
#include "net/gnrc/netif.h"
|
|
#include "net/gnrc/rpl.h"
|
|
#include "net/sock.h"
|
|
#include "timex.h"
|
|
|
|
#include "net/dhcpv6/client.h"
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
#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)
|
|
{
|
|
gnrc_netif_t *netif;
|
|
uint8_t *l2addr = ((uint8_t *)(duid)) + sizeof(dhcpv6_duid_l2_t);
|
|
int res;
|
|
|
|
duid->type = byteorder_htons(DHCPV6_DUID_TYPE_L2);
|
|
/* TODO make GNRC-independent */
|
|
if (iface == SOCK_ADDR_ANY_NETIF) {
|
|
netif = gnrc_netif_iter(NULL);
|
|
}
|
|
else {
|
|
netif = gnrc_netif_get_by_pid(iface);
|
|
}
|
|
assert(netif != NULL);
|
|
if ((res = gnrc_netapi_get(netif->pid, NETOPT_ADDRESS_LONG, 0,
|
|
l2addr, GNRC_NETIF_L2ADDR_MAXLEN)) > 0) {
|
|
duid->l2type = byteorder_htons(ARP_HWTYPE_EUI64);
|
|
}
|
|
else {
|
|
switch (netif->device_type) {
|
|
case NETDEV_TYPE_ETHERNET:
|
|
case NETDEV_TYPE_BLE:
|
|
case NETDEV_TYPE_ESP_NOW:
|
|
if ((res = gnrc_netapi_get(netif->pid,
|
|
NETOPT_ADDRESS,
|
|
0, l2addr,
|
|
GNRC_NETIF_L2ADDR_MAXLEN)) > 0) {
|
|
duid->l2type = byteorder_htons(ARP_HWTYPE_ETHERNET);
|
|
break;
|
|
}
|
|
/* intentionally falls through */
|
|
default:
|
|
LOG_ERROR("DHCPv6 client: Link-layer type of interface %u not supported "
|
|
"for DUID creation\n", netif->pid);
|
|
return 0;
|
|
}
|
|
}
|
|
return (uint8_t)res + sizeof(dhcpv6_duid_l2_t);
|
|
}
|
|
|
|
void dhcpv6_client_conf_prefix(unsigned iface, const ipv6_addr_t *pfx,
|
|
unsigned pfx_len, uint32_t valid,
|
|
uint32_t pref)
|
|
{
|
|
gnrc_netif_t *netif = gnrc_netif_get_by_pid(iface);
|
|
eui64_t iid;
|
|
ipv6_addr_t addr;
|
|
|
|
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 defined(MODULE_GNRC_IPV6_NIB) && GNRC_IPV6_NIB_CONF_6LBR && \
|
|
GNRC_IPV6_NIB_CONF_MULTIHOP_P6C
|
|
gnrc_ipv6_nib_abr_add(&addr);
|
|
#endif
|
|
#ifdef MODULE_GNRC_RPL
|
|
gnrc_rpl_init(netif->pid);
|
|
gnrc_rpl_instance_t *inst = gnrc_rpl_instance_get(GNRC_RPL_DEFAULT_INSTANCE);
|
|
if (inst) {
|
|
gnrc_rpl_instance_remove(inst);
|
|
}
|
|
gnrc_rpl_root_init(GNRC_RPL_DEFAULT_INSTANCE, &addr, false, false);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
uint32_t dhcpv6_client_prefix_valid_until(unsigned netif,
|
|
const ipv6_addr_t *pfx,
|
|
unsigned pfx_len)
|
|
{
|
|
gnrc_ipv6_nib_pl_t ple;
|
|
void *state = NULL;
|
|
uint32_t max_valid = 0;
|
|
|
|
while (gnrc_ipv6_nib_pl_iter(netif, &state, &ple)) {
|
|
if ((ple.pfx_len == pfx_len) &&
|
|
((ple.valid_until / MS_PER_SEC) > max_valid) &&
|
|
(ipv6_addr_match_prefix(&ple.pfx,
|
|
pfx) >= ple.pfx_len)) {
|
|
max_valid = ple.valid_until / MS_PER_SEC;
|
|
}
|
|
}
|
|
return max_valid;
|
|
}
|
|
|
|
/** @} */
|