2017-06-10 12:35:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, Inria.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-12-13 20:00:33 +00:00
|
|
|
* \addtogroup uip
|
2017-09-29 20:18:48 +00:00
|
|
|
* @{
|
|
|
|
*
|
2017-06-10 12:35:39 +00:00
|
|
|
* \file
|
2017-12-13 20:00:33 +00:00
|
|
|
* Source routing support
|
2017-06-10 12:35:39 +00:00
|
|
|
*
|
|
|
|
* \author Simon Duquennoy <simon.duquennoy@inria.fr>
|
|
|
|
*/
|
|
|
|
|
2017-12-13 20:00:33 +00:00
|
|
|
#include "contiki.h"
|
|
|
|
#include "net/ipv6/uip-sr.h"
|
|
|
|
#include "net/routing/routing.h"
|
2017-06-10 12:35:39 +00:00
|
|
|
#include "lib/list.h"
|
|
|
|
#include "lib/memb.h"
|
|
|
|
|
2017-06-22 08:19:36 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "sys/log.h"
|
2017-12-13 20:00:33 +00:00
|
|
|
#define LOG_MODULE "IPv6 SR"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_IPV6
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/* Total number of nodes */
|
|
|
|
static int num_nodes;
|
|
|
|
|
|
|
|
/* Every known node in the network */
|
|
|
|
LIST(nodelist);
|
2017-12-13 20:00:33 +00:00
|
|
|
MEMB(nodememb, uip_sr_node_t, UIP_SR_LINK_NUM);
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_num_nodes(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
|
|
|
return num_nodes;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2017-12-13 20:00:33 +00:00
|
|
|
node_matches_address(void *graph, const uip_sr_node_t *node, const uip_ipaddr_t *addr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-12-13 20:00:33 +00:00
|
|
|
if(node == NULL || addr == NULL || graph != node->graph) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
uip_ipaddr_t node_ipaddr;
|
|
|
|
NETSTACK_ROUTING.get_sr_node_ipaddr(&node_ipaddr, node);
|
|
|
|
return uip_ipaddr_cmp(&node_ipaddr, addr);
|
|
|
|
}
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *
|
|
|
|
uip_sr_get_node(void *graph, const uip_ipaddr_t *addr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *l;
|
2017-06-10 12:35:39 +00:00
|
|
|
for(l = list_head(nodelist); l != NULL; l = list_item_next(l)) {
|
|
|
|
/* Compare prefix and node identifier */
|
2017-12-13 20:00:33 +00:00
|
|
|
if(node_matches_address(graph, l, addr)) {
|
2017-06-10 12:35:39 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_is_addr_reachable(void *graph, const uip_ipaddr_t *addr)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-12-13 20:00:33 +00:00
|
|
|
int max_depth = UIP_SR_LINK_NUM;
|
|
|
|
uip_ipaddr_t root_ipaddr;
|
|
|
|
uip_sr_node_t *node;
|
|
|
|
uip_sr_node_t *root_node;
|
|
|
|
|
|
|
|
NETSTACK_ROUTING.get_root_ipaddr(&root_ipaddr);
|
|
|
|
node = uip_sr_get_node(graph, addr);
|
|
|
|
root_node = uip_sr_get_node(graph, &root_ipaddr);
|
|
|
|
|
2017-06-10 12:35:39 +00:00
|
|
|
while(node != NULL && node != root_node && max_depth > 0) {
|
|
|
|
node = node->parent;
|
|
|
|
max_depth--;
|
|
|
|
}
|
|
|
|
return node != NULL && node == root_node;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_expire_parent(void *graph, const uip_ipaddr_t *child, const uip_ipaddr_t *parent)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *l = uip_sr_get_node(graph, child);
|
2017-06-10 12:35:39 +00:00
|
|
|
/* Check if parent matches */
|
2017-12-13 20:00:33 +00:00
|
|
|
if(l != NULL && node_matches_address(graph, l->parent, parent)) {
|
|
|
|
l->lifetime = UIP_SR_REMOVAL_DELAY;
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *
|
|
|
|
uip_sr_update_node(void *graph, const uip_ipaddr_t *child, const uip_ipaddr_t *parent, uint32_t lifetime)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *child_node = uip_sr_get_node(graph, child);
|
|
|
|
uip_sr_node_t *parent_node = uip_sr_get_node(graph, parent);
|
|
|
|
uip_sr_node_t *old_parent_node;
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
if(parent != NULL) {
|
|
|
|
/* No node for the parent, add one with infinite lifetime */
|
|
|
|
if(parent_node == NULL) {
|
2017-12-13 20:00:33 +00:00
|
|
|
parent_node = uip_sr_update_node(graph, parent, NULL, UIP_SR_INFINITE_LIFETIME);
|
2017-06-10 12:35:39 +00:00
|
|
|
if(parent_node == NULL) {
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_ERR("NS: no space left for root node!\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No node for this child, add one */
|
|
|
|
if(child_node == NULL) {
|
|
|
|
child_node = memb_alloc(&nodememb);
|
|
|
|
/* No space left, abort */
|
|
|
|
if(child_node == NULL) {
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_ERR("NS: no space left for child ");
|
|
|
|
LOG_ERR_6ADDR(child);
|
2017-06-23 07:21:31 +00:00
|
|
|
LOG_ERR_("\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
child_node->parent = NULL;
|
|
|
|
list_add(nodelist, child_node);
|
|
|
|
num_nodes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize node */
|
2017-12-13 20:00:33 +00:00
|
|
|
child_node->graph = graph;
|
2017-06-10 12:35:39 +00:00
|
|
|
child_node->lifetime = lifetime;
|
|
|
|
memcpy(child_node->link_identifier, ((const unsigned char *)child) + 8, 8);
|
|
|
|
|
|
|
|
/* Is the node reachable before the update? */
|
2017-12-13 20:00:33 +00:00
|
|
|
if(uip_sr_is_addr_reachable(graph, child)) {
|
2017-06-10 12:35:39 +00:00
|
|
|
old_parent_node = child_node->parent;
|
|
|
|
/* Update node */
|
|
|
|
child_node->parent = parent_node;
|
|
|
|
/* Has the node become unreachable? May happen if we create a loop. */
|
2017-12-13 20:00:33 +00:00
|
|
|
if(!uip_sr_is_addr_reachable(graph, child)) {
|
2017-06-10 12:35:39 +00:00
|
|
|
/* The new parent makes the node unreachable, restore old parent.
|
|
|
|
* We will take the update next time, with chances we know more of
|
|
|
|
* the topology and the loop is gone. */
|
|
|
|
child_node->parent = old_parent_node;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
child_node->parent = parent_node;
|
|
|
|
}
|
|
|
|
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_INFO("NS: updating link, child ");
|
|
|
|
LOG_INFO_6ADDR(child);
|
2017-06-23 07:21:31 +00:00
|
|
|
LOG_INFO_(", parent ");
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_INFO_6ADDR(parent);
|
2017-06-23 07:21:31 +00:00
|
|
|
LOG_INFO_(", lifetime %u, num_nodes %u\n", (unsigned)lifetime, num_nodes);
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
return child_node;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_init(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
|
|
|
num_nodes = 0;
|
|
|
|
memb_init(&nodememb);
|
|
|
|
list_init(nodelist);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *
|
|
|
|
uip_sr_node_head(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
|
|
|
return list_head(nodelist);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *
|
|
|
|
uip_sr_node_next(uip_sr_node_t *item)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
|
|
|
return list_item_next(item);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_periodic(unsigned seconds)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *l;
|
|
|
|
uip_sr_node_t *next;
|
2017-11-12 13:05:29 +00:00
|
|
|
|
|
|
|
/* First pass, for all expired nodes, deallocate them iff no child points to them */
|
2017-07-07 13:12:23 +00:00
|
|
|
for(l = list_head(nodelist); l != NULL; l = next) {
|
|
|
|
next = list_item_next(l);
|
2017-06-10 12:35:39 +00:00
|
|
|
if(l->lifetime == 0) {
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *l2;
|
2017-06-10 12:35:39 +00:00
|
|
|
for(l2 = list_head(nodelist); l2 != NULL; l2 = list_item_next(l2)) {
|
|
|
|
if(l2->parent == l) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 14:23:42 +00:00
|
|
|
if(LOG_INFO_ENABLED) {
|
|
|
|
uip_ipaddr_t node_addr;
|
|
|
|
NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, l);
|
|
|
|
LOG_INFO("NS: removing expired node ");
|
|
|
|
LOG_INFO_6ADDR(&node_addr);
|
|
|
|
LOG_INFO_("\n");
|
|
|
|
}
|
2017-06-10 12:35:39 +00:00
|
|
|
/* No child found, deallocate node */
|
|
|
|
list_remove(nodelist, l);
|
|
|
|
memb_free(&nodememb, l);
|
|
|
|
num_nodes--;
|
2017-12-13 20:00:33 +00:00
|
|
|
} else if(l->lifetime != UIP_SR_INFINITE_LIFETIME) {
|
2017-11-12 13:05:29 +00:00
|
|
|
l->lifetime = l->lifetime > seconds ? l->lifetime - seconds : 0;
|
|
|
|
}
|
|
|
|
}
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
2017-07-07 13:12:23 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_free_all(void)
|
2017-07-07 13:12:23 +00:00
|
|
|
{
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *l;
|
|
|
|
uip_sr_node_t *next;
|
2017-07-07 13:12:23 +00:00
|
|
|
for(l = list_head(nodelist); l != NULL; l = next) {
|
|
|
|
next = list_item_next(l);
|
|
|
|
list_remove(nodelist, l);
|
|
|
|
memb_free(&nodememb, l);
|
|
|
|
num_nodes--;
|
|
|
|
}
|
|
|
|
}
|
2017-09-29 20:18:48 +00:00
|
|
|
/** @} */
|