2010-04-30 13:43:53 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, 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
|
|
|
|
* ICMP6 I/O for RPL control messages.
|
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
|
2010-04-30 23:10:45 +00:00
|
|
|
* Contributors: Niclas Finne <nfi@sics.se>, Joel Hoglund <joel@sics.se>,
|
2010-04-30 13:43:53 +00:00
|
|
|
* Mathieu Pouillot <m.pouillot@watteco.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/tcpip.h"
|
|
|
|
#include "net/uip.h"
|
|
|
|
#include "net/uip-ds6.h"
|
2010-04-30 15:04:47 +00:00
|
|
|
#include "net/uip-nd6.h"
|
2010-04-30 13:43:53 +00:00
|
|
|
#include "net/uip-icmp6.h"
|
2011-02-11 15:21:17 +00:00
|
|
|
#include "net/rpl/rpl-private.h"
|
2010-06-14 19:19:16 +00:00
|
|
|
#include "net/packetbuf.h"
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
2010-05-19 16:16:57 +00:00
|
|
|
#include <string.h>
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2010-11-03 15:41:23 +00:00
|
|
|
#define DEBUG DEBUG_NONE
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
#include "net/uip-debug.h"
|
|
|
|
|
2013-03-17 22:21:30 +00:00
|
|
|
#if UIP_CONF_IPV6
|
2010-06-06 21:42:50 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-12-13 10:54:25 +00:00
|
|
|
#define RPL_DIO_GROUNDED 0x80
|
|
|
|
#define RPL_DIO_MOP_SHIFT 3
|
|
|
|
#define RPL_DIO_MOP_MASK 0x3c
|
|
|
|
#define RPL_DIO_PREFERENCE_MASK 0x07
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
|
|
|
#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
|
|
|
|
#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len])
|
2010-06-06 21:42:50 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 13:43:53 +00:00
|
|
|
static void dis_input(void);
|
|
|
|
static void dio_input(void);
|
|
|
|
static void dao_input(void);
|
2010-06-06 21:42:50 +00:00
|
|
|
static void dao_ack_input(void);
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2011-10-27 10:32:16 +00:00
|
|
|
/* some debug callbacks useful when debugging RPL networks */
|
|
|
|
#ifdef RPL_DEBUG_DIO_INPUT
|
|
|
|
void RPL_DEBUG_DIO_INPUT(uip_ipaddr_t *, rpl_dio_t *);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RPL_DEBUG_DAO_OUTPUT
|
|
|
|
void RPL_DEBUG_DAO_OUTPUT(rpl_parent_t *);
|
|
|
|
#endif
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
static uint8_t dao_sequence = RPL_LOLLIPOP_INIT;
|
2011-11-21 14:09:19 +00:00
|
|
|
|
|
|
|
/* some debug callbacks useful when debugging RPL networks */
|
|
|
|
#ifdef RPL_DEBUG_DIO_INPUT
|
|
|
|
void RPL_DEBUG_DIO_INPUT(uip_ipaddr_t *, rpl_dio_t *);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RPL_DEBUG_DAO_OUTPUT
|
|
|
|
void RPL_DEBUG_DAO_OUTPUT(rpl_parent_t *);
|
|
|
|
#endif
|
|
|
|
|
2012-02-29 20:59:50 +00:00
|
|
|
extern rpl_of_t RPL_OF;
|
|
|
|
|
2010-06-06 21:42:50 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 13:43:53 +00:00
|
|
|
static int
|
|
|
|
get_global_addr(uip_ipaddr_t *addr)
|
|
|
|
{
|
|
|
|
int i;
|
2010-04-30 23:10:45 +00:00
|
|
|
int state;
|
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
|
2010-04-30 23:10:45 +00:00
|
|
|
state = uip_ds6_if.addr_list[i].state;
|
2010-04-30 13:43:53 +00:00
|
|
|
if(uip_ds6_if.addr_list[i].isused &&
|
|
|
|
(state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
|
|
|
|
if(!uip_is_addr_link_local(&uip_ds6_if.addr_list[i].ipaddr)) {
|
|
|
|
memcpy(addr, &uip_ds6_if.addr_list[i].ipaddr, sizeof(uip_ipaddr_t));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static uint32_t
|
|
|
|
get32(uint8_t *buffer, int pos)
|
|
|
|
{
|
|
|
|
return (uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
|
2011-09-06 15:28:39 +00:00
|
|
|
(uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3];
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set32(uint8_t *buffer, int pos, uint32_t value)
|
|
|
|
{
|
|
|
|
buffer[pos++] = value >> 24;
|
|
|
|
buffer[pos++] = (value >> 16) & 0xff;
|
|
|
|
buffer[pos++] = (value >> 8) & 0xff;
|
|
|
|
buffer[pos++] = value & 0xff;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-09-06 15:28:39 +00:00
|
|
|
static uint16_t
|
|
|
|
get16(uint8_t *buffer, int pos)
|
|
|
|
{
|
|
|
|
return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set16(uint8_t *buffer, int pos, uint16_t value)
|
|
|
|
{
|
|
|
|
buffer[pos++] = value >> 8;
|
|
|
|
buffer[pos++] = value & 0xff;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 13:43:53 +00:00
|
|
|
static void
|
|
|
|
dis_input(void)
|
|
|
|
{
|
2011-07-11 13:50:51 +00:00
|
|
|
rpl_instance_t *instance;
|
|
|
|
rpl_instance_t *end;
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
/* DAG Information Solicitation */
|
|
|
|
PRINTF("RPL: Received a DIS from ");
|
|
|
|
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
|
2011-11-16 08:00:19 +00:00
|
|
|
for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES; instance < end; ++instance) {
|
2012-04-21 22:18:07 +00:00
|
|
|
if(instance->used == 1) {
|
2011-07-11 13:50:51 +00:00
|
|
|
#if RPL_LEAF_ONLY
|
|
|
|
if(!uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
|
2013-02-20 23:25:32 +00:00
|
|
|
PRINTF("RPL: LEAF ONLY Multicast DIS will NOT reset DIO timer\n");
|
2011-07-11 13:50:51 +00:00
|
|
|
#else /* !RPL_LEAF_ONLY */
|
|
|
|
if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
|
|
|
|
PRINTF("RPL: Multicast DIS => reset DIO timer\n");
|
2012-01-04 10:22:11 +00:00
|
|
|
rpl_reset_dio_timer(instance);
|
2011-07-11 13:50:51 +00:00
|
|
|
} else {
|
|
|
|
#endif /* !RPL_LEAF_ONLY */
|
|
|
|
PRINTF("RPL: Unicast DIS, reply to sender\n");
|
|
|
|
dio_output(instance, &UIP_IP_BUF->srcipaddr);
|
|
|
|
}
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
dis_output(uip_ipaddr_t *addr)
|
|
|
|
{
|
|
|
|
unsigned char *buffer;
|
|
|
|
uip_ipaddr_t tmpaddr;
|
|
|
|
|
2012-04-21 22:18:07 +00:00
|
|
|
/* DAG Information Solicitation - 2 bytes reserved */
|
2010-06-06 12:45:55 +00:00
|
|
|
/* 0 1 2 */
|
|
|
|
/* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 */
|
|
|
|
/* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */
|
2010-12-13 10:54:25 +00:00
|
|
|
/* | Flags | Reserved | Option(s)... */
|
2010-06-06 12:45:55 +00:00
|
|
|
/* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
buffer = UIP_ICMP_PAYLOAD;
|
2010-06-06 12:45:55 +00:00
|
|
|
buffer[0] = buffer[1] = 0;
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
if(addr == NULL) {
|
2011-01-25 09:55:12 +00:00
|
|
|
uip_create_linklocal_rplnodes_mcast(&tmpaddr);
|
2010-04-30 13:43:53 +00:00
|
|
|
addr = &tmpaddr;
|
|
|
|
}
|
2012-04-21 22:18:07 +00:00
|
|
|
|
|
|
|
PRINTF("RPL: Sending a DIS to ");
|
|
|
|
PRINT6ADDR(addr);
|
|
|
|
PRINTF("\n");
|
|
|
|
|
2010-06-06 12:45:55 +00:00
|
|
|
uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 2);
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
dio_input(void)
|
|
|
|
{
|
|
|
|
unsigned char *buffer;
|
|
|
|
uint8_t buffer_length;
|
|
|
|
rpl_dio_t dio;
|
|
|
|
uint8_t subopt_type;
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
uip_ipaddr_t from;
|
|
|
|
uip_ds6_nbr_t *nbr;
|
|
|
|
|
2011-09-05 18:28:50 +00:00
|
|
|
memset(&dio, 0, sizeof(dio));
|
|
|
|
|
2012-02-29 20:59:50 +00:00
|
|
|
/* Set default values in case the DIO configuration option is missing. */
|
|
|
|
dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
|
|
|
|
dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
|
|
|
|
dio.dag_redund = RPL_DIO_REDUNDANCY;
|
|
|
|
dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
|
|
|
|
dio.dag_max_rankinc = RPL_MAX_RANKINC;
|
|
|
|
dio.ocp = RPL_OF.ocp;
|
|
|
|
dio.default_lifetime = RPL_DEFAULT_LIFETIME;
|
|
|
|
dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
|
|
|
|
|
|
|
|
/* DAG Information Object */
|
|
|
|
PRINTF("RPL: Received a DIO from ");
|
|
|
|
PRINT6ADDR(&from);
|
|
|
|
PRINTF("\n");
|
|
|
|
|
|
|
|
if((nbr = uip_ds6_nbr_lookup(&from)) == NULL) {
|
|
|
|
if((nbr = uip_ds6_nbr_add(&from, (uip_lladdr_t *)
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER),
|
|
|
|
0, NBR_REACHABLE)) != NULL) {
|
|
|
|
/* set reachable timer */
|
2010-12-06 09:48:48 +00:00
|
|
|
stimer_set(&nbr->reachable, UIP_ND6_REACHABLE_TIME / 1000);
|
2010-04-30 13:43:53 +00:00
|
|
|
PRINTF("RPL: Neighbor added to neighbor cache ");
|
|
|
|
PRINT6ADDR(&from);
|
|
|
|
PRINTF(", ");
|
|
|
|
PRINTLLADDR((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
PRINTF("\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PRINTF("RPL: Neighbor already in neighbor cache\n");
|
|
|
|
}
|
|
|
|
|
2012-03-07 23:48:04 +00:00
|
|
|
buffer_length = uip_len - uip_l3_icmp_hdr_len;
|
2011-06-28 12:19:48 +00:00
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
/* Process the DIO base option. */
|
|
|
|
i = 0;
|
|
|
|
buffer = UIP_ICMP_PAYLOAD;
|
2010-06-01 22:30:02 +00:00
|
|
|
|
|
|
|
dio.instance_id = buffer[i++];
|
|
|
|
dio.version = buffer[i++];
|
2011-09-06 15:28:39 +00:00
|
|
|
dio.rank = get16(buffer, i);
|
2010-06-01 22:30:02 +00:00
|
|
|
i += 2;
|
|
|
|
|
2012-02-29 20:59:50 +00:00
|
|
|
PRINTF("RPL: Incoming DIO (id, ver, rank) = (%u,%u,%u)\n",
|
|
|
|
(unsigned)dio.instance_id,
|
|
|
|
(unsigned)dio.version,
|
|
|
|
(unsigned)dio.rank);
|
2010-06-14 12:44:37 +00:00
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
|
2010-12-13 10:54:25 +00:00
|
|
|
dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
|
|
|
|
dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
dio.dtsn = buffer[i++];
|
2010-06-01 22:30:02 +00:00
|
|
|
/* two reserved bytes */
|
|
|
|
i += 2;
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id));
|
|
|
|
i += sizeof(dio.dag_id);
|
|
|
|
|
2012-02-29 20:59:50 +00:00
|
|
|
PRINTF("RPL: Incoming DIO (dag_id, pref) = (");
|
2011-07-11 13:50:51 +00:00
|
|
|
PRINT6ADDR(&dio.dag_id);
|
2012-02-29 20:59:50 +00:00
|
|
|
PRINTF(", %u)\n", dio.preference);
|
2011-07-11 13:50:51 +00:00
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
/* Check if there are any DIO suboptions. */
|
|
|
|
for(; i < buffer_length; i += len) {
|
|
|
|
subopt_type = buffer[i];
|
2011-09-05 18:28:50 +00:00
|
|
|
if(subopt_type == RPL_OPTION_PAD1) {
|
2010-04-30 13:43:53 +00:00
|
|
|
len = 1;
|
|
|
|
} else {
|
2010-06-01 22:30:02 +00:00
|
|
|
/* Suboption with a two-byte header + payload */
|
|
|
|
len = 2 + buffer[i + 1];
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-27 00:46:40 +00:00
|
|
|
if(len + i > buffer_length) {
|
|
|
|
PRINTF("RPL: Invalid DIO packet\n");
|
|
|
|
RPL_STAT(rpl_stats.malformed_msgs++);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-05 18:28:50 +00:00
|
|
|
PRINTF("RPL: DIO option %u, length: %u\n", subopt_type, len - 2);
|
2011-02-15 00:13:30 +00:00
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
switch(subopt_type) {
|
2011-09-05 18:28:50 +00:00
|
|
|
case RPL_OPTION_DAG_METRIC_CONTAINER:
|
2011-02-13 22:17:56 +00:00
|
|
|
if(len < 6) {
|
2010-04-30 13:43:53 +00:00
|
|
|
PRINTF("RPL: Invalid DAG MC, len = %d\n", len);
|
2010-10-27 00:46:40 +00:00
|
|
|
RPL_STAT(rpl_stats.malformed_msgs++);
|
2010-04-30 13:43:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-02-15 00:13:30 +00:00
|
|
|
dio.mc.type = buffer[i + 2];
|
2011-06-28 15:49:32 +00:00
|
|
|
dio.mc.flags = buffer[i + 3] << 1;
|
|
|
|
dio.mc.flags |= buffer[i + 4] >> 7;
|
|
|
|
dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
|
2011-02-15 00:13:30 +00:00
|
|
|
dio.mc.prec = buffer[i + 4] & 0xf;
|
|
|
|
dio.mc.length = buffer[i + 5];
|
2011-03-15 00:16:20 +00:00
|
|
|
|
2011-02-15 00:13:30 +00:00
|
|
|
if(dio.mc.type == RPL_DAG_MC_ETX) {
|
2011-09-06 15:28:39 +00:00
|
|
|
dio.mc.obj.etx = get16(buffer, i + 6);
|
2011-02-15 00:13:30 +00:00
|
|
|
|
|
|
|
PRINTF("RPL: DAG MC: type %u, flags %u, aggr %u, prec %u, length %u, ETX %u\n",
|
|
|
|
(unsigned)dio.mc.type,
|
|
|
|
(unsigned)dio.mc.flags,
|
|
|
|
(unsigned)dio.mc.aggr,
|
|
|
|
(unsigned)dio.mc.prec,
|
|
|
|
(unsigned)dio.mc.length,
|
2011-03-15 00:16:20 +00:00
|
|
|
(unsigned)dio.mc.obj.etx);
|
|
|
|
} else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
|
|
|
|
dio.mc.obj.energy.flags = buffer[i + 6];
|
|
|
|
dio.mc.obj.energy.energy_est = buffer[i + 7];
|
2011-02-15 00:13:30 +00:00
|
|
|
} else {
|
|
|
|
PRINTF("RPL: Unhandled DAG MC type: %u\n", (unsigned)dio.mc.type);
|
|
|
|
return;
|
2011-02-13 22:17:56 +00:00
|
|
|
}
|
2010-04-30 13:43:53 +00:00
|
|
|
break;
|
2011-09-05 18:28:50 +00:00
|
|
|
case RPL_OPTION_ROUTE_INFO:
|
2010-04-30 13:43:53 +00:00
|
|
|
if(len < 9) {
|
|
|
|
PRINTF("RPL: Invalid destination prefix option, len = %d\n", len);
|
2010-10-27 00:46:40 +00:00
|
|
|
RPL_STAT(rpl_stats.malformed_msgs++);
|
2010-04-30 13:43:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-02-13 22:17:56 +00:00
|
|
|
|
2012-02-29 20:59:50 +00:00
|
|
|
/* The flags field includes the preference value. */
|
2010-06-01 22:30:02 +00:00
|
|
|
dio.destination_prefix.length = buffer[i + 2];
|
|
|
|
dio.destination_prefix.flags = buffer[i + 3];
|
2010-05-25 19:19:43 +00:00
|
|
|
dio.destination_prefix.lifetime = get32(buffer, i + 4);
|
2010-06-01 22:30:02 +00:00
|
|
|
|
2011-09-05 18:28:50 +00:00
|
|
|
if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
|
2010-05-25 19:19:43 +00:00
|
|
|
dio.destination_prefix.length <= 128) {
|
|
|
|
PRINTF("RPL: Copying destination prefix\n");
|
2010-06-01 22:30:02 +00:00
|
|
|
memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
|
2010-05-25 19:19:43 +00:00
|
|
|
(dio.destination_prefix.length + 7) / 8);
|
|
|
|
} else {
|
2012-02-29 20:59:50 +00:00
|
|
|
PRINTF("RPL: Invalid route info option, len = %d\n", len);
|
2010-10-27 00:46:40 +00:00
|
|
|
RPL_STAT(rpl_stats.malformed_msgs++);
|
|
|
|
return;
|
2010-05-25 19:19:43 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
break;
|
2011-09-05 18:28:50 +00:00
|
|
|
case RPL_OPTION_DAG_CONF:
|
2011-02-13 22:17:56 +00:00
|
|
|
if(len != 16) {
|
|
|
|
PRINTF("RPL: Invalid DAG configuration option, len = %d\n", len);
|
|
|
|
RPL_STAT(rpl_stats.malformed_msgs++);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-01 22:30:02 +00:00
|
|
|
/* Path control field not yet implemented - at i + 2 */
|
2010-04-30 13:43:53 +00:00
|
|
|
dio.dag_intdoubl = buffer[i + 3];
|
|
|
|
dio.dag_intmin = buffer[i + 4];
|
|
|
|
dio.dag_redund = buffer[i + 5];
|
2011-09-06 15:28:39 +00:00
|
|
|
dio.dag_max_rankinc = get16(buffer, i + 6);
|
|
|
|
dio.dag_min_hoprankinc = get16(buffer, i + 8);
|
|
|
|
dio.ocp = get16(buffer, i + 10);
|
2010-12-13 10:59:37 +00:00
|
|
|
/* buffer + 12 is reserved */
|
|
|
|
dio.default_lifetime = buffer[i + 13];
|
2011-09-06 15:28:39 +00:00
|
|
|
dio.lifetime_unit = get16(buffer, i + 14);
|
2012-02-29 20:59:50 +00:00
|
|
|
PRINTF("RPL: DAG conf:dbl=%d, min=%d red=%d maxinc=%d mininc=%d ocp=%d d_l=%u l_u=%u\n",
|
2010-06-12 10:55:46 +00:00
|
|
|
dio.dag_intdoubl, dio.dag_intmin, dio.dag_redund,
|
2010-12-13 10:59:37 +00:00
|
|
|
dio.dag_max_rankinc, dio.dag_min_hoprankinc, dio.ocp,
|
|
|
|
dio.default_lifetime, dio.lifetime_unit);
|
2010-04-30 13:43:53 +00:00
|
|
|
break;
|
2011-09-05 18:28:50 +00:00
|
|
|
case RPL_OPTION_PREFIX_INFO:
|
2010-06-01 22:30:02 +00:00
|
|
|
if(len != 32) {
|
2012-02-29 20:59:50 +00:00
|
|
|
PRINTF("RPL: DAG prefix info not ok, len != 32\n");
|
2010-10-27 00:46:40 +00:00
|
|
|
RPL_STAT(rpl_stats.malformed_msgs++);
|
2010-06-01 22:30:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dio.prefix_info.length = buffer[i + 2];
|
|
|
|
dio.prefix_info.flags = buffer[i + 3];
|
|
|
|
/* valid lifetime is ingnored for now - at i + 4 */
|
|
|
|
/* preferred lifetime stored in lifetime */
|
|
|
|
dio.prefix_info.lifetime = get32(buffer, i + 8);
|
|
|
|
/* 32-bit reserved at i + 12 */
|
|
|
|
PRINTF("RPL: Copying prefix information\n");
|
|
|
|
memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
|
|
|
|
break;
|
2011-02-15 00:13:30 +00:00
|
|
|
default:
|
|
|
|
PRINTF("RPL: Unsupported suboption type in DIO: %u\n",
|
|
|
|
(unsigned)subopt_type);
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 10:32:16 +00:00
|
|
|
#ifdef RPL_DEBUG_DIO_INPUT
|
|
|
|
RPL_DEBUG_DIO_INPUT(&from, &dio);
|
|
|
|
#endif
|
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
rpl_process_dio(&from, &dio);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2011-07-11 13:50:51 +00:00
|
|
|
dio_output(rpl_instance_t *instance, uip_ipaddr_t *uc_addr)
|
2010-04-30 13:43:53 +00:00
|
|
|
{
|
|
|
|
unsigned char *buffer;
|
|
|
|
int pos;
|
2012-02-29 20:59:50 +00:00
|
|
|
rpl_dag_t *dag = instance->current_dag;
|
2011-07-11 13:50:51 +00:00
|
|
|
#if !RPL_LEAF_ONLY
|
2010-04-30 13:43:53 +00:00
|
|
|
uip_ipaddr_t addr;
|
2011-07-11 13:50:51 +00:00
|
|
|
#endif /* !RPL_LEAF_ONLY */
|
|
|
|
|
|
|
|
#if RPL_LEAF_ONLY
|
2012-04-21 22:18:07 +00:00
|
|
|
/* In leaf mode, we send DIO message only as unicasts in response to
|
|
|
|
unicast DIS messages. */
|
2011-07-11 13:50:51 +00:00
|
|
|
if(uc_addr == NULL) {
|
2013-02-20 23:25:32 +00:00
|
|
|
PRINTF("RPL: LEAF ONLY have multicast addr: skip dio_output\n");
|
2011-07-11 13:50:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* RPL_LEAF_ONLY */
|
|
|
|
|
2010-12-15 19:57:40 +00:00
|
|
|
/* DAG Information Object */
|
2010-04-30 13:43:53 +00:00
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
buffer = UIP_ICMP_PAYLOAD;
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos++] = instance->instance_id;
|
2010-06-02 11:59:51 +00:00
|
|
|
buffer[pos++] = dag->version;
|
2011-11-21 14:09:19 +00:00
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
#if RPL_LEAF_ONLY
|
2013-02-20 23:25:32 +00:00
|
|
|
PRINTF("RPL: LEAF ONLY DIO rank set to INFINITE_RANK\n");
|
2011-11-21 13:59:31 +00:00
|
|
|
set16(buffer, pos, INFINITE_RANK);
|
2011-07-11 13:50:51 +00:00
|
|
|
#else /* RPL_LEAF_ONLY */
|
2011-09-06 15:28:39 +00:00
|
|
|
set16(buffer, pos, dag->rank);
|
2011-07-11 13:50:51 +00:00
|
|
|
#endif /* RPL_LEAF_ONLY */
|
2011-09-06 15:28:39 +00:00
|
|
|
pos += 2;
|
2010-06-01 22:30:02 +00:00
|
|
|
|
2010-12-13 09:59:46 +00:00
|
|
|
buffer[pos] = 0;
|
2010-04-30 13:43:53 +00:00
|
|
|
if(dag->grounded) {
|
2010-06-01 22:30:02 +00:00
|
|
|
buffer[pos] |= RPL_DIO_GROUNDED;
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
2010-12-13 10:54:25 +00:00
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos] |= instance->mop << RPL_DIO_MOP_SHIFT;
|
|
|
|
buffer[pos] |= dag->preference & RPL_DIO_PREFERENCE_MASK;
|
2010-06-01 22:30:02 +00:00
|
|
|
pos++;
|
2010-06-08 16:21:54 +00:00
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos++] = instance->dtsn_out;
|
|
|
|
|
2012-02-29 23:01:59 +00:00
|
|
|
/* always request new DAO to refresh route */
|
|
|
|
RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
|
2010-12-13 10:59:37 +00:00
|
|
|
|
2010-06-01 22:30:02 +00:00
|
|
|
/* reserved 2 bytes */
|
2010-12-13 10:59:37 +00:00
|
|
|
buffer[pos++] = 0; /* flags */
|
|
|
|
buffer[pos++] = 0; /* reserved */
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
|
|
|
|
pos += 16;
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
#if !RPL_LEAF_ONLY
|
|
|
|
if(instance->mc.type != RPL_DAG_MC_NONE) {
|
|
|
|
instance->of->update_metric_container(instance);
|
2011-02-15 00:13:30 +00:00
|
|
|
|
2011-09-05 18:28:50 +00:00
|
|
|
buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
|
2011-02-15 00:13:30 +00:00
|
|
|
buffer[pos++] = 6;
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos++] = instance->mc.type;
|
|
|
|
buffer[pos++] = instance->mc.flags >> 1;
|
|
|
|
buffer[pos] = (instance->mc.flags & 1) << 7;
|
|
|
|
buffer[pos++] |= (instance->mc.aggr << 4) | instance->mc.prec;
|
|
|
|
if(instance->mc.type == RPL_DAG_MC_ETX) {
|
2011-02-15 00:13:30 +00:00
|
|
|
buffer[pos++] = 2;
|
2011-11-21 13:59:31 +00:00
|
|
|
set16(buffer, pos, instance->mc.obj.etx);
|
2011-09-06 15:28:39 +00:00
|
|
|
pos += 2;
|
2011-07-11 13:50:51 +00:00
|
|
|
} else if(instance->mc.type == RPL_DAG_MC_ENERGY) {
|
2011-03-15 00:16:20 +00:00
|
|
|
buffer[pos++] = 2;
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos++] = instance->mc.obj.energy.flags;
|
|
|
|
buffer[pos++] = instance->mc.obj.energy.energy_est;
|
2011-02-15 00:13:30 +00:00
|
|
|
} else {
|
|
|
|
PRINTF("RPL: Unable to send DIO because of unhandled DAG MC type %u\n",
|
2011-07-11 13:50:51 +00:00
|
|
|
(unsigned)instance->mc.type);
|
2011-02-15 00:13:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-11-21 14:09:19 +00:00
|
|
|
#endif /* !RPL_LEAF_ONLY */
|
2011-02-15 00:13:30 +00:00
|
|
|
|
2011-09-06 15:28:39 +00:00
|
|
|
/* Always add a DAG configuration option. */
|
2011-09-05 18:28:50 +00:00
|
|
|
buffer[pos++] = RPL_OPTION_DAG_CONF;
|
2010-12-13 10:59:37 +00:00
|
|
|
buffer[pos++] = 14;
|
|
|
|
buffer[pos++] = 0; /* No Auth, PCS = 0 */
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos++] = instance->dio_intdoubl;
|
|
|
|
buffer[pos++] = instance->dio_intmin;
|
|
|
|
buffer[pos++] = instance->dio_redundancy;
|
2011-11-21 13:59:31 +00:00
|
|
|
set16(buffer, pos, instance->max_rankinc);
|
2011-09-06 15:28:39 +00:00
|
|
|
pos += 2;
|
2011-11-21 13:59:31 +00:00
|
|
|
set16(buffer, pos, instance->min_hoprankinc);
|
2011-09-06 15:28:39 +00:00
|
|
|
pos += 2;
|
2010-12-13 10:59:37 +00:00
|
|
|
/* OCP is in the DAG_CONF option */
|
2011-11-21 13:59:31 +00:00
|
|
|
set16(buffer, pos, instance->of->ocp);
|
2011-09-06 15:28:39 +00:00
|
|
|
pos += 2;
|
2010-12-13 10:59:37 +00:00
|
|
|
buffer[pos++] = 0; /* reserved */
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos++] = instance->default_lifetime;
|
2011-11-21 13:59:31 +00:00
|
|
|
set16(buffer, pos, instance->lifetime_unit);
|
2011-09-06 15:28:39 +00:00
|
|
|
pos += 2;
|
2010-06-01 22:30:02 +00:00
|
|
|
|
2011-06-28 15:49:32 +00:00
|
|
|
/* Check if we have a prefix to send also. */
|
2010-06-01 22:30:02 +00:00
|
|
|
if(dag->prefix_info.length > 0) {
|
2011-09-05 18:28:50 +00:00
|
|
|
buffer[pos++] = RPL_OPTION_PREFIX_INFO;
|
2010-06-01 22:30:02 +00:00
|
|
|
buffer[pos++] = 30; /* always 30 bytes + 2 long */
|
|
|
|
buffer[pos++] = dag->prefix_info.length;
|
|
|
|
buffer[pos++] = dag->prefix_info.flags;
|
|
|
|
set32(buffer, pos, dag->prefix_info.lifetime);
|
|
|
|
pos += 4;
|
|
|
|
set32(buffer, pos, dag->prefix_info.lifetime);
|
|
|
|
pos += 4;
|
|
|
|
memset(&buffer[pos], 0, 4);
|
2010-05-25 19:19:43 +00:00
|
|
|
pos += 4;
|
2010-06-14 12:44:37 +00:00
|
|
|
memcpy(&buffer[pos], &dag->prefix_info.prefix, 16);
|
2010-06-01 22:30:02 +00:00
|
|
|
pos += 16;
|
2010-06-14 12:44:37 +00:00
|
|
|
PRINTF("RPL: Sending prefix info in DIO for ");
|
2010-06-01 22:30:02 +00:00
|
|
|
PRINT6ADDR(&dag->prefix_info.prefix);
|
2010-05-25 19:19:43 +00:00
|
|
|
PRINTF("\n");
|
|
|
|
} else {
|
2010-06-14 12:44:37 +00:00
|
|
|
PRINTF("RPL: No prefix to announce (len %d)\n",
|
2010-06-01 22:30:02 +00:00
|
|
|
dag->prefix_info.length);
|
2010-05-25 19:19:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
#if RPL_LEAF_ONLY
|
2013-02-20 23:25:32 +00:00
|
|
|
#if (DEBUG) & DEBUG_PRINT
|
|
|
|
if(uc_addr == NULL) {
|
|
|
|
PRINTF("RPL: LEAF ONLY sending unicast-DIO from multicast-DIO\n");
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_PRINT */
|
2011-07-11 13:50:51 +00:00
|
|
|
PRINTF("RPL: Sending unicast-DIO with rank %u to ",
|
|
|
|
(unsigned)dag->rank);
|
|
|
|
PRINT6ADDR(uc_addr);
|
|
|
|
PRINTF("\n");
|
|
|
|
uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
|
|
|
|
#else /* RPL_LEAF_ONLY */
|
2010-04-30 13:43:53 +00:00
|
|
|
/* Unicast requests get unicast replies! */
|
|
|
|
if(uc_addr == NULL) {
|
2010-05-18 16:43:56 +00:00
|
|
|
PRINTF("RPL: Sending a multicast-DIO with rank %u\n",
|
2011-07-11 13:50:51 +00:00
|
|
|
(unsigned)instance->current_dag->rank);
|
2011-01-25 09:55:12 +00:00
|
|
|
uip_create_linklocal_rplnodes_mcast(&addr);
|
2010-04-30 13:43:53 +00:00
|
|
|
uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DIO, pos);
|
|
|
|
} else {
|
2010-05-25 19:19:43 +00:00
|
|
|
PRINTF("RPL: Sending unicast-DIO with rank %u to ",
|
2011-07-11 13:50:51 +00:00
|
|
|
(unsigned)instance->current_dag->rank);
|
2010-04-30 13:43:53 +00:00
|
|
|
PRINT6ADDR(uc_addr);
|
|
|
|
PRINTF("\n");
|
|
|
|
uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
|
|
|
|
}
|
2011-07-11 13:50:51 +00:00
|
|
|
#endif /* RPL_LEAF_ONLY */
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
dao_input(void)
|
|
|
|
{
|
2010-06-06 21:42:50 +00:00
|
|
|
uip_ipaddr_t dao_sender_addr;
|
2010-04-30 13:43:53 +00:00
|
|
|
rpl_dag_t *dag;
|
2011-07-11 13:50:51 +00:00
|
|
|
rpl_instance_t *instance;
|
2010-04-30 13:43:53 +00:00
|
|
|
unsigned char *buffer;
|
|
|
|
uint16_t sequence;
|
|
|
|
uint8_t instance_id;
|
2011-07-11 13:50:51 +00:00
|
|
|
uint8_t lifetime;
|
2010-04-30 13:43:53 +00:00
|
|
|
uint8_t prefixlen;
|
2010-06-03 14:49:15 +00:00
|
|
|
uint8_t flags;
|
|
|
|
uint8_t subopt_type;
|
2011-03-01 11:33:34 +00:00
|
|
|
uint8_t pathcontrol;
|
|
|
|
uint8_t pathsequence;
|
2010-04-30 13:43:53 +00:00
|
|
|
uip_ipaddr_t prefix;
|
|
|
|
uip_ds6_route_t *rep;
|
2010-06-03 14:49:15 +00:00
|
|
|
uint8_t buffer_length;
|
2010-04-30 13:43:53 +00:00
|
|
|
int pos;
|
2010-06-03 14:49:15 +00:00
|
|
|
int len;
|
|
|
|
int i;
|
2010-06-06 21:42:50 +00:00
|
|
|
int learned_from;
|
2010-10-25 19:49:12 +00:00
|
|
|
rpl_parent_t *p;
|
2010-06-03 14:49:15 +00:00
|
|
|
|
|
|
|
prefixlen = 0;
|
|
|
|
|
2010-06-06 21:42:50 +00:00
|
|
|
uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
|
2010-04-30 13:43:53 +00:00
|
|
|
|
|
|
|
/* Destination Advertisement Object */
|
|
|
|
PRINTF("RPL: Received a DAO from ");
|
2010-06-06 21:42:50 +00:00
|
|
|
PRINT6ADDR(&dao_sender_addr);
|
2010-04-30 13:43:53 +00:00
|
|
|
PRINTF("\n");
|
|
|
|
|
|
|
|
buffer = UIP_ICMP_PAYLOAD;
|
2012-03-07 23:48:04 +00:00
|
|
|
buffer_length = uip_len - uip_l3_icmp_hdr_len;
|
2011-07-11 13:50:51 +00:00
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
pos = 0;
|
|
|
|
instance_id = buffer[pos++];
|
2010-06-14 12:44:37 +00:00
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
instance = rpl_get_instance(instance_id);
|
|
|
|
if(instance == NULL) {
|
|
|
|
PRINTF("RPL: Ignoring a DAO for an unknown RPL instance(%u)\n",
|
2010-06-14 12:44:37 +00:00
|
|
|
instance_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
lifetime = instance->default_lifetime;
|
2011-06-28 15:08:44 +00:00
|
|
|
|
2010-06-03 14:49:15 +00:00
|
|
|
flags = buffer[pos++];
|
|
|
|
/* reserved */
|
2010-04-30 13:43:53 +00:00
|
|
|
pos++;
|
2010-06-03 14:49:15 +00:00
|
|
|
sequence = buffer[pos++];
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
dag = instance->current_dag;
|
2010-12-13 10:54:25 +00:00
|
|
|
/* Is the DAGID present? */
|
2010-06-03 14:49:15 +00:00
|
|
|
if(flags & RPL_DAO_D_FLAG) {
|
2011-07-11 13:50:51 +00:00
|
|
|
if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
|
2012-02-29 20:59:50 +00:00
|
|
|
PRINTF("RPL: Ignoring a DAO for a DAG different from ours\n");
|
2011-07-11 13:50:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-06-03 14:49:15 +00:00
|
|
|
pos += 16;
|
2011-07-11 13:50:51 +00:00
|
|
|
} else {
|
|
|
|
/* Perhaps, there are verification to do but ... */
|
2010-06-03 14:49:15 +00:00
|
|
|
}
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2011-09-05 18:28:50 +00:00
|
|
|
/* Check if there are any RPL options present. */
|
2010-06-03 14:49:15 +00:00
|
|
|
i = pos;
|
|
|
|
for(; i < buffer_length; i += len) {
|
|
|
|
subopt_type = buffer[i];
|
2011-09-05 18:28:50 +00:00
|
|
|
if(subopt_type == RPL_OPTION_PAD1) {
|
2010-06-03 14:49:15 +00:00
|
|
|
len = 1;
|
|
|
|
} else {
|
2011-09-05 18:28:50 +00:00
|
|
|
/* The option consists of a two-byte header and a payload. */
|
2010-06-03 14:49:15 +00:00
|
|
|
len = 2 + buffer[i + 1];
|
|
|
|
}
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2010-06-03 14:49:15 +00:00
|
|
|
switch(subopt_type) {
|
2011-09-05 18:28:50 +00:00
|
|
|
case RPL_OPTION_TARGET:
|
2011-11-21 13:59:31 +00:00
|
|
|
/* Handle the target option. */
|
2010-06-03 14:49:15 +00:00
|
|
|
prefixlen = buffer[i + 3];
|
|
|
|
memset(&prefix, 0, sizeof(prefix));
|
2010-06-06 12:45:55 +00:00
|
|
|
memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
|
2010-06-03 14:49:15 +00:00
|
|
|
break;
|
2011-09-05 18:28:50 +00:00
|
|
|
case RPL_OPTION_TRANSIT:
|
2011-06-28 15:08:44 +00:00
|
|
|
/* The path sequence and control are ignored. */
|
2011-03-01 11:33:34 +00:00
|
|
|
pathcontrol = buffer[i + 3];
|
|
|
|
pathsequence = buffer[i + 4];
|
|
|
|
lifetime = buffer[i + 5];
|
2011-06-28 15:08:44 +00:00
|
|
|
/* The parent address is also ignored. */
|
2010-06-03 14:49:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2011-06-28 15:08:44 +00:00
|
|
|
PRINTF("RPL: DAO lifetime: %u, prefix length: %u prefix: ",
|
2011-07-11 13:50:51 +00:00
|
|
|
(unsigned)lifetime, (unsigned)prefixlen);
|
2010-06-06 12:45:55 +00:00
|
|
|
PRINT6ADDR(&prefix);
|
2010-05-24 16:38:56 +00:00
|
|
|
PRINTF("\n");
|
|
|
|
|
2011-06-28 15:08:44 +00:00
|
|
|
rep = uip_ds6_route_lookup(&prefix);
|
|
|
|
|
2012-02-29 20:59:50 +00:00
|
|
|
if(lifetime == RPL_ZERO_LIFETIME) {
|
2010-12-17 15:24:25 +00:00
|
|
|
/* No-Path DAO received; invoke the route purging routine. */
|
2011-11-21 13:59:31 +00:00
|
|
|
if(rep != NULL && rep->state.saved_lifetime == 0 && rep->length == prefixlen) {
|
2010-05-18 16:43:56 +00:00
|
|
|
PRINTF("RPL: Setting expiration timer for prefix ");
|
|
|
|
PRINT6ADDR(&prefix);
|
|
|
|
PRINTF("\n");
|
|
|
|
rep->state.saved_lifetime = rep->state.lifetime;
|
|
|
|
rep->state.lifetime = DAO_EXPIRATION_TIMEOUT;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-06 21:42:50 +00:00
|
|
|
learned_from = uip_is_addr_mcast(&dao_sender_addr) ?
|
|
|
|
RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
|
|
|
|
|
2010-10-25 19:49:12 +00:00
|
|
|
if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
|
2011-06-28 15:08:44 +00:00
|
|
|
/* Check whether this is a DAO forwarding loop. */
|
2010-10-25 19:49:12 +00:00
|
|
|
p = rpl_find_parent(dag, &dao_sender_addr);
|
2011-07-11 13:50:51 +00:00
|
|
|
/* check if this is a new DAO registration with an "illegal" rank */
|
|
|
|
/* if we already route to this node it is likely */
|
|
|
|
if(p != NULL && DAG_RANK(p->rank, instance) < DAG_RANK(dag->rank, instance)) {
|
2010-12-04 21:06:04 +00:00
|
|
|
PRINTF("RPL: Loop detected when receiving a unicast DAO from a node with a lower rank! (%u < %u)\n",
|
2011-07-11 13:50:51 +00:00
|
|
|
DAG_RANK(p->rank, instance), DAG_RANK(dag->rank, instance));
|
2011-02-07 16:29:23 +00:00
|
|
|
p->rank = INFINITE_RANK;
|
|
|
|
p->updated = 1;
|
2010-10-25 19:49:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 08:05:34 +00:00
|
|
|
rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
|
2010-04-30 13:43:53 +00:00
|
|
|
if(rep == NULL) {
|
2011-11-16 08:05:34 +00:00
|
|
|
RPL_STAT(rpl_stats.mem_overflows++);
|
|
|
|
PRINTF("RPL: Could not add a route after receiving a DAO\n");
|
|
|
|
return;
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
rep->state.lifetime = RPL_LIFETIME(instance, lifetime);
|
2011-06-28 15:08:44 +00:00
|
|
|
rep->state.learned_from = learned_from;
|
|
|
|
|
2010-06-06 21:42:50 +00:00
|
|
|
if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
|
2010-06-14 12:44:37 +00:00
|
|
|
if(dag->preferred_parent) {
|
2010-04-30 13:43:53 +00:00
|
|
|
PRINTF("RPL: Forwarding DAO to parent ");
|
2010-06-14 12:44:37 +00:00
|
|
|
PRINT6ADDR(&dag->preferred_parent->addr);
|
2010-04-30 13:43:53 +00:00
|
|
|
PRINTF("\n");
|
2010-06-14 12:44:37 +00:00
|
|
|
uip_icmp6_send(&dag->preferred_parent->addr,
|
2010-10-25 19:49:12 +00:00
|
|
|
ICMP6_RPL, RPL_CODE_DAO, buffer_length);
|
2011-07-11 13:50:51 +00:00
|
|
|
}
|
|
|
|
if(flags & RPL_DAO_K_FLAG) {
|
|
|
|
dao_ack_output(instance, &dao_sender_addr, sequence);
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2011-07-11 13:50:51 +00:00
|
|
|
dao_output(rpl_parent_t *n, uint8_t lifetime)
|
2010-04-30 13:43:53 +00:00
|
|
|
{
|
|
|
|
rpl_dag_t *dag;
|
2011-07-11 13:50:51 +00:00
|
|
|
rpl_instance_t *instance;
|
2010-04-30 13:43:53 +00:00
|
|
|
unsigned char *buffer;
|
|
|
|
uint8_t prefixlen;
|
|
|
|
uip_ipaddr_t prefix;
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
/* Destination Advertisement Object */
|
2012-04-17 21:04:20 +00:00
|
|
|
|
2010-06-03 14:49:15 +00:00
|
|
|
if(get_global_addr(&prefix) == 0) {
|
|
|
|
PRINTF("RPL: No global address set for this node - suppressing DAO\n");
|
|
|
|
return;
|
|
|
|
}
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2012-04-17 21:04:20 +00:00
|
|
|
dag = n->dag;
|
2011-07-11 13:50:51 +00:00
|
|
|
instance = dag->instance;
|
|
|
|
|
2011-10-27 10:32:16 +00:00
|
|
|
#ifdef RPL_DEBUG_DAO_OUTPUT
|
|
|
|
RPL_DEBUG_DAO_OUTPUT(n);
|
|
|
|
#endif
|
|
|
|
|
2010-04-30 13:43:53 +00:00
|
|
|
buffer = UIP_ICMP_PAYLOAD;
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
RPL_LOLLIPOP_INCREMENT(dao_sequence);
|
2010-04-30 13:43:53 +00:00
|
|
|
pos = 0;
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos++] = instance->instance_id;
|
|
|
|
buffer[pos] = 0;
|
2012-02-29 20:59:50 +00:00
|
|
|
#if RPL_DAO_SPECIFY_DAG
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos] |= RPL_DAO_D_FLAG;
|
2012-02-29 20:59:50 +00:00
|
|
|
#endif /* RPL_DAO_SPECIFY_DAG */
|
2010-06-14 12:44:37 +00:00
|
|
|
#if RPL_CONF_DAO_ACK
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos] |= RPL_DAO_K_FLAG;
|
|
|
|
#endif /* RPL_CONF_DAO_ACK */
|
|
|
|
++pos;
|
2010-06-03 14:49:15 +00:00
|
|
|
buffer[pos++] = 0; /* reserved */
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[pos++] = dao_sequence;
|
2012-02-29 20:59:50 +00:00
|
|
|
#if RPL_DAO_SPECIFY_DAG
|
2011-07-11 13:50:51 +00:00
|
|
|
memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
|
|
|
|
pos+=sizeof(dag->dag_id);
|
2012-02-29 20:59:50 +00:00
|
|
|
#endif /* RPL_DAO_SPECIFY_DAG */
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2010-06-03 14:49:15 +00:00
|
|
|
/* create target subopt */
|
2010-05-18 16:43:56 +00:00
|
|
|
prefixlen = sizeof(prefix) * CHAR_BIT;
|
2011-09-05 18:28:50 +00:00
|
|
|
buffer[pos++] = RPL_OPTION_TARGET;
|
2010-06-03 14:49:15 +00:00
|
|
|
buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
|
|
|
|
buffer[pos++] = 0; /* reserved */
|
2010-04-30 13:43:53 +00:00
|
|
|
buffer[pos++] = prefixlen;
|
2010-06-03 14:49:15 +00:00
|
|
|
memcpy(buffer + pos, &prefix, (prefixlen + 7) / CHAR_BIT);
|
|
|
|
pos += ((prefixlen + 7) / CHAR_BIT);
|
|
|
|
|
2011-06-28 15:08:44 +00:00
|
|
|
/* Create a transit information sub-option. */
|
2011-09-05 18:28:50 +00:00
|
|
|
buffer[pos++] = RPL_OPTION_TRANSIT;
|
2011-03-01 11:33:34 +00:00
|
|
|
buffer[pos++] = 4;
|
|
|
|
buffer[pos++] = 0; /* flags - ignored */
|
2010-06-03 14:49:15 +00:00
|
|
|
buffer[pos++] = 0; /* path control - ignored */
|
2011-03-01 11:33:34 +00:00
|
|
|
buffer[pos++] = 0; /* path seq - ignored */
|
2011-06-28 15:08:44 +00:00
|
|
|
buffer[pos++] = lifetime;
|
2010-04-30 13:43:53 +00:00
|
|
|
|
2010-05-18 16:43:56 +00:00
|
|
|
PRINTF("RPL: Sending DAO with prefix ");
|
|
|
|
PRINT6ADDR(&prefix);
|
|
|
|
PRINTF(" to ");
|
2012-04-17 21:04:20 +00:00
|
|
|
PRINT6ADDR(&n->addr);
|
2010-05-18 16:43:56 +00:00
|
|
|
PRINTF("\n");
|
|
|
|
|
2012-04-17 21:04:20 +00:00
|
|
|
uip_icmp6_send(&n->addr, ICMP6_RPL, RPL_CODE_DAO, pos);
|
2010-04-30 13:43:53 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-06-06 21:42:50 +00:00
|
|
|
static void
|
|
|
|
dao_ack_input(void)
|
|
|
|
{
|
|
|
|
unsigned char *buffer;
|
|
|
|
uint8_t buffer_length;
|
|
|
|
uint8_t instance_id;
|
|
|
|
uint8_t sequence;
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
buffer = UIP_ICMP_PAYLOAD;
|
2012-03-07 23:48:04 +00:00
|
|
|
buffer_length = uip_len - uip_l3_icmp_hdr_len;
|
2010-06-06 21:42:50 +00:00
|
|
|
|
|
|
|
instance_id = buffer[0];
|
|
|
|
sequence = buffer[2];
|
|
|
|
status = buffer[3];
|
|
|
|
|
|
|
|
PRINTF("RPL: Received a DAO ACK with sequence number %d and status %d from ",
|
|
|
|
sequence, status);
|
|
|
|
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2011-07-11 13:50:51 +00:00
|
|
|
dao_ack_output(rpl_instance_t *instance, uip_ipaddr_t *dest, uint8_t sequence)
|
2010-06-06 21:42:50 +00:00
|
|
|
{
|
|
|
|
unsigned char *buffer;
|
|
|
|
|
|
|
|
PRINTF("RPL: Sending a DAO ACK with sequence number %d to ", sequence);
|
|
|
|
PRINT6ADDR(dest);
|
|
|
|
PRINTF("\n");
|
|
|
|
|
|
|
|
buffer = UIP_ICMP_PAYLOAD;
|
|
|
|
|
2011-07-11 13:50:51 +00:00
|
|
|
buffer[0] = instance->instance_id;
|
2010-06-06 21:42:50 +00:00
|
|
|
buffer[1] = 0;
|
|
|
|
buffer[2] = sequence;
|
|
|
|
buffer[3] = 0;
|
|
|
|
|
|
|
|
uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 13:43:53 +00:00
|
|
|
void
|
|
|
|
uip_rpl_input(void)
|
|
|
|
{
|
|
|
|
PRINTF("Received an RPL control message\n");
|
|
|
|
switch(UIP_ICMP_BUF->icode) {
|
|
|
|
case RPL_CODE_DIO:
|
|
|
|
dio_input();
|
|
|
|
break;
|
|
|
|
case RPL_CODE_DIS:
|
|
|
|
dis_input();
|
|
|
|
break;
|
|
|
|
case RPL_CODE_DAO:
|
|
|
|
dao_input();
|
|
|
|
break;
|
2010-06-06 21:42:50 +00:00
|
|
|
case RPL_CODE_DAO_ACK:
|
|
|
|
dao_ack_input();
|
|
|
|
break;
|
2010-04-30 13:43:53 +00:00
|
|
|
default:
|
|
|
|
PRINTF("RPL: received an unknown ICMP6 code (%u)\n", UIP_ICMP_BUF->icode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uip_len = 0;
|
|
|
|
}
|
2013-03-17 22:21:30 +00:00
|
|
|
#endif /* UIP_CONF_IPV6 */
|