2013-06-22 05:11:53 +02:00
|
|
|
/**
|
|
|
|
* RPL implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 INRIA.
|
|
|
|
*
|
|
|
|
* This file subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* @ingroup rpl
|
|
|
|
* @{
|
|
|
|
* @file rpl.c
|
|
|
|
* @brief basic RPL functions
|
|
|
|
* @author Eric Engel <eric.engel@fu-berlin.de>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2012-01-19 17:35:50 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <vtimer.h>
|
2012-01-26 20:26:55 +01:00
|
|
|
#include <thread.h>
|
2012-03-26 00:04:21 +02:00
|
|
|
#include <mutex.h>
|
2012-01-26 20:26:55 +01:00
|
|
|
#include "msg.h"
|
2012-01-19 17:35:50 +01:00
|
|
|
#include "rpl.h"
|
2013-03-15 17:48:13 +01:00
|
|
|
#include "etx_beaconing.h"
|
2012-01-26 20:26:55 +01:00
|
|
|
#include "of0.h"
|
2013-03-15 17:48:13 +01:00
|
|
|
#include "of_mrhof.h"
|
2012-02-02 21:31:28 +01:00
|
|
|
#include "trickle.h"
|
2012-01-19 17:35:50 +01:00
|
|
|
|
|
|
|
#include "sys/net/sixlowpan/sixlowmac.h"
|
|
|
|
#include "sys/net/sixlowpan/sixlowip.h"
|
|
|
|
#include "sys/net/sixlowpan/sixlowpan.h"
|
2012-02-22 00:50:40 +01:00
|
|
|
#include "sys/net/sixlowpan/sixlownd.h"
|
2012-02-14 22:22:01 +01:00
|
|
|
#include "sys/net/sixlowpan/sixlowerror.h"
|
2012-01-19 17:35:50 +01:00
|
|
|
|
2012-01-26 20:26:55 +01:00
|
|
|
char rpl_process_buf[RPL_PROCESS_STACKSIZE];
|
2013-06-22 05:11:53 +02:00
|
|
|
/* global variables */
|
2012-02-16 23:23:15 +01:00
|
|
|
char i_am_root = 0;
|
2012-01-26 20:26:55 +01:00
|
|
|
rpl_of_t *objective_functions[NUMBER_IMPLEMENTED_OFS];
|
2012-02-14 22:22:01 +01:00
|
|
|
rpl_routing_entry_t routing_table[RPL_MAX_ROUTING_ENTRIES];
|
2012-01-26 20:26:55 +01:00
|
|
|
unsigned int rpl_process_pid;
|
2012-02-14 22:22:01 +01:00
|
|
|
ipv6_addr_t my_address;
|
2012-03-26 00:04:21 +02:00
|
|
|
mutex_t rpl_send_mutex;
|
|
|
|
mutex_t rpl_recv_mutex;
|
2013-06-22 05:11:53 +02:00
|
|
|
/* receive buffer without LL_HDR */
|
2012-03-26 00:04:21 +02:00
|
|
|
uint8_t rpl_buffer[BUFFER_SIZE - LL_HDR_LEN];
|
2013-06-22 05:11:53 +02:00
|
|
|
/* in send buffer we need space fpr LL_HDR */
|
2012-03-26 00:04:21 +02:00
|
|
|
uint8_t rpl_send_buffer[BUFFER_SIZE];
|
2012-01-19 17:35:50 +01:00
|
|
|
|
2012-01-26 20:26:55 +01:00
|
|
|
msg_t msg_queue[RPL_PKT_RECV_BUF_SIZE];
|
2013-06-22 05:11:53 +02:00
|
|
|
/* SEND BUFFERS */
|
2013-06-24 14:09:33 +02:00
|
|
|
static ipv6_hdr_t *ipv6_send_buf;
|
2013-06-22 05:11:53 +02:00
|
|
|
static struct icmpv6_hdr_t *icmp_send_buf;
|
2012-03-26 00:04:21 +02:00
|
|
|
static struct rpl_dio_t *rpl_send_dio_buf;
|
|
|
|
static struct rpl_dis_t *rpl_send_dis_buf;
|
|
|
|
static struct rpl_dao_t *rpl_send_dao_buf;
|
2013-06-22 05:11:53 +02:00
|
|
|
static struct rpl_dao_ack_t *rpl_send_dao_ack_buf;
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_dodag_conf_t *rpl_send_opt_dodag_conf_buf;
|
2013-06-22 05:11:53 +02:00
|
|
|
/* static struct rpl_opt_solicited_t * rpl_send_opt_solicited_buf; */
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_target_t *rpl_send_opt_target_buf;
|
|
|
|
static rpl_opt_transit_t *rpl_send_opt_transit_buf;
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
/* RECEIVE BUFFERS */
|
2013-06-24 14:09:33 +02:00
|
|
|
static ipv6_hdr_t *ipv6_buf;
|
2012-01-19 17:35:50 +01:00
|
|
|
static struct rpl_dio_t *rpl_dio_buf;
|
2012-01-31 19:36:26 +01:00
|
|
|
static struct rpl_dis_t *rpl_dis_buf;
|
2012-01-19 17:35:50 +01:00
|
|
|
static struct rpl_dao_t *rpl_dao_buf;
|
2013-06-22 05:11:53 +02:00
|
|
|
static struct rpl_dao_ack_t *rpl_dao_ack_buf;
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_t *rpl_opt_buf;
|
|
|
|
static rpl_opt_dodag_conf_t *rpl_opt_dodag_conf_buf;
|
|
|
|
static rpl_opt_solicited_t *rpl_opt_solicited_buf;
|
|
|
|
static rpl_opt_target_t *rpl_opt_target_buf;
|
|
|
|
static rpl_opt_transit_t *rpl_opt_transit_buf;
|
2012-01-19 17:35:50 +01:00
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
/* SEND BUFFERS */
|
2013-06-24 14:09:33 +02:00
|
|
|
static ipv6_hdr_t *get_rpl_send_ipv6_buf(void)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((ipv6_hdr_t *)&(rpl_send_buffer[0]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static uint8_t *get_rpl_send_payload_buf(uint8_t ext_len)
|
|
|
|
{
|
|
|
|
return &(rpl_send_buffer[IPV6_HDR_LEN + ext_len]);
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static struct icmpv6_hdr_t *get_rpl_send_icmpv6_buf(uint8_t ext_len)
|
|
|
|
{
|
|
|
|
return ((struct icmpv6_hdr_t *)&(rpl_send_buffer[IPV6_HDR_LEN + ext_len]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static struct rpl_dio_t *get_rpl_send_dio_buf(void)
|
|
|
|
{
|
|
|
|
return ((struct rpl_dio_t *)&(rpl_send_buffer[IPV6HDR_ICMPV6HDR_LEN]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static struct rpl_dao_t *get_rpl_send_dao_buf(void)
|
|
|
|
{
|
|
|
|
return ((struct rpl_dao_t *)&(rpl_send_buffer[IPV6HDR_ICMPV6HDR_LEN]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static struct rpl_dao_ack_t *get_rpl_send_dao_ack_buf(void)
|
|
|
|
{
|
|
|
|
return ((struct rpl_dao_ack_t *)&(rpl_send_buffer[IPV6HDR_ICMPV6HDR_LEN]));
|
2012-03-27 17:56:47 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static struct rpl_dis_t *get_rpl_send_dis_buf(void)
|
|
|
|
{
|
|
|
|
return ((struct rpl_dis_t *)&(rpl_send_buffer[IPV6HDR_ICMPV6HDR_LEN]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_dodag_conf_t *get_rpl_send_opt_dodag_conf_buf(uint8_t rpl_msg_len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((rpl_opt_dodag_conf_t *)&(rpl_send_buffer[IPV6HDR_ICMPV6HDR_LEN + rpl_msg_len]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_target_t *get_rpl_send_opt_target_buf(uint8_t rpl_msg_len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((rpl_opt_target_t *)&(rpl_send_buffer[IPV6HDR_ICMPV6HDR_LEN + rpl_msg_len]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_transit_t *get_rpl_send_opt_transit_buf(uint8_t rpl_msg_len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((rpl_opt_transit_t *)&(rpl_send_buffer[IPV6HDR_ICMPV6HDR_LEN + rpl_msg_len]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2012-01-19 17:35:50 +01:00
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
/* RECEIVE BUFFERS */
|
2013-06-24 14:09:33 +02:00
|
|
|
static ipv6_hdr_t *get_rpl_ipv6_buf(void)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((ipv6_hdr_t *)&(rpl_buffer[0]));
|
2012-03-26 00:04:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static struct rpl_dio_t *get_rpl_dio_buf(void)
|
|
|
|
{
|
|
|
|
return ((struct rpl_dio_t *)&(rpl_buffer[IPV6HDR_ICMPV6HDR_LEN]));
|
2012-01-19 17:35:50 +01:00
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
static struct rpl_dao_t *get_rpl_dao_buf(void)
|
|
|
|
{
|
|
|
|
return ((struct rpl_dao_t *)&(rpl_buffer[IPV6HDR_ICMPV6HDR_LEN]));
|
2012-01-19 17:35:50 +01:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static struct rpl_dao_ack_t *get_rpl_dao_ack_buf(void)
|
|
|
|
{
|
|
|
|
return ((struct rpl_dao_ack_t *)&(buffer[LLHDR_ICMPV6HDR_LEN]));
|
2012-03-27 17:56:47 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
static struct rpl_dis_t *get_rpl_dis_buf(void)
|
|
|
|
{
|
|
|
|
return ((struct rpl_dis_t *)&(rpl_buffer[IPV6HDR_ICMPV6HDR_LEN]));
|
2012-01-31 19:36:26 +01:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_t *get_rpl_opt_buf(uint8_t rpl_msg_len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((rpl_opt_t *)&(rpl_buffer[IPV6HDR_ICMPV6HDR_LEN + rpl_msg_len]));
|
2012-01-19 17:35:50 +01:00
|
|
|
}
|
|
|
|
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_dodag_conf_t *get_rpl_opt_dodag_conf_buf(uint8_t rpl_msg_len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((rpl_opt_dodag_conf_t *)&(rpl_buffer[IPV6HDR_ICMPV6HDR_LEN + rpl_msg_len]));
|
2012-01-19 17:35:50 +01:00
|
|
|
}
|
|
|
|
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_solicited_t *get_rpl_opt_solicited_buf(uint8_t rpl_msg_len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((rpl_opt_solicited_t *)&(rpl_buffer[IPV6HDR_ICMPV6HDR_LEN + rpl_msg_len]));
|
2012-02-16 23:23:15 +01:00
|
|
|
}
|
|
|
|
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_target_t *get_rpl_opt_target_buf(uint8_t rpl_msg_len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((rpl_opt_target_t *)&(rpl_buffer[IPV6HDR_ICMPV6HDR_LEN + rpl_msg_len]));
|
2012-02-16 23:23:15 +01:00
|
|
|
}
|
|
|
|
|
2013-06-24 14:09:33 +02:00
|
|
|
static rpl_opt_transit_t *get_rpl_opt_transit_buf(uint8_t rpl_msg_len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-06-24 14:09:33 +02:00
|
|
|
return ((rpl_opt_transit_t *)&(rpl_buffer[IPV6HDR_ICMPV6HDR_LEN + rpl_msg_len]));
|
2012-02-26 19:30:48 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* find implemented OF via objective code point */
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_of_t *rpl_get_of_for_ocp(uint16_t ocp)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (uint16_t i = 0; i < NUMBER_IMPLEMENTED_OFS; i++) {
|
|
|
|
if (ocp == objective_functions[i]->ocp) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return objective_functions[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2012-01-26 20:26:55 +01:00
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
uint8_t rpl_init(transceiver_type_t trans, uint16_t rpl_address)
|
|
|
|
{
|
|
|
|
mutex_init(&rpl_send_mutex);
|
|
|
|
mutex_init(&rpl_recv_mutex);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_address == 0) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return SIXLOWERROR_ADDRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize routing table */
|
|
|
|
rpl_clear_routing_table();
|
|
|
|
init_trickle();
|
|
|
|
rpl_process_pid = thread_create(rpl_process_buf, RPL_PROCESS_STACKSIZE,
|
|
|
|
PRIORITY_MAIN - 1, CREATE_STACKTEST,
|
|
|
|
rpl_process, "rpl_process");
|
|
|
|
|
|
|
|
/* INSERT NEW OBJECTIVE FUNCTIONS HERE */
|
|
|
|
objective_functions[0] = rpl_get_of0();
|
|
|
|
/* objective_functions[1] = rpl_get_of_ETX() */
|
2012-02-14 22:22:01 +01:00
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
sixlowpan_init(trans, rpl_address, 0);
|
2013-07-16 13:41:23 +02:00
|
|
|
/* need link local prefix to query _our_ corresponding address */
|
2013-06-22 05:11:53 +02:00
|
|
|
ipv6_addr_t ll_address;
|
|
|
|
ipv6_set_ll_prefix(&ll_address);
|
2012-03-27 17:56:47 +02:00
|
|
|
ipv6_get_saddr(&my_address, &ll_address);
|
2013-06-22 05:11:53 +02:00
|
|
|
set_rpl_process_pid(rpl_process_pid);
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* initialize ETX-calculation if needed */
|
2013-06-28 17:53:21 +02:00
|
|
|
if(RPL_DEFAULT_OCP == 1){
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("INIT ETX BEACONING\n");
|
2013-03-15 17:48:13 +01:00
|
|
|
etx_init_beaconing(&my_address);
|
2013-06-28 17:53:21 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-28 17:53:21 +02:00
|
|
|
return SUCCESS;
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rpl_init_root(void)
|
|
|
|
{
|
|
|
|
rpl_instance_t *inst;
|
|
|
|
rpl_dodag_t *dodag;
|
|
|
|
|
|
|
|
inst = rpl_new_instance(RPL_DEFAULT_INSTANCE);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (inst == NULL) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("Error - No memory for another RPL instance\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
inst->id = RPL_DEFAULT_INSTANCE;
|
|
|
|
inst->joined = 1;
|
|
|
|
|
|
|
|
dodag = rpl_new_dodag(RPL_DEFAULT_INSTANCE, &my_address);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (dodag != NULL) {
|
2013-06-24 14:09:33 +02:00
|
|
|
dodag->of = (struct rpl_of_t*) rpl_get_of_for_ocp(RPL_DEFAULT_OCP);
|
2013-06-22 05:11:53 +02:00
|
|
|
dodag->instance = inst;
|
|
|
|
dodag->mop = RPL_DEFAULT_MOP;
|
|
|
|
dodag->dtsn = 1;
|
|
|
|
dodag->prf = 0;
|
|
|
|
dodag->dio_interval_doubling = DEFAULT_DIO_INTERVAL_DOUBLINGS;
|
|
|
|
dodag->dio_min = DEFAULT_DIO_INTERVAL_MIN;
|
|
|
|
dodag->dio_redundancy = DEFAULT_DIO_REDUNDANCY_CONSTANT;
|
|
|
|
dodag->maxrankincrease = 0;
|
|
|
|
dodag->minhoprankincrease = (uint16_t)DEFAULT_MIN_HOP_RANK_INCREASE;
|
|
|
|
dodag->default_lifetime = (uint8_t)RPL_DEFAULT_LIFETIME;
|
|
|
|
dodag->lifetime_unit = RPL_LIFETIME_UNIT;
|
|
|
|
dodag->version = RPL_COUNTER_INIT;
|
|
|
|
dodag->grounded = RPL_GROUNDED;
|
2013-06-28 17:53:21 +02:00
|
|
|
dodag->node_status = (uint8_t) ROOT_NODE;
|
2013-06-22 05:11:53 +02:00
|
|
|
dodag->my_rank = RPL_ROOT_RANK;
|
|
|
|
dodag->joined = 1;
|
|
|
|
dodag->my_preferred_parent = NULL;
|
|
|
|
}
|
|
|
|
else {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("Error - could not generate DODAG\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
i_am_root = 1;
|
|
|
|
start_trickle(dodag->dio_min, dodag->dio_interval_doubling, dodag->dio_redundancy);
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("ROOT INIT FINISHED\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void send_DIO(ipv6_addr_t *destination)
|
|
|
|
{
|
|
|
|
mutex_lock(&rpl_send_mutex);
|
|
|
|
rpl_dodag_t *mydodag;
|
|
|
|
icmp_send_buf = get_rpl_send_icmpv6_buf(ipv6_ext_hdr_len);
|
|
|
|
|
|
|
|
mydodag = rpl_get_my_dodag();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (mydodag == NULL) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("Error - trying to send DIO without being part of a dodag.\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
mutex_unlock(&rpl_send_mutex, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
icmp_send_buf->type = ICMP_RPL_CONTROL;
|
|
|
|
icmp_send_buf->code = ICMP_CODE_DIO;
|
|
|
|
icmp_send_buf->checksum = ~icmpv6_csum(PROTO_NUM_ICMPV6);
|
|
|
|
|
|
|
|
rpl_send_dio_buf = get_rpl_send_dio_buf();
|
|
|
|
memset(rpl_send_dio_buf, 0, sizeof(*rpl_send_dio_buf));
|
|
|
|
rpl_send_dio_buf->rpl_instanceid = mydodag->instance->id;
|
|
|
|
rpl_send_dio_buf->version_number = mydodag->version;
|
|
|
|
rpl_send_dio_buf->rank = mydodag->my_rank;
|
|
|
|
rpl_send_dio_buf->g_mop_prf = (mydodag->grounded << RPL_GROUNDED_SHIFT) | (mydodag->mop << RPL_MOP_SHIFT) | mydodag->prf;
|
|
|
|
rpl_send_dio_buf->dtsn = mydodag->dtsn;
|
|
|
|
rpl_send_dio_buf->flags = 0;
|
|
|
|
rpl_send_dio_buf->reserved = 0;
|
|
|
|
rpl_send_dio_buf->dodagid = mydodag->dodag_id;
|
|
|
|
|
|
|
|
int opt_hdr_len = 0;
|
2013-07-16 13:41:23 +02:00
|
|
|
/* DODAG configuration option */
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_send_opt_dodag_conf_buf = get_rpl_send_opt_dodag_conf_buf(DIO_BASE_LEN);
|
|
|
|
rpl_send_opt_dodag_conf_buf->type = RPL_OPT_DODAG_CONF;
|
|
|
|
rpl_send_opt_dodag_conf_buf->length = RPL_OPT_DODAG_CONF_LEN;
|
|
|
|
rpl_send_opt_dodag_conf_buf->flags_a_pcs = 0;
|
|
|
|
rpl_send_opt_dodag_conf_buf->DIOIntDoubl = mydodag->dio_interval_doubling;
|
|
|
|
rpl_send_opt_dodag_conf_buf->DIOIntMin = mydodag->dio_min;
|
|
|
|
rpl_send_opt_dodag_conf_buf->DIORedun = mydodag->dio_redundancy;
|
|
|
|
rpl_send_opt_dodag_conf_buf->MaxRankIncrease = mydodag->maxrankincrease;
|
|
|
|
rpl_send_opt_dodag_conf_buf->MinHopRankIncrease = mydodag->minhoprankincrease;
|
|
|
|
rpl_send_opt_dodag_conf_buf->ocp = mydodag->of->ocp;
|
|
|
|
rpl_send_opt_dodag_conf_buf->reserved = 0;
|
|
|
|
rpl_send_opt_dodag_conf_buf->default_lifetime = mydodag->default_lifetime;
|
|
|
|
rpl_send_opt_dodag_conf_buf->lifetime_unit = mydodag->lifetime_unit;
|
|
|
|
|
|
|
|
|
|
|
|
opt_hdr_len += RPL_OPT_LEN + RPL_OPT_DODAG_CONF_LEN;
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t plen = ICMPV6_HDR_LEN + DIO_BASE_LEN + opt_hdr_len;
|
|
|
|
rpl_send(destination, (uint8_t *)icmp_send_buf, plen, PROTO_NUM_ICMPV6, NULL);
|
|
|
|
mutex_unlock(&rpl_send_mutex, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_DIS(ipv6_addr_t *destination)
|
|
|
|
{
|
|
|
|
mutex_lock(&rpl_send_mutex);
|
|
|
|
icmp_send_buf = get_rpl_send_icmpv6_buf(ipv6_ext_hdr_len);
|
|
|
|
|
|
|
|
icmp_send_buf->type = ICMP_RPL_CONTROL;
|
|
|
|
icmp_send_buf->code = ICMP_CODE_DIS;
|
|
|
|
icmp_send_buf->checksum = ~icmpv6_csum(PROTO_NUM_ICMPV6);
|
|
|
|
|
|
|
|
rpl_send_dis_buf = get_rpl_send_dis_buf();
|
|
|
|
|
|
|
|
uint16_t plen = ICMPV6_HDR_LEN + DIS_BASE_LEN;
|
|
|
|
rpl_send(destination, (uint8_t *)icmp_send_buf, plen, PROTO_NUM_ICMPV6, NULL);
|
|
|
|
mutex_unlock(&rpl_send_mutex, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void send_DAO(ipv6_addr_t *destination, uint8_t lifetime, bool default_lifetime, uint8_t start_index)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (i_am_root) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&rpl_send_mutex);
|
|
|
|
rpl_dodag_t *my_dodag;
|
|
|
|
my_dodag = rpl_get_my_dodag();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (destination == NULL) {
|
2013-06-22 05:11:53 +02:00
|
|
|
destination = &my_dodag->my_preferred_parent->addr;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (default_lifetime) {
|
2013-06-22 05:11:53 +02:00
|
|
|
lifetime = my_dodag->default_lifetime;
|
|
|
|
}
|
|
|
|
|
|
|
|
icmp_send_buf = get_rpl_send_icmpv6_buf(ipv6_ext_hdr_len);
|
|
|
|
|
|
|
|
icmp_send_buf->type = ICMP_RPL_CONTROL;
|
|
|
|
icmp_send_buf->code = ICMP_CODE_DAO;
|
|
|
|
icmp_send_buf->checksum = ~icmpv6_csum(PROTO_NUM_ICMPV6);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (my_dodag == NULL) {
|
2013-06-22 05:11:53 +02:00
|
|
|
mutex_unlock(&rpl_send_mutex, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl_send_dao_buf = get_rpl_send_dao_buf();
|
|
|
|
memset(rpl_send_dao_buf, 0, sizeof(*rpl_send_dao_buf));
|
|
|
|
rpl_send_dao_buf->rpl_instanceid = my_dodag->instance->id;
|
|
|
|
rpl_send_dao_buf->k_d_flags = 0x00;
|
|
|
|
rpl_send_dao_buf->dao_sequence = my_dodag->dao_seq;
|
|
|
|
uint16_t opt_len = 0;
|
|
|
|
rpl_send_opt_target_buf = get_rpl_send_opt_target_buf(DAO_BASE_LEN);
|
2013-07-16 13:41:23 +02:00
|
|
|
/* add all targets from routing table as targets */
|
2013-06-22 05:11:53 +02:00
|
|
|
uint8_t entries = 0;
|
|
|
|
uint8_t continue_index = 0;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
for (uint8_t i = start_index; i < RPL_MAX_ROUTING_ENTRIES; i++) {
|
|
|
|
if (routing_table[i].used) {
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_send_opt_target_buf->type = RPL_OPT_TARGET;
|
|
|
|
rpl_send_opt_target_buf->length = RPL_OPT_TARGET_LEN;
|
|
|
|
rpl_send_opt_target_buf->flags = 0x00;
|
|
|
|
rpl_send_opt_target_buf->prefix_length = RPL_DODAG_ID_LEN;
|
|
|
|
memcpy(&rpl_send_opt_target_buf->target, &routing_table[i].address, sizeof(ipv6_addr_t));
|
|
|
|
opt_len += RPL_OPT_TARGET_LEN + 2;
|
|
|
|
rpl_send_opt_transit_buf = get_rpl_send_opt_transit_buf(DAO_BASE_LEN + opt_len);
|
|
|
|
rpl_send_opt_transit_buf->type = RPL_OPT_TRANSIT;
|
|
|
|
rpl_send_opt_transit_buf->length = RPL_OPT_TRANSIT_LEN;
|
|
|
|
rpl_send_opt_transit_buf->e_flags = 0x00;
|
|
|
|
rpl_send_opt_transit_buf->path_control = 0x00; /* not used */
|
|
|
|
rpl_send_opt_transit_buf->path_sequence = 0x00; /* not used */
|
|
|
|
rpl_send_opt_transit_buf->path_lifetime = lifetime;
|
|
|
|
opt_len += RPL_OPT_TRANSIT_LEN + 2;
|
|
|
|
rpl_send_opt_target_buf = get_rpl_send_opt_target_buf(DAO_BASE_LEN + opt_len);
|
|
|
|
entries++;
|
|
|
|
}
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* Split DAO, so packages don't get too big.
|
|
|
|
* The value 5 is based on experience. */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (entries >= 5) {
|
2013-06-22 05:11:53 +02:00
|
|
|
continue_index = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* add own address */
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_send_opt_target_buf->type = RPL_OPT_TARGET;
|
|
|
|
rpl_send_opt_target_buf->length = RPL_OPT_TARGET_LEN;
|
|
|
|
rpl_send_opt_target_buf->flags = 0x00;
|
|
|
|
rpl_send_opt_target_buf->prefix_length = RPL_DODAG_ID_LEN;
|
|
|
|
memcpy(&rpl_send_opt_target_buf->target, &my_address, sizeof(ipv6_addr_t));
|
|
|
|
opt_len += RPL_OPT_TARGET_LEN + 2;
|
|
|
|
|
|
|
|
rpl_send_opt_transit_buf = get_rpl_send_opt_transit_buf(DAO_BASE_LEN + opt_len);
|
|
|
|
rpl_send_opt_transit_buf->type = RPL_OPT_TRANSIT;
|
|
|
|
rpl_send_opt_transit_buf->length = RPL_OPT_TRANSIT_LEN;
|
|
|
|
rpl_send_opt_transit_buf->e_flags = 0x00;
|
|
|
|
rpl_send_opt_transit_buf->path_control = 0x00;
|
|
|
|
rpl_send_opt_transit_buf->path_sequence = 0x00;
|
|
|
|
rpl_send_opt_transit_buf->path_lifetime = lifetime;
|
|
|
|
opt_len += RPL_OPT_TRANSIT_LEN + 2;
|
|
|
|
|
|
|
|
uint16_t plen = ICMPV6_HDR_LEN + DAO_BASE_LEN + opt_len;
|
|
|
|
rpl_send(destination, (uint8_t *)icmp_send_buf, plen, PROTO_NUM_ICMPV6, NULL);
|
|
|
|
mutex_unlock(&rpl_send_mutex, 0);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (continue_index > 1) {
|
2013-06-22 05:11:53 +02:00
|
|
|
send_DAO(destination, lifetime, default_lifetime, continue_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_DAO_ACK(ipv6_addr_t *destination)
|
|
|
|
{
|
|
|
|
ipv6_print_addr(destination);
|
|
|
|
rpl_dodag_t *my_dodag;
|
|
|
|
my_dodag = rpl_get_my_dodag();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (my_dodag == NULL) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&rpl_send_mutex);
|
|
|
|
icmp_send_buf = get_rpl_send_icmpv6_buf(ipv6_ext_hdr_len);
|
|
|
|
|
|
|
|
icmp_send_buf->type = ICMP_RPL_CONTROL;
|
|
|
|
icmp_send_buf->code = ICMP_CODE_DAO_ACK;
|
|
|
|
icmp_send_buf->checksum = ~icmpv6_csum(PROTO_NUM_ICMPV6);
|
|
|
|
|
|
|
|
rpl_send_dao_ack_buf = get_rpl_send_dao_ack_buf();
|
|
|
|
rpl_send_dao_ack_buf->rpl_instanceid = my_dodag->instance->id;
|
|
|
|
rpl_send_dao_ack_buf->d_reserved = 0;
|
|
|
|
rpl_send_dao_ack_buf->dao_sequence = my_dodag->dao_seq;
|
|
|
|
rpl_send_dao_ack_buf->status = 0;
|
|
|
|
|
|
|
|
uint16_t plen = ICMPV6_HDR_LEN + DIS_BASE_LEN;
|
|
|
|
rpl_send(destination, (uint8_t *)icmp_send_buf, plen, PROTO_NUM_ICMPV6, NULL);
|
|
|
|
mutex_unlock(&rpl_send_mutex, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpl_process(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
msg_t m_recv;
|
|
|
|
msg_init_queue(msg_queue, RPL_PKT_RECV_BUF_SIZE);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (1) {
|
2013-06-22 05:11:53 +02:00
|
|
|
msg_receive(&m_recv);
|
|
|
|
uint8_t *code;
|
|
|
|
code = ((uint8_t *)m_recv.content.ptr);
|
2013-07-16 13:41:23 +02:00
|
|
|
/* differentiate packet types */
|
2013-06-22 05:11:53 +02:00
|
|
|
ipv6_buf = get_ipv6_buf();
|
|
|
|
memcpy(&rpl_buffer, ipv6_buf, ipv6_buf->length + IPV6_HDR_LEN);
|
|
|
|
|
|
|
|
switch(*code) {
|
2013-06-24 22:37:35 +02:00
|
|
|
case (ICMP_CODE_DIS): {
|
2013-06-22 05:11:53 +02:00
|
|
|
recv_rpl_dis();
|
|
|
|
mutex_unlock(&rpl_recv_mutex, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (ICMP_CODE_DIO): {
|
2013-06-22 05:11:53 +02:00
|
|
|
recv_rpl_dio();
|
|
|
|
mutex_unlock(&rpl_recv_mutex, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (ICMP_CODE_DAO): {
|
2013-06-22 05:11:53 +02:00
|
|
|
recv_rpl_dao();
|
|
|
|
mutex_unlock(&rpl_recv_mutex, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (ICMP_CODE_DAO_ACK): {
|
2013-06-22 05:11:53 +02:00
|
|
|
recv_rpl_dao_ack();
|
|
|
|
mutex_unlock(&rpl_recv_mutex, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
mutex_unlock(&rpl_recv_mutex, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void recv_rpl_dio(void)
|
|
|
|
{
|
|
|
|
ipv6_buf = get_rpl_ipv6_buf();
|
|
|
|
|
|
|
|
rpl_dio_buf = get_rpl_dio_buf();
|
|
|
|
int len = DIO_BASE_LEN;
|
|
|
|
|
|
|
|
rpl_instance_t *dio_inst = rpl_get_instance(rpl_dio_buf->rpl_instanceid);
|
|
|
|
rpl_instance_t *my_inst = rpl_get_my_instance();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (dio_inst == NULL) {
|
|
|
|
if (my_inst != NULL) {
|
2013-07-16 13:41:23 +02:00
|
|
|
/* already part of a DODAG -> impossible to join other instance */
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dio_inst = rpl_new_instance(rpl_dio_buf->rpl_instanceid);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (dio_inst == NULL) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 22:37:35 +02:00
|
|
|
else if (my_inst->id != dio_inst->id) {
|
2013-07-16 13:41:23 +02:00
|
|
|
/* TODO: Add support support for several instances. */
|
|
|
|
|
|
|
|
/* At the moment, nodes can only join one instance, this is
|
|
|
|
* the instance they join first.
|
|
|
|
* Instances cannot be switched later on. */
|
|
|
|
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("Ignoring instance - we are %d and got %d\n", my_inst->id, dio_inst->id);
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl_dodag_t dio_dodag;
|
|
|
|
memset(&dio_dodag, 0, sizeof(dio_dodag));
|
|
|
|
|
|
|
|
memcpy(&dio_dodag.dodag_id, &rpl_dio_buf->dodagid, sizeof(dio_dodag.dodag_id));
|
|
|
|
dio_dodag.dtsn = rpl_dio_buf->dtsn;
|
|
|
|
dio_dodag.mop = ((rpl_dio_buf->g_mop_prf >> RPL_MOP_SHIFT) & RPL_SHIFTED_MOP_MASK);
|
|
|
|
dio_dodag.grounded = rpl_dio_buf->g_mop_prf >> RPL_GROUNDED_SHIFT;
|
|
|
|
dio_dodag.prf = (rpl_dio_buf->g_mop_prf & RPL_PRF_MASK);
|
|
|
|
dio_dodag.version = rpl_dio_buf->version_number;
|
|
|
|
dio_dodag.instance = dio_inst;
|
|
|
|
|
|
|
|
uint8_t has_dodag_conf_opt = 0;
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* Parse until all options are consumed.
|
|
|
|
* ipv6_buf->length contains the packet length minus ipv6 and
|
|
|
|
* icmpv6 header, so only ICMPV6_HDR_LEN remains to be
|
|
|
|
* subtracted. */
|
2013-06-24 22:37:35 +02:00
|
|
|
while (len < (ipv6_buf->length - ICMPV6_HDR_LEN)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_opt_buf = get_rpl_opt_buf(len);
|
|
|
|
|
|
|
|
switch(rpl_opt_buf->type) {
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_PAD1): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_PADN): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += rpl_opt_buf->length + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_DAG_METRIC_CONTAINER): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += rpl_opt_buf->length + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_ROUTE_INFO): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += rpl_opt_buf->length + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_DODAG_CONF): {
|
2013-06-22 05:11:53 +02:00
|
|
|
has_dodag_conf_opt = 1;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_opt_buf->length != RPL_OPT_DODAG_CONF_LEN) {
|
2013-06-22 05:11:53 +02:00
|
|
|
/* error malformed */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl_opt_dodag_conf_buf = get_rpl_opt_dodag_conf_buf(len);
|
|
|
|
dio_dodag.dio_interval_doubling = rpl_opt_dodag_conf_buf->DIOIntDoubl;
|
|
|
|
dio_dodag.dio_min = rpl_opt_dodag_conf_buf->DIOIntMin;
|
|
|
|
dio_dodag.dio_redundancy = rpl_opt_dodag_conf_buf->DIORedun;
|
|
|
|
dio_dodag.maxrankincrease = rpl_opt_dodag_conf_buf->MaxRankIncrease;
|
|
|
|
dio_dodag.minhoprankincrease = rpl_opt_dodag_conf_buf->MinHopRankIncrease;
|
|
|
|
dio_dodag.default_lifetime = rpl_opt_dodag_conf_buf->default_lifetime;
|
|
|
|
dio_dodag.lifetime_unit = rpl_opt_dodag_conf_buf->lifetime_unit;
|
2013-06-24 14:09:33 +02:00
|
|
|
dio_dodag.of = (struct rpl_of_t*) rpl_get_of_for_ocp(rpl_opt_dodag_conf_buf->ocp);
|
2013-06-22 05:11:53 +02:00
|
|
|
len += RPL_OPT_DODAG_CONF_LEN + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_PREFIX_INFO): {
|
|
|
|
if (rpl_opt_buf->length != RPL_OPT_PREFIX_INFO_LEN) {
|
2013-06-22 05:11:53 +02:00
|
|
|
/* error malformed */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += RPL_OPT_PREFIX_INFO_LEN + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("[Error] Unsupported DIO option\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle packet content... */
|
|
|
|
rpl_dodag_t *my_dodag = rpl_get_my_dodag();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (my_dodag == NULL) {
|
|
|
|
if (!has_dodag_conf_opt) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("send DIS\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
send_DIS(&ipv6_buf->srcaddr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_dio_buf->rank < ROOT_RANK) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("DIO with Rank < ROOT_RANK\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (dio_dodag.mop != RPL_DEFAULT_MOP) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("Required MOP not supported\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (dio_dodag.of == NULL) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("Required objective function not supported\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_dio_buf->rank != INFINITE_RANK) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("Will join DODAG\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
ipv6_print_addr(&dio_dodag.dodag_id);
|
|
|
|
rpl_join_dodag(&dio_dodag, &ipv6_buf->srcaddr, rpl_dio_buf->rank);
|
|
|
|
}
|
|
|
|
else {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("Cannot access DODAG because of DIO with infinite rank\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_equal_id(&my_dodag->dodag_id, &dio_dodag.dodag_id)) {
|
2013-07-16 13:41:23 +02:00
|
|
|
/* "our" DODAG */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (RPL_COUNTER_GREATER_THAN(dio_dodag.version, my_dodag->version)) {
|
|
|
|
if (my_dodag->my_rank == ROOT_RANK) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("[Warning] Inconsistent Dodag Version\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
my_dodag->version = RPL_COUNTER_INCREMENT(dio_dodag.version);
|
|
|
|
reset_trickletimer();
|
|
|
|
}
|
|
|
|
else {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("[Info] New Version of dodag %d\n", dio_dodag.version);
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_global_repair(&dio_dodag, &ipv6_buf->srcaddr, rpl_dio_buf->rank);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2013-06-24 22:37:35 +02:00
|
|
|
else if (RPL_COUNTER_GREATER_THAN(my_dodag->version, dio_dodag.version)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
/* ein Knoten hat noch eine kleinere Versionsnummer -> mehr DIOs senden */
|
|
|
|
reset_trickletimer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* version matches, DODAG matches */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_dio_buf->rank == INFINITE_RANK) {
|
2013-06-22 05:11:53 +02:00
|
|
|
reset_trickletimer();
|
|
|
|
}
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* We are root, all done! */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (my_dodag->my_rank == ROOT_RANK) {
|
|
|
|
if (rpl_dio_buf->rank != INFINITE_RANK) {
|
2013-06-22 05:11:53 +02:00
|
|
|
trickle_increment_counter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/********************* Parent Handling *********************/
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
rpl_parent_t *parent;
|
|
|
|
parent = rpl_find_parent(&ipv6_buf->srcaddr);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (parent == NULL) {
|
2013-07-16 13:41:23 +02:00
|
|
|
/* add new parent candidate */
|
2013-06-22 05:11:53 +02:00
|
|
|
parent = rpl_new_parent(my_dodag, &ipv6_buf->srcaddr, rpl_dio_buf->rank);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (parent == NULL) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-16 13:41:23 +02:00
|
|
|
/* DIO OK */
|
2013-06-22 05:11:53 +02:00
|
|
|
trickle_increment_counter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update parent rank */
|
|
|
|
parent->rank = rpl_dio_buf->rank;
|
|
|
|
rpl_parent_update(parent);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_equal_id(&parent->addr, &my_dodag->my_preferred_parent->addr) && (parent->dtsn != rpl_dio_buf->dtsn)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
delay_dao();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->dtsn = rpl_dio_buf->dtsn;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void recv_rpl_dis(void)
|
|
|
|
{
|
|
|
|
rpl_dodag_t *my_dodag = rpl_get_my_dodag();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (my_dodag == NULL) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv6_buf = get_rpl_ipv6_buf();
|
|
|
|
rpl_dis_buf = get_rpl_dis_buf();
|
|
|
|
int len = DIS_BASE_LEN;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (len < (ipv6_buf->length - ICMPV6_HDR_LEN)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_opt_buf = get_rpl_opt_buf(len);
|
|
|
|
|
|
|
|
switch(rpl_opt_buf->type) {
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_PAD1): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_PADN): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += rpl_opt_buf->length + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_SOLICITED_INFO): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += RPL_OPT_SOLICITED_INFO_LEN + 2;
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* extract and check */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_opt_buf->length != RPL_OPT_SOLICITED_INFO_LEN) {
|
2013-06-22 05:11:53 +02:00
|
|
|
/* error malformed */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl_opt_solicited_buf = get_rpl_opt_solicited_buf(len);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_opt_solicited_buf->VID_Flags & RPL_DIS_I_MASK) {
|
|
|
|
if (my_dodag->instance->id != rpl_opt_solicited_buf->rplinstanceid) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_opt_solicited_buf->VID_Flags & RPL_DIS_D_MASK) {
|
|
|
|
if (!rpl_equal_id(&my_dodag->dodag_id, &rpl_opt_solicited_buf->dodagid)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_opt_solicited_buf->VID_Flags & RPL_DIS_V_MASK) {
|
|
|
|
if (my_dodag->version != rpl_opt_solicited_buf->version) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
send_DIO(&ipv6_buf->srcaddr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void recv_rpl_dao(void)
|
|
|
|
{
|
|
|
|
rpl_dodag_t *my_dodag = rpl_get_my_dodag();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (my_dodag == NULL) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("[Error] got DAO although not a DODAG\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv6_buf = get_rpl_ipv6_buf();
|
|
|
|
rpl_dao_buf = get_rpl_dao_buf();
|
|
|
|
int len = DAO_BASE_LEN;
|
|
|
|
uint8_t increment_seq = 0;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (len < (ipv6_buf->length - ICMPV6_HDR_LEN)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_opt_buf = get_rpl_opt_buf(len);
|
|
|
|
|
|
|
|
switch(rpl_opt_buf->type) {
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_PAD1): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_PADN): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += rpl_opt_buf->length + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_DAG_METRIC_CONTAINER): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += rpl_opt_buf->length + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_TARGET): {
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_opt_target_buf = get_rpl_opt_target_buf(len);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_opt_target_buf->prefix_length != RPL_DODAG_ID_LEN) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("prefixes are not supported yet");
|
2013-06-22 05:11:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += rpl_opt_target_buf->length + 2;
|
|
|
|
rpl_opt_transit_buf = get_rpl_opt_transit_buf(len);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_opt_transit_buf->type != RPL_OPT_TRANSIT) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("[Error] - no transit information for target option type = %d\n", rpl_opt_transit_buf->type);
|
2013-06-22 05:11:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += rpl_opt_transit_buf->length + 2;
|
2013-07-16 13:41:23 +02:00
|
|
|
/* route lifetime seconds = (DAO lifetime) * (Unit Lifetime) */
|
2013-06-22 05:11:53 +02:00
|
|
|
rpl_add_routing_entry(&rpl_opt_target_buf->target, &ipv6_buf->srcaddr, rpl_opt_transit_buf->path_lifetime * my_dodag->lifetime_unit);
|
|
|
|
increment_seq = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_TRANSIT): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += rpl_opt_buf->length + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
case (RPL_OPT_TARGET_DESC): {
|
2013-06-22 05:11:53 +02:00
|
|
|
len += rpl_opt_buf->length + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
send_DAO_ACK(&ipv6_buf->srcaddr);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (increment_seq) {
|
2013-06-22 05:11:53 +02:00
|
|
|
RPL_COUNTER_INCREMENT(my_dodag->dao_seq);
|
|
|
|
delay_dao();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void recv_rpl_dao_ack(void)
|
|
|
|
{
|
|
|
|
rpl_dodag_t *my_dodag = rpl_get_my_dodag();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (my_dodag == NULL) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl_dao_ack_buf = get_rpl_dao_ack_buf();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_dao_ack_buf->rpl_instanceid != my_dodag->instance->id) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rpl_dao_ack_buf->status != 0) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dao_ack_received();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-28 17:53:21 +02:00
|
|
|
/* TODO: tcp_socket unused? */
|
2013-06-22 05:11:53 +02:00
|
|
|
void rpl_send(ipv6_addr_t *destination, uint8_t *payload, uint16_t p_len, uint8_t next_header, void *tcp_socket)
|
|
|
|
{
|
|
|
|
uint8_t *p_ptr;
|
2012-03-26 00:04:21 +02:00
|
|
|
ipv6_send_buf = get_rpl_send_ipv6_buf();
|
|
|
|
p_ptr = get_rpl_send_payload_buf(ipv6_ext_hdr_len);
|
2012-02-14 22:22:01 +01:00
|
|
|
packet_length = 0;
|
|
|
|
|
2012-03-26 00:04:21 +02:00
|
|
|
ipv6_send_buf->version_trafficclass = IPV6_VER;
|
|
|
|
ipv6_send_buf->trafficclass_flowlabel = 0;
|
|
|
|
ipv6_send_buf->flowlabel = 0;
|
|
|
|
ipv6_send_buf->nextheader = next_header;
|
|
|
|
ipv6_send_buf->hoplimit = MULTIHOP_HOPLIMIT;
|
|
|
|
ipv6_send_buf->length = p_len;
|
|
|
|
|
|
|
|
memcpy(&(ipv6_send_buf->destaddr), destination, 16);
|
|
|
|
ipv6_get_saddr(&(ipv6_send_buf->srcaddr), &(ipv6_send_buf->destaddr));
|
|
|
|
|
2013-07-16 13:41:23 +02:00
|
|
|
/* The packet was "assembled" in rpl.c. Therefore rpl_send_buf was used.
|
|
|
|
* Therefore memcpy is not needed because the payload is at the
|
|
|
|
* right memory location already. */
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (p_ptr != payload) {
|
2013-06-22 05:11:53 +02:00
|
|
|
memcpy(p_ptr, payload, p_len);
|
|
|
|
}
|
2012-02-14 22:22:01 +01:00
|
|
|
|
|
|
|
packet_length = IPV6_HDR_LEN + p_len;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (ipv6_prefix_mcast_match(&ipv6_send_buf->destaddr)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
lowpan_init((ieee_802154_long_t *)&(ipv6_send_buf->destaddr.uint16[4]), (uint8_t *)ipv6_send_buf);
|
|
|
|
}
|
|
|
|
else {
|
2013-07-16 13:41:23 +02:00
|
|
|
/* find appropriate next hop before sending */
|
2013-06-22 05:11:53 +02:00
|
|
|
ipv6_addr_t *next_hop = rpl_get_next_hop(&ipv6_send_buf->destaddr);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (next_hop == NULL) {
|
|
|
|
if (i_am_root) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("[Error] destination unknown\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next_hop = rpl_get_my_preferred_parent();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (next_hop == NULL) {
|
2013-07-16 13:50:11 +02:00
|
|
|
DEBUG("[Error] no preferred parent, dropping package\n");
|
2013-06-22 05:11:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lowpan_init((ieee_802154_long_t *)&(next_hop->uint16[4]), (uint8_t *)ipv6_send_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv6_addr_t *rpl_get_next_hop(ipv6_addr_t *addr)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (uint8_t i = 0; i < RPL_MAX_ROUTING_ENTRIES; i++) {
|
|
|
|
if (routing_table[i].used && rpl_equal_id(&routing_table[i].address, addr)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return &routing_table[i].next_hop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpl_add_routing_entry(ipv6_addr_t *addr, ipv6_addr_t *next_hop, uint16_t lifetime)
|
|
|
|
{
|
|
|
|
rpl_routing_entry_t *entry = rpl_find_routing_entry(addr);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (entry != NULL) {
|
2013-06-22 05:11:53 +02:00
|
|
|
entry->lifetime = lifetime;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
for (uint8_t i = 0; i < RPL_MAX_ROUTING_ENTRIES; i++) {
|
|
|
|
if (!routing_table[i].used) {
|
2013-06-22 05:11:53 +02:00
|
|
|
routing_table[i].address = *addr;
|
|
|
|
routing_table[i].next_hop = *next_hop;
|
|
|
|
routing_table[i].lifetime = lifetime;
|
|
|
|
routing_table[i].used = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpl_del_routing_entry(ipv6_addr_t *addr)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (uint8_t i = 0; i < RPL_MAX_ROUTING_ENTRIES; i++) {
|
|
|
|
if (routing_table[i].used && rpl_equal_id(&routing_table[i].address, addr)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
memset(&routing_table[i], 0, sizeof(routing_table[i]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl_routing_entry_t *rpl_find_routing_entry(ipv6_addr_t *addr)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (uint8_t i = 0; i < RPL_MAX_ROUTING_ENTRIES; i++) {
|
|
|
|
if (routing_table[i].used && rpl_equal_id(&routing_table[i].address, addr)) {
|
2013-06-22 05:11:53 +02:00
|
|
|
return &routing_table[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpl_clear_routing_table(void)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (uint8_t i = 0; i < RPL_MAX_ROUTING_ENTRIES; i++) {
|
2013-06-22 05:11:53 +02:00
|
|
|
memset(&routing_table[i], 0, sizeof(routing_table[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl_routing_entry_t *rpl_get_routing_table(void)
|
|
|
|
{
|
|
|
|
return routing_table;
|
2012-02-22 00:50:40 +01:00
|
|
|
}
|