2010-04-30 13:43:53 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-11-08 00:15:42 +00:00
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
/**
|
|
|
|
* \file
|
2012-04-21 22:18:07 +00:00
|
|
|
* ContikiRPL, an implementation of RPL: IPv6 Routing Protocol
|
|
|
|
* for Low-Power and Lossy Networks (IETF RFC 6550)
|
2010-04-30 13:43:53 +00:00
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 00:15:42 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-11-22 08:17:54 +00:00
|
|
|
#include "net/ip/uip.h"
|
|
|
|
#include "net/ip/tcpip.h"
|
|
|
|
#include "net/ipv6/uip-ds6.h"
|
2014-03-08 21:36:45 +00:00
|
|
|
#include "net/ipv6/uip-icmp6.h"
|
2011-02-11 15:21:17 +00:00
|
|
|
#include "net/rpl/rpl-private.h"
|
2011-12-09 12:15:04 +00:00
|
|
|
#include "net/ipv6/multicast/uip-mcast6.h"
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2010-11-03 15:41:23 +00:00
|
|
|
#define DEBUG DEBUG_NONE
|
2013-11-22 08:17:54 +00:00
|
|
|
#include "net/ip/uip-debug.h"
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
2010-10-27 00:46:40 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if RPL_CONF_STATS
|
|
|
|
rpl_stats_t rpl_stats;
|
|
|
|
#endif
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2013-11-20 13:10:39 +00:00
|
|
|
static enum rpl_mode mode = RPL_MODE_MESH;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
enum rpl_mode
|
|
|
|
rpl_get_mode(void)
|
|
|
|
{
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
enum rpl_mode
|
|
|
|
rpl_set_mode(enum rpl_mode m)
|
|
|
|
{
|
|
|
|
enum rpl_mode oldmode = mode;
|
|
|
|
|
|
|
|
/* We need to do different things depending on what mode we are
|
|
|
|
switching to. */
|
|
|
|
if(m == RPL_MODE_MESH) {
|
|
|
|
|
2015-01-04 23:09:47 +00:00
|
|
|
/* If we switch to mesh mode, we should send out a DAO message to
|
2013-11-20 13:10:39 +00:00
|
|
|
inform our parent that we now are reachable. Before we do this,
|
2015-01-04 23:09:47 +00:00
|
|
|
we must set the mode variable, since DAOs will not be sent if
|
2013-11-20 13:10:39 +00:00
|
|
|
we are in feather mode. */
|
|
|
|
PRINTF("RPL: switching to mesh mode\n");
|
|
|
|
mode = m;
|
|
|
|
|
|
|
|
if(default_instance != NULL) {
|
|
|
|
rpl_schedule_dao_immediately(default_instance);
|
|
|
|
}
|
|
|
|
} else if(m == RPL_MODE_FEATHER) {
|
|
|
|
|
|
|
|
PRINTF("RPL: switching to feather mode\n");
|
|
|
|
if(default_instance != NULL) {
|
2015-08-26 14:47:10 +00:00
|
|
|
PRINTF("rpl_set_mode: RPL sending DAO with zero lifetime\n");
|
|
|
|
if(default_instance->current_dag != NULL) {
|
|
|
|
dao_output(default_instance->current_dag->preferred_parent, RPL_ZERO_LIFETIME);
|
|
|
|
}
|
2013-11-20 13:10:39 +00:00
|
|
|
rpl_cancel_dao(default_instance);
|
2015-08-26 14:47:10 +00:00
|
|
|
} else {
|
|
|
|
PRINTF("rpl_set_mode: no default instance\n");
|
2013-11-20 13:10:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 14:47:10 +00:00
|
|
|
mode = m;
|
2013-11-20 13:10:39 +00:00
|
|
|
} else {
|
|
|
|
mode = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldmode;
|
|
|
|
}
|
2012-11-26 18:53:38 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 13:43:53 +00:00
|
|
|
void
|
|
|
|
rpl_purge_routes(void)
|
|
|
|
{
|
2012-11-26 18:53:38 +00:00
|
|
|
uip_ds6_route_t *r;
|
2013-07-25 08:39:39 +00:00
|
|
|
uip_ipaddr_t prefix;
|
|
|
|
rpl_dag_t *dag;
|
2011-12-09 12:15:04 +00:00
|
|
|
#if RPL_CONF_MULTICAST
|
|
|
|
uip_mcast6_route_t *mcast_route;
|
|
|
|
#endif
|
2012-11-26 18:53:38 +00:00
|
|
|
|
2012-12-19 22:22:14 +00:00
|
|
|
/* First pass, decrement lifetime */
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_head();
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2012-11-26 18:53:38 +00:00
|
|
|
while(r != NULL) {
|
2012-12-19 22:22:14 +00:00
|
|
|
if(r->state.lifetime >= 1) {
|
|
|
|
/*
|
|
|
|
* If a route is at lifetime == 1, set it to 0, scheduling it for
|
|
|
|
* immediate removal below. This achieves the same as the original code,
|
|
|
|
* which would delete lifetime <= 1
|
|
|
|
*/
|
|
|
|
r->state.lifetime--;
|
|
|
|
}
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_next(r);
|
2012-12-19 22:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Second pass, remove dead routes */
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_head();
|
2012-12-19 22:22:14 +00:00
|
|
|
|
|
|
|
while(r != NULL) {
|
|
|
|
if(r->state.lifetime < 1) {
|
|
|
|
/* Routes with lifetime == 1 have only just been decremented from 2 to 1,
|
|
|
|
* thus we want to keep them. Hence < and not <= */
|
2013-07-25 08:39:39 +00:00
|
|
|
uip_ipaddr_copy(&prefix, &r->ipaddr);
|
2012-11-26 18:53:38 +00:00
|
|
|
uip_ds6_route_rm(r);
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_head();
|
2013-07-25 08:39:39 +00:00
|
|
|
PRINTF("No more routes to ");
|
|
|
|
PRINT6ADDR(&prefix);
|
|
|
|
dag = default_instance->current_dag;
|
2015-01-05 04:54:02 +00:00
|
|
|
/* Propagate this information with a No-Path DAO to preferred parent if we are not a RPL Root */
|
2013-07-25 08:39:39 +00:00
|
|
|
if(dag->rank != ROOT_RANK(default_instance)) {
|
|
|
|
PRINTF(" -> generate No-Path DAO\n");
|
|
|
|
dao_output_target(dag->preferred_parent, &prefix, RPL_ZERO_LIFETIME);
|
|
|
|
/* Don't schedule more than 1 No-Path DAO, let next iteration handle that */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PRINTF("\n");
|
2012-11-26 18:53:38 +00:00
|
|
|
} else {
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_next(r);
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-09 12:15:04 +00:00
|
|
|
|
|
|
|
#if RPL_CONF_MULTICAST
|
|
|
|
mcast_route = uip_mcast6_route_list_head();
|
|
|
|
|
|
|
|
while(mcast_route != NULL) {
|
|
|
|
if(mcast_route->lifetime <= 1) {
|
|
|
|
uip_mcast6_route_rm(mcast_route);
|
|
|
|
mcast_route = uip_mcast6_route_list_head();
|
|
|
|
} else {
|
|
|
|
mcast_route->lifetime--;
|
|
|
|
mcast_route = list_item_next(mcast_route);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-06-03 15:20:56 +00:00
|
|
|
void
|
|
|
|
rpl_remove_routes(rpl_dag_t *dag)
|
|
|
|
{
|
2012-11-26 18:53:38 +00:00
|
|
|
uip_ds6_route_t *r;
|
2011-12-09 12:15:04 +00:00
|
|
|
#if RPL_CONF_MULTICAST
|
|
|
|
uip_mcast6_route_t *mcast_route;
|
|
|
|
#endif
|
2010-06-03 15:20:56 +00:00
|
|
|
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_head();
|
2012-11-26 18:53:38 +00:00
|
|
|
|
|
|
|
while(r != NULL) {
|
|
|
|
if(r->state.dag == dag) {
|
|
|
|
uip_ds6_route_rm(r);
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_head();
|
2012-11-26 18:53:38 +00:00
|
|
|
} else {
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_next(r);
|
2010-06-03 15:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-09 12:15:04 +00:00
|
|
|
|
|
|
|
#if RPL_CONF_MULTICAST
|
|
|
|
mcast_route = uip_mcast6_route_list_head();
|
|
|
|
|
|
|
|
while(mcast_route != NULL) {
|
|
|
|
if(mcast_route->dag == dag) {
|
|
|
|
uip_mcast6_route_rm(mcast_route);
|
|
|
|
mcast_route = uip_mcast6_route_list_head();
|
|
|
|
} else {
|
|
|
|
mcast_route = list_item_next(mcast_route);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-03 15:20:56 +00:00
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-07-11 13:50:51 +00:00
|
|
|
void
|
|
|
|
rpl_remove_routes_by_nexthop(uip_ipaddr_t *nexthop, rpl_dag_t *dag)
|
|
|
|
{
|
2012-11-26 18:53:38 +00:00
|
|
|
uip_ds6_route_t *r;
|
|
|
|
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_head();
|
2012-11-26 18:53:38 +00:00
|
|
|
|
|
|
|
while(r != NULL) {
|
2013-07-03 17:45:56 +00:00
|
|
|
if(uip_ipaddr_cmp(uip_ds6_route_nexthop(r), nexthop) &&
|
2012-11-26 18:53:38 +00:00
|
|
|
r->state.dag == dag) {
|
|
|
|
uip_ds6_route_rm(r);
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_head();
|
2012-11-26 18:53:38 +00:00
|
|
|
} else {
|
2013-07-03 17:45:56 +00:00
|
|
|
r = uip_ds6_route_next(r);
|
2012-11-26 18:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ANNOTATE("#L %u 0\n", nexthop->u8[sizeof(uip_ipaddr_t) - 1]);
|
2011-07-11 13:50:51 +00:00
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 13:43:53 +00: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;
|
|
|
|
|
2013-07-03 17:45:56 +00:00
|
|
|
if((rep = uip_ds6_route_add(prefix, prefix_len, next_hop)) == NULL) {
|
|
|
|
PRINTF("RPL: No space for more route entries\n");
|
|
|
|
return NULL;
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
2013-07-03 17:45:56 +00:00
|
|
|
|
2010-05-24 16:38:56 +00:00
|
|
|
rep->state.dag = dag;
|
2011-07-11 13:50:51 +00:00
|
|
|
rep->state.lifetime = RPL_LIFETIME(dag->instance, dag->instance->default_lifetime);
|
2015-10-02 05:38:52 +00:00
|
|
|
/* always clear state flags for the no-path received when adding/refreshing */
|
|
|
|
RPL_ROUTE_CLEAR_NOPATH_RECEIVED(rep);
|
2010-05-24 16:38:56 +00:00
|
|
|
|
|
|
|
PRINTF("RPL: Added a route to ");
|
|
|
|
PRINT6ADDR(prefix);
|
|
|
|
PRINTF("/%d via ", prefix_len);
|
|
|
|
PRINT6ADDR(next_hop);
|
|
|
|
PRINTF("\n");
|
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
return rep;
|
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-07-29 19:50:33 +00:00
|
|
|
void
|
2013-12-12 22:58:52 +00:00
|
|
|
rpl_link_neighbor_callback(const linkaddr_t *addr, int status, int numtx)
|
2010-04-30 13:43:53 +00:00
|
|
|
{
|
|
|
|
uip_ipaddr_t ipaddr;
|
2010-05-25 21:58:54 +00:00
|
|
|
rpl_parent_t *parent;
|
2011-07-11 13:50:51 +00:00
|
|
|
rpl_instance_t *instance;
|
|
|
|
rpl_instance_t *end;
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
uip_ip6addr(&ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
uip_ds6_set_addr_iid(&ipaddr, (uip_lladdr_t *)addr);
|
|
|
|
|
2011-11-16 08:00:19 +00:00
|
|
|
for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES; instance < end; ++instance) {
|
|
|
|
if(instance->used == 1 ) {
|
2011-07-11 13:50:51 +00:00
|
|
|
parent = rpl_find_parent_any_dag(instance, &ipaddr);
|
2011-11-16 08:00:19 +00:00
|
|
|
if(parent != NULL) {
|
2011-07-11 13:50:51 +00:00
|
|
|
/* Trigger DAG rank recalculation. */
|
2013-08-11 21:42:51 +00:00
|
|
|
PRINTF("RPL: rpl_link_neighbor_callback triggering update\n");
|
2014-04-29 14:25:16 +00:00
|
|
|
parent->flags |= RPL_PARENT_FLAG_UPDATED;
|
2011-07-11 13:50:51 +00:00
|
|
|
}
|
2010-05-29 22:23:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 13:43:53 +00:00
|
|
|
void
|
2010-06-14 18:35:04 +00:00
|
|
|
rpl_ipv6_neighbor_callback(uip_ds6_nbr_t *nbr)
|
|
|
|
{
|
|
|
|
rpl_parent_t *p;
|
2011-07-11 13:50:51 +00:00
|
|
|
rpl_instance_t *instance;
|
|
|
|
rpl_instance_t *end;
|
2010-09-20 15:30:12 +00:00
|
|
|
|
2015-07-27 15:36:45 +00:00
|
|
|
PRINTF("RPL: Neighbor state changed for ");
|
2013-07-03 17:32:26 +00:00
|
|
|
PRINT6ADDR(&nbr->ipaddr);
|
2015-11-20 18:07:20 +00:00
|
|
|
#if UIP_ND6_SEND_NA || UIP_ND6_SEND_RA
|
2015-07-27 15:36:45 +00:00
|
|
|
PRINTF(", nscount=%u, state=%u\n", nbr->nscount, nbr->state);
|
2015-11-20 18:07:20 +00:00
|
|
|
#else /* UIP_ND6_SEND_NA || UIP_ND6_SEND_RA */
|
|
|
|
PRINTF(", state=%u\n", nbr->state);
|
|
|
|
#endif /* UIP_ND6_SEND_NA || UIP_ND6_SEND_RA */
|
2013-07-03 17:32:26 +00:00
|
|
|
for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES; instance < end; ++instance) {
|
|
|
|
if(instance->used == 1 ) {
|
|
|
|
p = rpl_find_parent_any_dag(instance, &nbr->ipaddr);
|
|
|
|
if(p != NULL) {
|
|
|
|
p->rank = INFINITE_RANK;
|
|
|
|
/* Trigger DAG rank recalculation. */
|
2013-08-11 21:42:51 +00:00
|
|
|
PRINTF("RPL: rpl_ipv6_neighbor_callback infinite rank\n");
|
2014-04-29 14:25:16 +00:00
|
|
|
p->flags |= RPL_PARENT_FLAG_UPDATED;
|
2011-07-11 13:50:51 +00:00
|
|
|
}
|
2010-06-14 18:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-06-14 18:35:04 +00:00
|
|
|
void
|
2015-09-30 07:39:44 +00:00
|
|
|
rpl_purge_dags(void)
|
|
|
|
{
|
|
|
|
rpl_instance_t *instance;
|
|
|
|
rpl_instance_t *end;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES;
|
|
|
|
instance < end; ++instance) {
|
|
|
|
if(instance->used) {
|
|
|
|
for(i = 0; i < RPL_MAX_DAG_PER_INSTANCE; i++) {
|
|
|
|
if(instance->dag_table[i].used) {
|
|
|
|
if(instance->dag_table[i].lifetime == 0) {
|
|
|
|
if(!instance->dag_table[i].joined) {
|
|
|
|
PRINTF("Removing dag ");
|
|
|
|
PRINT6ADDR(&instance->dag_table[i].dag_id);
|
|
|
|
PRINTF("\n");
|
|
|
|
rpl_free_dag(&instance->dag_table[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
instance->dag_table[i].lifetime--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-04-30 13:43:53 +00:00
|
|
|
rpl_init(void)
|
|
|
|
{
|
2011-01-25 22:41:30 +00:00
|
|
|
uip_ipaddr_t rplmaddr;
|
2010-04-30 13:43:53 +00:00
|
|
|
PRINTF("RPL started\n");
|
2011-11-16 08:00:19 +00:00
|
|
|
default_instance = NULL;
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2013-07-03 17:53:51 +00:00
|
|
|
rpl_dag_init();
|
2010-04-30 13:43:53 +00:00
|
|
|
rpl_reset_periodic_timer();
|
2014-03-08 21:36:45 +00:00
|
|
|
rpl_icmp6_register_handlers();
|
2011-01-25 22:41:30 +00:00
|
|
|
|
|
|
|
/* add rpl multicast address */
|
|
|
|
uip_create_linklocal_rplnodes_mcast(&rplmaddr);
|
|
|
|
uip_ds6_maddr_add(&rplmaddr);
|
|
|
|
|
2010-10-27 00:46:40 +00:00
|
|
|
#if RPL_CONF_STATS
|
|
|
|
memset(&rpl_stats, 0, sizeof(rpl_stats));
|
|
|
|
#endif
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-05-30 09:01:20 +00:00
|
|
|
|
|
|
|
/** @}*/
|