2017-06-10 12:35:39 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-09-29 20:18:48 +00:00
|
|
|
* \addtogroup rpl-lite
|
|
|
|
* @{
|
|
|
|
*
|
2017-06-10 12:35:39 +00:00
|
|
|
* \file
|
|
|
|
* ContikiRPL, an implementation of RPL: IPv6 Routing Protocol
|
|
|
|
* for Low-Power and Lossy Networks (IETF RFC 6550)
|
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
* Simon Duquennoy <simon.duquennoy@inria.fr>
|
|
|
|
*/
|
|
|
|
|
2017-12-09 12:55:57 +00:00
|
|
|
#include "net/routing/rpl-lite/rpl.h"
|
2017-12-09 13:13:09 +00:00
|
|
|
#include "net/routing/routing.h"
|
2017-06-10 12:35:39 +00:00
|
|
|
|
2017-06-22 08:19:36 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "sys/log.h"
|
|
|
|
#define LOG_MODULE "RPL"
|
2017-07-15 13:19:09 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_RPL
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
uip_ipaddr_t rpl_multicast_addr;
|
2018-04-12 11:39:37 +00:00
|
|
|
static uint8_t rpl_leaf_only = RPL_DEFAULT_LEAF_ONLY;
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
rpl_lollipop_greater_than(int a, int b)
|
|
|
|
{
|
|
|
|
/* Check if we are comparing an initial value with an old value */
|
|
|
|
if(a > RPL_LOLLIPOP_CIRCULAR_REGION && b <= RPL_LOLLIPOP_CIRCULAR_REGION) {
|
|
|
|
return (RPL_LOLLIPOP_MAX_VALUE + 1 + b - a) > RPL_LOLLIPOP_SEQUENCE_WINDOWS;
|
|
|
|
}
|
|
|
|
/* Otherwise check if a > b and comparable => ok, or
|
|
|
|
if they have wrapped and are still comparable */
|
|
|
|
return (a > b && (a - b) < RPL_LOLLIPOP_SEQUENCE_WINDOWS) ||
|
|
|
|
(a < b && (b - a) > (RPL_LOLLIPOP_CIRCULAR_REGION + 1-
|
|
|
|
RPL_LOLLIPOP_SEQUENCE_WINDOWS));
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const uip_ipaddr_t *
|
|
|
|
rpl_get_global_address(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t state;
|
|
|
|
uip_ipaddr_t *ipaddr = NULL;
|
2017-12-11 10:09:37 +00:00
|
|
|
uip_ipaddr_t *prefix = NULL;
|
|
|
|
uint8_t prefix_length = 0;
|
|
|
|
|
|
|
|
if(curr_instance.used && curr_instance.dag.prefix_info.length != 0) {
|
|
|
|
prefix = &curr_instance.dag.prefix_info.prefix;
|
|
|
|
prefix_length = curr_instance.dag.prefix_info.length;
|
|
|
|
}
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
|
|
|
|
state = uip_ds6_if.addr_list[i].state;
|
|
|
|
if(uip_ds6_if.addr_list[i].isused &&
|
|
|
|
state == ADDR_PREFERRED &&
|
2017-12-11 10:09:37 +00:00
|
|
|
!uip_is_addr_linklocal(&uip_ds6_if.addr_list[i].ipaddr) &&
|
|
|
|
(prefix == NULL || uip_ipaddr_prefixcmp(prefix, &uip_ds6_if.addr_list[i].ipaddr, prefix_length))) {
|
2017-06-10 12:35:39 +00:00
|
|
|
ipaddr = &uip_ds6_if.addr_list[i].ipaddr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipaddr;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-06-24 09:46:37 +00:00
|
|
|
rpl_link_callback(const linkaddr_t *addr, int status, int numtx)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
|
|
|
if(curr_instance.used == 1 ) {
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_nbr_t *nbr = rpl_neighbor_get_from_lladdr((uip_lladdr_t *)addr);
|
|
|
|
if(nbr != NULL) {
|
2018-05-09 21:38:59 +00:00
|
|
|
/* If this is the neighbor we were probing urgently, mark urgent
|
|
|
|
probing as done */
|
|
|
|
if(curr_instance.dag.urgent_probing_target == nbr) {
|
|
|
|
curr_instance.dag.urgent_probing_target = NULL;
|
|
|
|
}
|
2017-06-10 12:35:39 +00:00
|
|
|
/* Link stats were updated, and we need to update our internal state.
|
|
|
|
Updating from here is unsafe; postpone */
|
2017-06-23 19:12:36 +00:00
|
|
|
LOG_INFO("packet sent to ");
|
|
|
|
LOG_INFO_LLADDR(addr);
|
2017-06-23 19:23:12 +00:00
|
|
|
LOG_INFO_(", status %u, tx %u, new link metric %u\n", status, numtx, rpl_neighbor_get_link_metric(nbr));
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_timers_schedule_state_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-12-13 20:55:22 +00:00
|
|
|
rpl_has_joined(void)
|
|
|
|
{
|
|
|
|
return curr_instance.used && curr_instance.dag.state >= DAG_JOINED;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_is_reachable(void)
|
|
|
|
{
|
2017-06-27 09:06:38 +00:00
|
|
|
return curr_instance.used && curr_instance.dag.state == DAG_REACHABLE;
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set_ip_from_prefix(uip_ipaddr_t *ipaddr, rpl_prefix_t *prefix)
|
|
|
|
{
|
|
|
|
memset(ipaddr, 0, sizeof(uip_ipaddr_t));
|
|
|
|
memcpy(ipaddr, &prefix->prefix, (prefix->length + 7) / 8);
|
|
|
|
uip_ds6_set_addr_iid(ipaddr, &uip_lladdr);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rpl_reset_prefix(rpl_prefix_t *last_prefix)
|
|
|
|
{
|
|
|
|
uip_ipaddr_t ipaddr;
|
|
|
|
uip_ds6_addr_t *rep;
|
|
|
|
set_ip_from_prefix(&ipaddr, last_prefix);
|
|
|
|
rep = uip_ds6_addr_lookup(&ipaddr);
|
|
|
|
if(rep != NULL) {
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_INFO("removing global IP address ");
|
|
|
|
LOG_INFO_6ADDR(&ipaddr);
|
2017-06-23 07:21:31 +00:00
|
|
|
LOG_INFO_("\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
uip_ds6_addr_rm(rep);
|
|
|
|
}
|
|
|
|
curr_instance.dag.prefix_info.length = 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
rpl_set_prefix_from_addr(uip_ipaddr_t *addr, unsigned len, uint8_t flags)
|
|
|
|
{
|
|
|
|
uip_ipaddr_t ipaddr;
|
|
|
|
|
|
|
|
if(addr == NULL || len == 0 || len > 128 || !(flags & UIP_ND6_RA_FLAG_AUTONOMOUS)) {
|
2017-07-15 07:25:41 +00:00
|
|
|
LOG_WARN("prefix not included, not-supported or invalid\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try and initialize prefix */
|
|
|
|
memset(&curr_instance.dag.prefix_info.prefix, 0, sizeof(rpl_prefix_t));
|
|
|
|
memcpy(&curr_instance.dag.prefix_info.prefix, addr, (len + 7) / 8);
|
|
|
|
curr_instance.dag.prefix_info.length = len;
|
|
|
|
curr_instance.dag.prefix_info.lifetime = RPL_ROUTE_INFINITE_LIFETIME;
|
|
|
|
curr_instance.dag.prefix_info.flags = flags;
|
|
|
|
|
|
|
|
/* Add global address if not already there */
|
|
|
|
set_ip_from_prefix(&ipaddr, &curr_instance.dag.prefix_info);
|
|
|
|
if(uip_ds6_addr_lookup(&ipaddr) == NULL) {
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_INFO("adding global IP address ");
|
|
|
|
LOG_INFO_6ADDR(&ipaddr);
|
2017-06-23 07:21:31 +00:00
|
|
|
LOG_INFO_("\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
rpl_set_prefix(rpl_prefix_t *prefix)
|
|
|
|
{
|
|
|
|
if(prefix != NULL && rpl_set_prefix_from_addr(&prefix->prefix, prefix->length, prefix->flags)) {
|
|
|
|
curr_instance.dag.prefix_info.lifetime = prefix->lifetime;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-09 13:13:09 +00:00
|
|
|
static void
|
2017-12-09 16:27:57 +00:00
|
|
|
init(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_INFO("initializing\n");
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/* Initialize multicast address and register it */
|
|
|
|
uip_create_linklocal_rplnodes_mcast(&rpl_multicast_addr);
|
|
|
|
uip_ds6_maddr_add(&rpl_multicast_addr);
|
|
|
|
|
|
|
|
rpl_dag_init();
|
2017-06-19 09:35:02 +00:00
|
|
|
rpl_neighbor_init();
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_timers_init();
|
|
|
|
rpl_icmp6_init();
|
|
|
|
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_init();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
get_sr_node_ipaddr(uip_ipaddr_t *addr, const uip_sr_node_t *node)
|
|
|
|
{
|
|
|
|
if(addr != NULL && node != NULL) {
|
|
|
|
memcpy(addr, &curr_instance.dag.dag_id, 8);
|
|
|
|
memcpy(((unsigned char *)addr) + 8, &node->link_identifier, 8);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-10 14:55:28 +00:00
|
|
|
static void
|
2017-12-10 15:34:16 +00:00
|
|
|
neighbor_state_changed(uip_ds6_nbr_t *nbr)
|
|
|
|
{
|
|
|
|
/* Nothing needs be done in non-storing mode */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2017-12-10 14:55:28 +00:00
|
|
|
drop_route(uip_ds6_route_t *route)
|
|
|
|
{
|
|
|
|
/* Do nothing. RPL-lite only supports non-storing mode, i.e. no routes */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-04-12 11:39:37 +00:00
|
|
|
void
|
|
|
|
rpl_set_leaf_only(uint8_t value)
|
|
|
|
{
|
|
|
|
rpl_leaf_only = value;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uint8_t
|
|
|
|
rpl_get_leaf_only(void)
|
|
|
|
{
|
|
|
|
return rpl_leaf_only;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-09 13:13:09 +00:00
|
|
|
const struct routing_driver rpl_lite_driver = {
|
|
|
|
"RPL Lite",
|
2017-12-09 16:27:57 +00:00
|
|
|
init,
|
2017-12-09 13:19:01 +00:00
|
|
|
rpl_dag_root_set_prefix,
|
2017-12-09 13:23:35 +00:00
|
|
|
rpl_dag_root_start,
|
2017-12-10 19:51:26 +00:00
|
|
|
rpl_dag_root_is_root,
|
|
|
|
rpl_dag_get_root_ipaddr,
|
2017-12-13 20:00:33 +00:00
|
|
|
get_sr_node_ipaddr,
|
2017-12-10 19:51:26 +00:00
|
|
|
rpl_dag_poison_and_leave,
|
2017-12-13 20:55:22 +00:00
|
|
|
rpl_has_joined,
|
2017-12-10 19:51:26 +00:00
|
|
|
rpl_is_reachable,
|
2017-12-09 16:45:10 +00:00
|
|
|
rpl_global_repair,
|
|
|
|
rpl_local_repair,
|
2017-12-10 13:59:19 +00:00
|
|
|
rpl_ext_header_remove,
|
2017-12-10 14:11:01 +00:00
|
|
|
rpl_ext_header_update,
|
2017-12-10 14:21:37 +00:00
|
|
|
rpl_ext_header_hbh_update,
|
2017-12-10 14:35:50 +00:00
|
|
|
rpl_ext_header_srh_update,
|
2017-12-10 14:10:35 +00:00
|
|
|
rpl_ext_header_srh_get_next_hop,
|
2017-12-10 14:39:38 +00:00
|
|
|
rpl_link_callback,
|
2017-12-10 15:34:16 +00:00
|
|
|
neighbor_state_changed,
|
2017-12-10 14:55:28 +00:00
|
|
|
drop_route,
|
2017-12-09 13:13:09 +00:00
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/** @}*/
|