2016-06-01 20:25:22 +08:00
|
|
|
/* nbr.c - Neighbor table management */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-06-01 20:25:22 +08:00
|
|
|
*/
|
|
|
|
|
2018-11-30 18:54:56 +08:00
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(net_nbr, CONFIG_NET_IPV6_NBR_CACHE_LOG_LEVEL);
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <net/net_core.h>
|
|
|
|
|
2016-07-06 17:36:53 +08:00
|
|
|
#include "net_private.h"
|
|
|
|
|
2016-06-01 20:25:22 +08:00
|
|
|
#include "nbr.h"
|
|
|
|
|
|
|
|
NET_NBR_LLADDR_INIT(net_neighbor_lladdr, CONFIG_NET_IPV6_MAX_NEIGHBORS);
|
|
|
|
|
2018-07-06 16:35:07 +08:00
|
|
|
#if defined(CONFIG_NET_IPV6_NBR_CACHE_LOG_LEVEL_DBG)
|
2016-09-20 18:14:00 +08:00
|
|
|
void net_nbr_unref_debug(struct net_nbr *nbr, const char *caller, int line)
|
|
|
|
#define net_nbr_unref(nbr) net_nbr_unref_debug(nbr, __func__, __LINE__)
|
|
|
|
#else
|
2016-06-01 20:25:22 +08:00
|
|
|
void net_nbr_unref(struct net_nbr *nbr)
|
2016-09-20 18:14:00 +08:00
|
|
|
#endif
|
2016-06-01 20:25:22 +08:00
|
|
|
{
|
2018-07-06 16:35:07 +08:00
|
|
|
#if defined(CONFIG_NET_IPV6_NBR_CACHE_LOG_LEVEL_DBG)
|
2016-09-20 18:14:00 +08:00
|
|
|
NET_DBG("nbr %p ref %u (%s():%d)", nbr, nbr->ref - 1, caller, line);
|
|
|
|
#else
|
2016-06-01 20:25:22 +08:00
|
|
|
NET_DBG("nbr %p ref %u", nbr, nbr->ref - 1);
|
2016-09-20 18:14:00 +08:00
|
|
|
#endif
|
2016-06-01 20:25:22 +08:00
|
|
|
if (--nbr->ref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbr->remove) {
|
|
|
|
nbr->remove(nbr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:35:07 +08:00
|
|
|
#if defined(CONFIG_NET_IPV6_NBR_CACHE_LOG_LEVEL_DBG)
|
2016-09-20 18:14:00 +08:00
|
|
|
struct net_nbr *net_nbr_ref_debug(struct net_nbr *nbr, const char *caller,
|
|
|
|
int line)
|
|
|
|
#else
|
2016-06-01 20:25:22 +08:00
|
|
|
struct net_nbr *net_nbr_ref(struct net_nbr *nbr)
|
2016-09-20 18:14:00 +08:00
|
|
|
#endif
|
2016-06-01 20:25:22 +08:00
|
|
|
{
|
2018-07-06 16:35:07 +08:00
|
|
|
#if defined(CONFIG_NET_IPV6_NBR_CACHE_LOG_LEVEL_DBG)
|
2016-09-20 18:14:00 +08:00
|
|
|
NET_DBG("nbr %p ref %u (%s():%d)", nbr, nbr->ref + 1, caller, line);
|
|
|
|
#else
|
2016-06-01 20:25:22 +08:00
|
|
|
NET_DBG("nbr %p ref %u", nbr, nbr->ref + 1);
|
2016-09-20 18:14:00 +08:00
|
|
|
#endif
|
2016-06-01 20:25:22 +08:00
|
|
|
nbr->ref++;
|
|
|
|
|
|
|
|
return nbr;
|
|
|
|
}
|
|
|
|
|
2016-07-07 22:08:37 +08:00
|
|
|
static inline struct net_nbr *get_nbr(struct net_nbr *start, int idx)
|
|
|
|
{
|
|
|
|
NET_ASSERT(idx < CONFIG_NET_IPV6_MAX_NEIGHBORS);
|
|
|
|
|
2018-09-14 20:24:09 +08:00
|
|
|
return (struct net_nbr *)((u8_t *)start +
|
2017-03-31 19:10:55 +08:00
|
|
|
((sizeof(struct net_nbr) +
|
|
|
|
start->size + start->extra_data_size) * idx));
|
2016-07-07 22:08:37 +08:00
|
|
|
}
|
|
|
|
|
2016-06-01 20:25:22 +08:00
|
|
|
struct net_nbr *net_nbr_get(struct net_nbr_table *table)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-08-10 21:51:20 +08:00
|
|
|
for (i = 0; i < table->nbr_count; i++) {
|
2016-07-07 22:08:37 +08:00
|
|
|
struct net_nbr *nbr = get_nbr(table->nbr, i);
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
if (!nbr->ref) {
|
2016-07-07 22:08:37 +08:00
|
|
|
nbr->data = nbr->__nbr;
|
|
|
|
|
2016-06-01 20:25:22 +08:00
|
|
|
return net_nbr_ref(nbr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_nbr_link(struct net_nbr *nbr, struct net_if *iface,
|
|
|
|
struct net_linkaddr *lladdr)
|
|
|
|
{
|
2016-10-05 21:14:43 +08:00
|
|
|
int i, avail = -1;
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
if (nbr->idx != NET_NBR_LLADDR_UNKNOWN) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_NET_IPV6_MAX_NEIGHBORS; i++) {
|
2016-10-05 21:14:43 +08:00
|
|
|
if (avail < 0 && !net_neighbor_lladdr[i].ref) {
|
|
|
|
avail = i;
|
2016-06-01 20:25:22 +08:00
|
|
|
}
|
|
|
|
|
2016-10-05 21:14:43 +08:00
|
|
|
if (net_neighbor_lladdr[i].ref &&
|
|
|
|
!memcmp(lladdr->addr,
|
|
|
|
net_neighbor_lladdr[i].lladdr.addr,
|
|
|
|
lladdr->len)) {
|
|
|
|
/* We found same lladdr in nbr cache so just
|
|
|
|
* increase the ref count.
|
|
|
|
*/
|
|
|
|
net_neighbor_lladdr[i].ref++;
|
2016-06-01 20:25:22 +08:00
|
|
|
|
2016-10-05 21:14:43 +08:00
|
|
|
nbr->idx = i;
|
|
|
|
nbr->iface = iface;
|
2016-06-01 20:25:22 +08:00
|
|
|
|
2016-10-05 21:14:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-06-01 20:25:22 +08:00
|
|
|
|
2016-10-05 21:14:43 +08:00
|
|
|
if (avail < 0) {
|
|
|
|
return -ENOENT;
|
2016-06-01 20:25:22 +08:00
|
|
|
}
|
|
|
|
|
2016-10-05 21:14:43 +08:00
|
|
|
/* There was no existing entry in the lladdr cache,
|
|
|
|
* so allocate one for this lladdr.
|
|
|
|
*/
|
|
|
|
net_neighbor_lladdr[avail].ref++;
|
|
|
|
nbr->idx = avail;
|
|
|
|
|
2017-01-12 07:18:05 +08:00
|
|
|
net_linkaddr_set(&net_neighbor_lladdr[avail].lladdr, lladdr->addr,
|
|
|
|
lladdr->len);
|
2016-10-05 21:14:43 +08:00
|
|
|
net_neighbor_lladdr[avail].lladdr.len = lladdr->len;
|
|
|
|
|
|
|
|
nbr->iface = iface;
|
|
|
|
|
|
|
|
return 0;
|
2016-06-01 20:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int net_nbr_unlink(struct net_nbr *nbr, struct net_linkaddr *lladdr)
|
|
|
|
{
|
2016-12-21 03:15:00 +08:00
|
|
|
ARG_UNUSED(lladdr);
|
|
|
|
|
2016-06-01 20:25:22 +08:00
|
|
|
if (nbr->idx == NET_NBR_LLADDR_UNKNOWN) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NET_ASSERT(nbr->idx < CONFIG_NET_IPV6_MAX_NEIGHBORS);
|
|
|
|
NET_ASSERT(net_neighbor_lladdr[nbr->idx].ref > 0);
|
|
|
|
|
|
|
|
net_neighbor_lladdr[nbr->idx].ref--;
|
|
|
|
|
|
|
|
if (!net_neighbor_lladdr[nbr->idx].ref) {
|
2018-09-12 10:09:03 +08:00
|
|
|
(void)memset(net_neighbor_lladdr[nbr->idx].lladdr.addr, 0,
|
|
|
|
sizeof(net_neighbor_lladdr[nbr->idx].lladdr.addr));
|
2016-06-01 20:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nbr->idx = NET_NBR_LLADDR_UNKNOWN;
|
|
|
|
nbr->iface = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct net_nbr *net_nbr_lookup(struct net_nbr_table *table,
|
|
|
|
struct net_if *iface,
|
|
|
|
struct net_linkaddr *lladdr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-08-10 21:51:20 +08:00
|
|
|
for (i = 0; i < table->nbr_count; i++) {
|
2016-07-07 22:08:37 +08:00
|
|
|
struct net_nbr *nbr = get_nbr(table->nbr, i);
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
if (nbr->ref && nbr->iface == iface &&
|
|
|
|
net_neighbor_lladdr[nbr->idx].ref &&
|
|
|
|
!memcmp(net_neighbor_lladdr[nbr->idx].lladdr.addr,
|
|
|
|
lladdr->addr, lladdr->len)) {
|
|
|
|
return nbr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:27:50 +08:00
|
|
|
struct net_linkaddr_storage *net_nbr_get_lladdr(u8_t idx)
|
2016-06-01 20:25:22 +08:00
|
|
|
{
|
2016-09-19 20:03:56 +08:00
|
|
|
NET_ASSERT_INFO(idx < CONFIG_NET_IPV6_MAX_NEIGHBORS,
|
|
|
|
"idx %d >= max %d", idx,
|
|
|
|
CONFIG_NET_IPV6_MAX_NEIGHBORS);
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
return &net_neighbor_lladdr[idx].lladdr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_nbr_clear_table(struct net_nbr_table *table)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-08-10 21:51:20 +08:00
|
|
|
for (i = 0; i < table->nbr_count; i++) {
|
2016-07-07 22:08:37 +08:00
|
|
|
struct net_nbr *nbr = get_nbr(table->nbr, i);
|
2016-06-01 20:25:22 +08:00
|
|
|
struct net_linkaddr lladdr = {
|
|
|
|
.addr = net_neighbor_lladdr[i].lladdr.addr,
|
|
|
|
.len = net_neighbor_lladdr[i].lladdr.len
|
|
|
|
};
|
|
|
|
|
|
|
|
net_nbr_unlink(nbr, &lladdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (table->clear) {
|
|
|
|
table->clear(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 17:36:53 +08:00
|
|
|
void net_nbr_print(struct net_nbr_table *table)
|
|
|
|
{
|
2018-11-30 18:54:56 +08:00
|
|
|
if (CONFIG_NET_IPV6_NBR_CACHE_LOG_LEVEL >= LOG_LEVEL_DBG) {
|
2018-07-06 16:35:07 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < table->nbr_count; i++) {
|
|
|
|
struct net_nbr *nbr = get_nbr(table->nbr, i);
|
|
|
|
|
|
|
|
if (!nbr->ref) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NET_DBG("[%d] nbr %p data %p ref %d iface %p idx %d "
|
|
|
|
"ll %s",
|
|
|
|
i, nbr, nbr->data, nbr->ref, nbr->iface,
|
|
|
|
nbr->idx,
|
|
|
|
nbr->idx == NET_NBR_LLADDR_UNKNOWN ?
|
|
|
|
"<unknown>" :
|
2018-10-02 19:57:55 +08:00
|
|
|
log_strdup(net_sprint_ll_addr(
|
|
|
|
net_neighbor_lladdr[nbr->idx].lladdr.addr,
|
|
|
|
net_neighbor_lladdr[nbr->idx].lladdr.len)));
|
2016-07-06 17:36:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|