2010-04-30 15:43:53 +02:00
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* ContikiRPL, an implementation of IETF ROLL RPL.
|
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/uip.h"
|
|
|
|
#include "net/tcpip.h"
|
|
|
|
#include "net/uip-ds6.h"
|
2011-02-11 16:21:17 +01:00
|
|
|
#include "net/rpl/rpl-private.h"
|
2010-04-30 15:43:53 +02:00
|
|
|
#include "net/neighbor-info.h"
|
|
|
|
|
2010-11-03 16:41:23 +01:00
|
|
|
#define DEBUG DEBUG_NONE
|
2010-04-30 15:43:53 +02:00
|
|
|
#include "net/uip-debug.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
2010-10-27 02:46:40 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if RPL_CONF_STATS
|
|
|
|
rpl_stats_t rpl_stats;
|
|
|
|
#endif
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
extern uip_ds6_route_t uip_ds6_routing_table[UIP_DS6_ROUTE_NB];
|
2010-05-30 00:23:21 +02:00
|
|
|
/************************************************************************/
|
2010-04-30 15:43:53 +02:00
|
|
|
void
|
|
|
|
rpl_purge_routes(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < UIP_DS6_ROUTE_NB; i++) {
|
|
|
|
if(uip_ds6_routing_table[i].isused) {
|
|
|
|
if(uip_ds6_routing_table[i].state.lifetime <= 1) {
|
|
|
|
uip_ds6_route_rm(&uip_ds6_routing_table[i]);
|
|
|
|
} else {
|
|
|
|
uip_ds6_routing_table[i].state.lifetime--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/************************************************************************/
|
2010-06-03 17:20:56 +02:00
|
|
|
void
|
|
|
|
rpl_remove_routes(rpl_dag_t *dag)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < UIP_DS6_ROUTE_NB; i++) {
|
|
|
|
if(uip_ds6_routing_table[i].state.dag == dag) {
|
|
|
|
uip_ds6_route_rm(&uip_ds6_routing_table[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/************************************************************************/
|
2010-04-30 15:43:53 +02:00
|
|
|
uip_ds6_route_t *
|
|
|
|
rpl_add_route(rpl_dag_t *dag, uip_ipaddr_t *prefix, int prefix_len,
|
|
|
|
uip_ipaddr_t *next_hop)
|
|
|
|
{
|
|
|
|
uip_ds6_route_t *rep;
|
|
|
|
|
2010-05-24 18:38:56 +02:00
|
|
|
rep = uip_ds6_route_lookup(prefix);
|
|
|
|
if(rep == NULL) {
|
2010-04-30 15:43:53 +02:00
|
|
|
if((rep = uip_ds6_route_add(prefix, prefix_len, next_hop, 0)) == NULL) {
|
2010-05-18 18:43:56 +02:00
|
|
|
PRINTF("RPL: No space for more route entries\n");
|
2010-04-30 15:43:53 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-24 18:38:56 +02:00
|
|
|
} else {
|
|
|
|
PRINTF("RPL: Updated the next hop for prefix ");
|
2010-04-30 15:43:53 +02:00
|
|
|
PRINT6ADDR(prefix);
|
2010-05-24 18:38:56 +02:00
|
|
|
PRINTF(" to ");
|
2010-04-30 15:43:53 +02:00
|
|
|
PRINT6ADDR(next_hop);
|
|
|
|
PRINTF("\n");
|
2010-05-24 18:38:56 +02:00
|
|
|
uip_ipaddr_copy(&rep->nexthop, next_hop);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2010-05-24 18:38:56 +02:00
|
|
|
rep->state.dag = dag;
|
|
|
|
rep->state.lifetime = DEFAULT_ROUTE_LIFETIME;
|
|
|
|
rep->state.learned_from = RPL_ROUTE_FROM_INTERNAL;
|
|
|
|
|
|
|
|
PRINTF("RPL: Added a route to ");
|
|
|
|
PRINT6ADDR(prefix);
|
|
|
|
PRINTF("/%d via ", prefix_len);
|
|
|
|
PRINT6ADDR(next_hop);
|
|
|
|
PRINTF("\n");
|
|
|
|
|
2010-04-30 15:43:53 +02:00
|
|
|
return rep;
|
|
|
|
}
|
|
|
|
/************************************************************************/
|
|
|
|
static void
|
2010-06-14 20:35:04 +02:00
|
|
|
rpl_link_neighbor_callback(const rimeaddr_t *addr, int known, int etx)
|
2010-04-30 15:43:53 +02:00
|
|
|
{
|
|
|
|
uip_ipaddr_t ipaddr;
|
|
|
|
rpl_dag_t *dag;
|
2010-05-25 23:58:54 +02:00
|
|
|
rpl_parent_t *parent;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
uip_ip6addr(&ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
uip_ds6_set_addr_iid(&ipaddr, (uip_lladdr_t *)addr);
|
|
|
|
PRINTF("RPL: Neighbor ");
|
|
|
|
PRINT6ADDR(&ipaddr);
|
2010-12-15 15:35:07 +01:00
|
|
|
PRINTF(" is %sknown. ETX = %u\n", known ? "" : "no longer ", FIX2ETX(etx));
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
dag = rpl_get_dag(RPL_DEFAULT_INSTANCE);
|
2010-05-30 00:23:21 +02:00
|
|
|
if(dag == NULL) {
|
2010-04-30 15:43:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-30 00:23:21 +02:00
|
|
|
parent = rpl_find_parent(dag, &ipaddr);
|
|
|
|
if(parent == NULL) {
|
|
|
|
if(!known) {
|
|
|
|
PRINTF("RPL: Deleting routes installed by DAOs received from ");
|
|
|
|
PRINT6ADDR(&ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
uip_ds6_route_rm_by_nexthop(&ipaddr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-11 14:18:57 +01:00
|
|
|
if(etx != parent->etx) {
|
2010-06-14 14:44:37 +02:00
|
|
|
/* Trigger DAG rank recalculation. */
|
|
|
|
parent->updated = 1;
|
|
|
|
}
|
2011-02-11 14:18:57 +01:00
|
|
|
parent->etx = etx;
|
2010-06-14 14:44:37 +02:00
|
|
|
|
2010-05-30 00:23:21 +02:00
|
|
|
if(dag->of->parent_state_callback != NULL) {
|
|
|
|
dag->of->parent_state_callback(parent, known, etx);
|
|
|
|
}
|
|
|
|
|
2010-05-24 18:38:56 +02:00
|
|
|
if(!known) {
|
2010-04-30 15:43:53 +02:00
|
|
|
PRINTF("RPL: Removing parent ");
|
|
|
|
PRINT6ADDR(&parent->addr);
|
2010-05-18 18:43:56 +02:00
|
|
|
PRINTF(" because of bad connectivity (ETX %d)\n", etx);
|
2010-06-14 14:44:37 +02:00
|
|
|
parent->rank = INFINITE_RANK;
|
|
|
|
parent->updated = 1;
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/************************************************************************/
|
|
|
|
void
|
2010-06-14 20:35:04 +02:00
|
|
|
rpl_ipv6_neighbor_callback(uip_ds6_nbr_t *nbr)
|
|
|
|
{
|
|
|
|
rpl_dag_t *dag;
|
|
|
|
rpl_parent_t *p;
|
|
|
|
|
2010-09-20 17:30:12 +02:00
|
|
|
/* This only handles one DODAG - if multiple we need to check all */
|
2010-06-14 20:35:04 +02:00
|
|
|
dag = rpl_get_dag(RPL_ANY_INSTANCE);
|
2010-09-20 17:30:12 +02:00
|
|
|
if(dag == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if this is our default route then clean the dag->def_route state */
|
|
|
|
if(dag->def_route != NULL &&
|
|
|
|
uip_ipaddr_cmp(&dag->def_route->ipaddr, &nbr->ipaddr)) {
|
|
|
|
dag->def_route = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!nbr->isused) {
|
2010-06-14 20:35:04 +02:00
|
|
|
PRINTF("RPL: Removing neighbor ");
|
|
|
|
PRINT6ADDR(&nbr->ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
p = rpl_find_parent(dag, &nbr->ipaddr);
|
|
|
|
if(p != NULL) {
|
|
|
|
p->rank = INFINITE_RANK;
|
|
|
|
/* Trigger DAG rank recalculation. */
|
|
|
|
p->updated = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/************************************************************************/
|
|
|
|
void
|
2010-04-30 15:43:53 +02:00
|
|
|
rpl_init(void)
|
|
|
|
{
|
2011-01-25 23:41:30 +01:00
|
|
|
uip_ipaddr_t rplmaddr;
|
2010-04-30 15:43:53 +02:00
|
|
|
PRINTF("RPL started\n");
|
|
|
|
|
|
|
|
rpl_reset_periodic_timer();
|
2010-06-14 20:35:04 +02:00
|
|
|
neighbor_info_subscribe(rpl_link_neighbor_callback);
|
2011-01-25 23:41:30 +01:00
|
|
|
|
|
|
|
/* add rpl multicast address */
|
|
|
|
uip_create_linklocal_rplnodes_mcast(&rplmaddr);
|
|
|
|
uip_ds6_maddr_add(&rplmaddr);
|
|
|
|
|
2010-10-27 02:46:40 +02:00
|
|
|
#if RPL_CONF_STATS
|
|
|
|
memset(&rpl_stats, 0, sizeof(rpl_stats));
|
|
|
|
#endif
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
|
|
|
/************************************************************************/
|