2013-07-03 19:32:26 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-02-15 19:02:07 +01:00
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-07-03 19:32:26 +02:00
|
|
|
/**
|
|
|
|
* \file
|
2015-02-15 19:02:07 +01:00
|
|
|
* IPv6 Neighbor cache (link-layer/IPv6 address mapping)
|
2013-07-03 19:32:26 +02:00
|
|
|
* \author Mathilde Durvy <mdurvy@cisco.com>
|
|
|
|
* \author Julien Abeille <jabeille@cisco.com>
|
|
|
|
* \author Simon Duquennoy <simonduq@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "lib/list.h"
|
2013-12-12 23:58:52 +01:00
|
|
|
#include "net/linkaddr.h"
|
2013-07-03 19:32:26 +02:00
|
|
|
#include "net/packetbuf.h"
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ipv6/uip-ds6-nbr.h"
|
2013-07-03 19:32:26 +02:00
|
|
|
|
|
|
|
#define DEBUG DEBUG_NONE
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2013-07-03 19:32:26 +02:00
|
|
|
|
|
|
|
#ifdef UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED
|
|
|
|
#define NEIGHBOR_STATE_CHANGED(n) UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED(n)
|
|
|
|
void NEIGHBOR_STATE_CHANGED(uip_ds6_nbr_t *n);
|
|
|
|
#else
|
|
|
|
#define NEIGHBOR_STATE_CHANGED(n)
|
|
|
|
#endif /* UIP_DS6_CONF_NEIGHBOR_STATE_CHANGED */
|
|
|
|
|
2013-07-29 21:50:33 +02:00
|
|
|
#ifdef UIP_CONF_DS6_LINK_NEIGHBOR_CALLBACK
|
|
|
|
#define LINK_NEIGHBOR_CALLBACK(addr, status, numtx) UIP_CONF_DS6_LINK_NEIGHBOR_CALLBACK(addr, status, numtx)
|
2013-12-12 23:58:52 +01:00
|
|
|
void LINK_NEIGHBOR_CALLBACK(const linkaddr_t *addr, int status, int numtx);
|
2013-07-29 21:50:33 +02:00
|
|
|
#else
|
|
|
|
#define LINK_NEIGHBOR_CALLBACK(addr, status, numtx)
|
|
|
|
#endif /* UIP_CONF_DS6_LINK_NEIGHBOR_CALLBACK */
|
|
|
|
|
2013-07-29 18:49:21 +02:00
|
|
|
NBR_TABLE_GLOBAL(uip_ds6_nbr_t, ds6_neighbors);
|
2013-07-03 19:32:26 +02:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_neighbors_init(void)
|
|
|
|
{
|
2013-07-29 18:49:21 +02:00
|
|
|
nbr_table_register(ds6_neighbors, (nbr_table_callback *)uip_ds6_nbr_rm);
|
2013-07-03 19:32:26 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ds6_nbr_t *
|
2013-11-16 14:42:57 +01:00
|
|
|
uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr,
|
2013-07-03 19:32:26 +02:00
|
|
|
uint8_t isrouter, uint8_t state)
|
|
|
|
{
|
2013-12-12 23:58:52 +01:00
|
|
|
uip_ds6_nbr_t *nbr = nbr_table_add_lladdr(ds6_neighbors, (linkaddr_t*)lladdr);
|
2013-07-03 19:32:26 +02:00
|
|
|
if(nbr) {
|
|
|
|
uip_ipaddr_copy(&nbr->ipaddr, ipaddr);
|
|
|
|
nbr->isrouter = isrouter;
|
|
|
|
nbr->state = state;
|
|
|
|
#if UIP_CONF_IPV6_QUEUE_PKT
|
|
|
|
uip_packetqueue_new(&nbr->packethandle);
|
|
|
|
#endif /* UIP_CONF_IPV6_QUEUE_PKT */
|
|
|
|
/* timers are set separately, for now we put them in expired state */
|
|
|
|
stimer_set(&nbr->reachable, 0);
|
|
|
|
stimer_set(&nbr->sendns, 0);
|
|
|
|
nbr->nscount = 0;
|
|
|
|
PRINTF("Adding neighbor with ip addr ");
|
|
|
|
PRINT6ADDR(ipaddr);
|
|
|
|
PRINTF(" link addr ");
|
|
|
|
PRINTLLADDR(lladdr);
|
|
|
|
PRINTF(" state %u\n", state);
|
|
|
|
NEIGHBOR_STATE_CHANGED(nbr);
|
|
|
|
return nbr;
|
|
|
|
} else {
|
|
|
|
PRINTF("uip_ds6_nbr_add drop ip addr ");
|
|
|
|
PRINT6ADDR(ipaddr);
|
|
|
|
PRINTF(" link addr (%p) ", lladdr);
|
|
|
|
PRINTLLADDR(lladdr);
|
|
|
|
PRINTF(" state %u\n", state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_nbr_rm(uip_ds6_nbr_t *nbr)
|
|
|
|
{
|
|
|
|
if(nbr != NULL) {
|
|
|
|
#if UIP_CONF_IPV6_QUEUE_PKT
|
|
|
|
uip_packetqueue_free(&nbr->packethandle);
|
|
|
|
#endif /* UIP_CONF_IPV6_QUEUE_PKT */
|
|
|
|
NEIGHBOR_STATE_CHANGED(nbr);
|
|
|
|
nbr_table_remove(ds6_neighbors, nbr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-16 14:42:57 +01:00
|
|
|
const uip_ipaddr_t *
|
|
|
|
uip_ds6_nbr_get_ipaddr(const uip_ds6_nbr_t *nbr)
|
2013-07-03 19:32:26 +02:00
|
|
|
{
|
|
|
|
return (nbr != NULL) ? &nbr->ipaddr : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-16 14:42:57 +01:00
|
|
|
const uip_lladdr_t *
|
|
|
|
uip_ds6_nbr_get_ll(const uip_ds6_nbr_t *nbr)
|
2013-07-03 19:32:26 +02:00
|
|
|
{
|
2013-11-16 14:42:57 +01:00
|
|
|
return (const uip_lladdr_t *)nbr_table_get_lladdr(ds6_neighbors, nbr);
|
2013-07-03 19:32:26 +02:00
|
|
|
}
|
2013-08-11 23:55:08 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
uip_ds6_nbr_num(void)
|
|
|
|
{
|
|
|
|
uip_ds6_nbr_t *nbr;
|
|
|
|
int num;
|
2013-07-03 19:32:26 +02:00
|
|
|
|
2013-08-11 23:55:08 +02:00
|
|
|
num = 0;
|
|
|
|
for(nbr = nbr_table_head(ds6_neighbors);
|
|
|
|
nbr != NULL;
|
|
|
|
nbr = nbr_table_next(ds6_neighbors, nbr)) {
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
2013-07-03 19:32:26 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ds6_nbr_t *
|
2013-11-16 14:42:57 +01:00
|
|
|
uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr)
|
2013-07-03 19:32:26 +02:00
|
|
|
{
|
|
|
|
uip_ds6_nbr_t *nbr = nbr_table_head(ds6_neighbors);
|
2013-08-12 00:19:12 +02:00
|
|
|
if(ipaddr != NULL) {
|
|
|
|
while(nbr != NULL) {
|
|
|
|
if(uip_ipaddr_cmp(&nbr->ipaddr, ipaddr)) {
|
|
|
|
return nbr;
|
|
|
|
}
|
|
|
|
nbr = nbr_table_next(ds6_neighbors, nbr);
|
2013-07-03 19:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ds6_nbr_t *
|
2013-11-16 14:42:57 +01:00
|
|
|
uip_ds6_nbr_ll_lookup(const uip_lladdr_t *lladdr)
|
2013-07-03 19:32:26 +02:00
|
|
|
{
|
2013-12-12 23:58:52 +01:00
|
|
|
return nbr_table_get_from_lladdr(ds6_neighbors, (linkaddr_t*)lladdr);
|
2013-07-03 19:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ipaddr_t *
|
2013-11-16 14:42:57 +01:00
|
|
|
uip_ds6_nbr_ipaddr_from_lladdr(const uip_lladdr_t *lladdr)
|
2013-07-03 19:32:26 +02:00
|
|
|
{
|
|
|
|
uip_ds6_nbr_t *nbr = uip_ds6_nbr_ll_lookup(lladdr);
|
|
|
|
return nbr ? &nbr->ipaddr : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-16 14:42:57 +01:00
|
|
|
const uip_lladdr_t *
|
|
|
|
uip_ds6_nbr_lladdr_from_ipaddr(const uip_ipaddr_t *ipaddr)
|
2013-07-03 19:32:26 +02:00
|
|
|
{
|
|
|
|
uip_ds6_nbr_t *nbr = uip_ds6_nbr_lookup(ipaddr);
|
|
|
|
return nbr ? uip_ds6_nbr_get_ll(nbr) : NULL;
|
|
|
|
}
|
2013-07-29 21:50:33 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_link_neighbor_callback(int status, int numtx)
|
|
|
|
{
|
2013-12-12 23:58:52 +01:00
|
|
|
const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
|
|
|
|
if(linkaddr_cmp(dest, &linkaddr_null)) {
|
2013-07-29 21:50:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LINK_NEIGHBOR_CALLBACK(dest, status, numtx);
|
|
|
|
|
|
|
|
#if UIP_DS6_LL_NUD
|
2014-10-27 11:57:49 +01:00
|
|
|
/* From RFC4861, page 72, last paragraph of section 7.3.3:
|
|
|
|
*
|
|
|
|
* "In some cases, link-specific information may indicate that a path to
|
|
|
|
* a neighbor has failed (e.g., the resetting of a virtual circuit). In
|
|
|
|
* such cases, link-specific information may be used to purge Neighbor
|
|
|
|
* Cache entries before the Neighbor Unreachability Detection would do
|
|
|
|
* so. However, link-specific information MUST NOT be used to confirm
|
|
|
|
* the reachability of a neighbor; such information does not provide
|
|
|
|
* end-to-end confirmation between neighboring IP layers."
|
|
|
|
*
|
|
|
|
* However, we assume that receiving a link layer ack ensures the delivery
|
|
|
|
* of the transmitted packed to the IP stack of the neighbour. This is a
|
|
|
|
* fair assumption and allows battery powered nodes save some battery by
|
|
|
|
* not re-testing the state of a neighbour periodically if it
|
|
|
|
* acknowledges link packets. */
|
2013-07-29 21:50:33 +02:00
|
|
|
if(status == MAC_TX_OK) {
|
|
|
|
uip_ds6_nbr_t *nbr;
|
|
|
|
nbr = uip_ds6_nbr_ll_lookup((uip_lladdr_t *)dest);
|
2014-10-27 11:57:49 +01:00
|
|
|
if(nbr != NULL && nbr->state != NBR_INCOMPLETE) {
|
2013-09-10 02:48:11 +02:00
|
|
|
nbr->state = NBR_REACHABLE;
|
2013-07-29 21:50:33 +02:00
|
|
|
stimer_set(&nbr->reachable, UIP_ND6_REACHABLE_TIME / 1000);
|
|
|
|
PRINTF("uip-ds6-neighbor : received a link layer ACK : ");
|
|
|
|
PRINTLLADDR((uip_lladdr_t *)dest);
|
|
|
|
PRINTF(" is reachable.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* UIP_DS6_LL_NUD */
|
|
|
|
|
|
|
|
}
|
2013-07-03 19:32:26 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2013-08-11 23:46:26 +02:00
|
|
|
uip_ds6_neighbor_periodic(void)
|
2013-07-03 19:32:26 +02:00
|
|
|
{
|
|
|
|
/* Periodic processing on neighbors */
|
|
|
|
uip_ds6_nbr_t *nbr = nbr_table_head(ds6_neighbors);
|
|
|
|
while(nbr != NULL) {
|
|
|
|
switch(nbr->state) {
|
|
|
|
case NBR_REACHABLE:
|
|
|
|
if(stimer_expired(&nbr->reachable)) {
|
2014-08-07 12:58:27 +02:00
|
|
|
#if UIP_CONF_IPV6_RPL
|
|
|
|
/* when a neighbor leave it's REACHABLE state and is a default router,
|
|
|
|
instead of going to STALE state it enters DELAY state in order to
|
|
|
|
force a NUD on it. Otherwise, if there is no upward traffic, the
|
|
|
|
node never knows if the default router is still reachable. This
|
|
|
|
mimics the 6LoWPAN-ND behavior.
|
|
|
|
*/
|
|
|
|
if(uip_ds6_defrt_lookup(&nbr->ipaddr) != NULL) {
|
|
|
|
PRINTF("REACHABLE: defrt moving to DELAY (");
|
|
|
|
PRINT6ADDR(&nbr->ipaddr);
|
|
|
|
PRINTF(")\n");
|
|
|
|
nbr->state = NBR_DELAY;
|
|
|
|
stimer_set(&nbr->reachable, UIP_ND6_DELAY_FIRST_PROBE_TIME);
|
|
|
|
nbr->nscount = 0;
|
|
|
|
} else {
|
|
|
|
PRINTF("REACHABLE: moving to STALE (");
|
|
|
|
PRINT6ADDR(&nbr->ipaddr);
|
|
|
|
PRINTF(")\n");
|
|
|
|
nbr->state = NBR_STALE;
|
|
|
|
}
|
|
|
|
#else /* UIP_CONF_IPV6_RPL */
|
2013-07-03 19:32:26 +02:00
|
|
|
PRINTF("REACHABLE: moving to STALE (");
|
|
|
|
PRINT6ADDR(&nbr->ipaddr);
|
|
|
|
PRINTF(")\n");
|
|
|
|
nbr->state = NBR_STALE;
|
2014-08-07 12:58:27 +02:00
|
|
|
#endif /* UIP_CONF_IPV6_RPL */
|
2013-07-03 19:32:26 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#if UIP_ND6_SEND_NA
|
|
|
|
case NBR_INCOMPLETE:
|
|
|
|
if(nbr->nscount >= UIP_ND6_MAX_MULTICAST_SOLICIT) {
|
|
|
|
uip_ds6_nbr_rm(nbr);
|
|
|
|
} else if(stimer_expired(&nbr->sendns) && (uip_len == 0)) {
|
|
|
|
nbr->nscount++;
|
|
|
|
PRINTF("NBR_INCOMPLETE: NS %u\n", nbr->nscount);
|
|
|
|
uip_nd6_ns_output(NULL, NULL, &nbr->ipaddr);
|
|
|
|
stimer_set(&nbr->sendns, uip_ds6_if.retrans_timer / 1000);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NBR_DELAY:
|
|
|
|
if(stimer_expired(&nbr->reachable)) {
|
|
|
|
nbr->state = NBR_PROBE;
|
|
|
|
nbr->nscount = 0;
|
|
|
|
PRINTF("DELAY: moving to PROBE\n");
|
|
|
|
stimer_set(&nbr->sendns, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NBR_PROBE:
|
|
|
|
if(nbr->nscount >= UIP_ND6_MAX_UNICAST_SOLICIT) {
|
|
|
|
uip_ds6_defrt_t *locdefrt;
|
|
|
|
PRINTF("PROBE END\n");
|
|
|
|
if((locdefrt = uip_ds6_defrt_lookup(&nbr->ipaddr)) != NULL) {
|
|
|
|
if (!locdefrt->isinfinite) {
|
|
|
|
uip_ds6_defrt_rm(locdefrt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uip_ds6_nbr_rm(nbr);
|
|
|
|
} else if(stimer_expired(&nbr->sendns) && (uip_len == 0)) {
|
|
|
|
nbr->nscount++;
|
|
|
|
PRINTF("PROBE: NS %u\n", nbr->nscount);
|
|
|
|
uip_nd6_ns_output(NULL, &nbr->ipaddr, &nbr->ipaddr);
|
|
|
|
stimer_set(&nbr->sendns, uip_ds6_if.retrans_timer / 1000);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* UIP_ND6_SEND_NA */
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nbr = nbr_table_next(ds6_neighbors, nbr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ds6_nbr_t *
|
|
|
|
uip_ds6_get_least_lifetime_neighbor(void)
|
|
|
|
{
|
|
|
|
uip_ds6_nbr_t *nbr = nbr_table_head(ds6_neighbors);
|
|
|
|
uip_ds6_nbr_t *nbr_expiring = NULL;
|
|
|
|
while(nbr != NULL) {
|
|
|
|
if(nbr_expiring != NULL) {
|
|
|
|
clock_time_t curr = stimer_remaining(&nbr->reachable);
|
|
|
|
if(curr < stimer_remaining(&nbr->reachable)) {
|
|
|
|
nbr_expiring = nbr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nbr_expiring = nbr;
|
|
|
|
}
|
|
|
|
nbr = nbr_table_next(ds6_neighbors, nbr);
|
|
|
|
}
|
|
|
|
return nbr_expiring;
|
|
|
|
}
|
2013-11-16 14:42:57 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-05-30 11:01:20 +02:00
|
|
|
/** @} */
|