2011-11-16 08:00:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
2014-11-08 00:15:42 +00:00
|
|
|
|
2011-11-16 08:00:19 +00:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Management of extension headers for ContikiRPL.
|
|
|
|
*
|
2011-12-02 14:55:07 +00:00
|
|
|
* \author Vincent Brillault <vincent.brillault@imag.fr>,
|
|
|
|
* Joakim Eriksson <joakime@sics.se>,
|
|
|
|
* Niclas Finne <nfi@sics.se>,
|
|
|
|
* Nicolas Tsiftes <nvt@sics.se>.
|
2011-11-16 08:00:19 +00:00
|
|
|
*/
|
|
|
|
|
2014-11-08 00:15:42 +00:00
|
|
|
/**
|
2017-09-29 20:18:48 +00:00
|
|
|
* \addtogroup uip
|
2014-11-08 00:15:42 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2017-12-13 20:00:33 +00:00
|
|
|
#include "net/routing/routing.h"
|
2017-09-22 13:06:44 +00:00
|
|
|
#include "net/ipv6/uip.h"
|
|
|
|
#include "net/ipv6/tcpip.h"
|
2013-11-22 08:17:54 +00:00
|
|
|
#include "net/ipv6/uip-ds6.h"
|
2017-12-13 20:00:33 +00:00
|
|
|
#include "net/ipv6/uip-sr.h"
|
2017-12-09 12:55:57 +00:00
|
|
|
#include "net/routing/rpl-classic/rpl-private.h"
|
2014-01-31 08:38:42 +00:00
|
|
|
#include "net/packetbuf.h"
|
2011-11-16 08:00:19 +00:00
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
#include "sys/log.h"
|
|
|
|
|
|
|
|
#define LOG_MODULE "RPL"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_RPL
|
2011-11-16 08:00:19 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-11-16 08:00:19 +00:00
|
|
|
int
|
2018-10-18 16:29:37 +00:00
|
|
|
rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset)
|
2011-11-16 08:00:19 +00:00
|
|
|
{
|
|
|
|
rpl_instance_t *instance;
|
2011-12-02 15:16:42 +00:00
|
|
|
int down;
|
2014-01-22 11:16:27 +00:00
|
|
|
uint16_t sender_rank;
|
2012-01-13 13:37:30 +00:00
|
|
|
uint8_t sender_closer;
|
2013-11-20 14:14:11 +00:00
|
|
|
uip_ds6_route_t *route;
|
2018-10-18 16:29:37 +00:00
|
|
|
rpl_parent_t *sender;
|
|
|
|
struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)ext_buf;
|
|
|
|
struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset);
|
2011-11-16 08:00:19 +00:00
|
|
|
|
2018-10-18 16:29:37 +00:00
|
|
|
if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
|
|
|
|
|| rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL
|
|
|
|
|| rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
|
2014-05-09 09:28:36 +00:00
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("Hop-by-hop extension header has wrong size or type (%u %u %u)\n",
|
2018-10-18 16:29:37 +00:00
|
|
|
hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len);
|
2016-06-13 10:04:19 +00:00
|
|
|
return 0; /* Drop */
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 16:29:37 +00:00
|
|
|
instance = rpl_get_instance(rpl_opt->instance);
|
2011-11-16 08:00:19 +00:00
|
|
|
if(instance == NULL) {
|
2018-10-18 16:29:37 +00:00
|
|
|
LOG_ERR("Unknown instance: %u\n", rpl_opt->instance);
|
2016-06-13 10:04:19 +00:00
|
|
|
return 0;
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 16:29:37 +00:00
|
|
|
if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("Forward error!\n");
|
2013-08-11 22:06:12 +00:00
|
|
|
/* We should try to repair it by removing the neighbor that caused
|
2016-02-01 17:02:33 +00:00
|
|
|
the packet to be forwareded in the first place. We drop any
|
|
|
|
routes that go through the neighbor that sent the packet to
|
|
|
|
us. */
|
|
|
|
if(RPL_IS_STORING(instance)) {
|
|
|
|
route = uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr);
|
|
|
|
if(route != NULL) {
|
|
|
|
uip_ds6_route_rm(route);
|
|
|
|
}
|
2013-08-11 22:06:12 +00:00
|
|
|
}
|
2014-01-22 14:26:43 +00:00
|
|
|
RPL_STAT(rpl_stats.forward_errors++);
|
|
|
|
/* Trigger DAO retransmission */
|
|
|
|
rpl_reset_dio_timer(instance);
|
|
|
|
/* drop the packet as it is not routable */
|
2016-06-13 10:04:19 +00:00
|
|
|
return 0;
|
2013-08-11 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-16 08:00:19 +00:00
|
|
|
if(!instance->current_dag->joined) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("No DAG in the instance\n");
|
2016-06-13 10:04:19 +00:00
|
|
|
return 0;
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
2011-12-02 15:16:42 +00:00
|
|
|
down = 0;
|
2018-10-18 16:29:37 +00:00
|
|
|
if(rpl_opt->flags & RPL_HDR_OPT_DOWN) {
|
2011-12-02 15:16:42 +00:00
|
|
|
down = 1;
|
|
|
|
}
|
|
|
|
|
2018-10-18 16:29:37 +00:00
|
|
|
sender_rank = UIP_HTONS(rpl_opt->senderrank);
|
2015-09-17 20:42:19 +00:00
|
|
|
sender = nbr_table_get_from_lladdr(rpl_parents, packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
|
2018-10-18 16:29:37 +00:00
|
|
|
if(sender != NULL && (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR)) {
|
2015-09-17 20:42:19 +00:00
|
|
|
/* A rank error was signalled, attempt to repair it by updating
|
|
|
|
* the sender's rank from ext header */
|
|
|
|
sender->rank = sender_rank;
|
2016-06-03 19:18:15 +00:00
|
|
|
if(RPL_IS_NON_STORING(instance)) {
|
|
|
|
/* Select DAG and preferred parent only in non-storing mode. In storing mode,
|
|
|
|
* a parent switch would result in an immediate No-path DAO transmission, dropping
|
|
|
|
* current incoming packet. */
|
|
|
|
rpl_select_dag(instance, sender);
|
|
|
|
}
|
2015-09-17 20:42:19 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 21:51:39 +00:00
|
|
|
sender_closer = sender_rank < instance->current_dag->rank;
|
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("Packet going %s, sender closer %d (%d < %d)\n", down == 1 ? "down" : "up",
|
2016-02-04 21:51:39 +00:00
|
|
|
sender_closer,
|
|
|
|
sender_rank,
|
|
|
|
instance->current_dag->rank
|
|
|
|
);
|
|
|
|
|
2012-01-13 13:37:30 +00:00
|
|
|
if((down && !sender_closer) || (!down && sender_closer)) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_WARN("Loop detected - senderrank: %d my-rank: %d sender_closer: %d\n",
|
2016-06-27 18:57:48 +00:00
|
|
|
sender_rank, instance->current_dag->rank,
|
|
|
|
sender_closer);
|
2015-09-17 20:42:19 +00:00
|
|
|
/* Attempt to repair the loop by sending a unicast DIO back to the sender
|
|
|
|
* so that it gets a fresh update of our rank. */
|
|
|
|
if(sender != NULL) {
|
|
|
|
instance->unicast_dio_target = sender;
|
|
|
|
rpl_schedule_unicast_dio_immediately(instance);
|
|
|
|
}
|
2018-10-18 16:29:37 +00:00
|
|
|
if(rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) {
|
2014-01-16 08:24:55 +00:00
|
|
|
RPL_STAT(rpl_stats.loop_errors++);
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR(" Rank error signalled in RPL option!\n");
|
2014-01-31 08:37:35 +00:00
|
|
|
/* Packet must be dropped and dio trickle timer reset, see RFC6550 - 11.2.2.2 */
|
2013-08-11 22:07:13 +00:00
|
|
|
rpl_reset_dio_timer(instance);
|
2016-06-13 10:04:19 +00:00
|
|
|
return 0;
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_WARN("Single error tolerated\n");
|
2014-01-16 08:24:55 +00:00
|
|
|
RPL_STAT(rpl_stats.loop_warnings++);
|
2018-10-18 16:29:37 +00:00
|
|
|
rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR;
|
2016-06-13 10:04:19 +00:00
|
|
|
return 1;
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("Rank OK\n");
|
2016-06-13 10:04:19 +00:00
|
|
|
return 1;
|
2016-02-01 17:02:33 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr)
|
2016-02-01 17:02:33 +00:00
|
|
|
{
|
2017-12-10 15:32:43 +00:00
|
|
|
#if RPL_WITH_NON_STORING
|
2018-10-17 16:09:40 +00:00
|
|
|
struct uip_routing_hdr *rh_header;
|
2016-02-15 18:04:40 +00:00
|
|
|
rpl_dag_t *dag;
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *dest_node;
|
|
|
|
uip_sr_node_t *root_node;
|
2011-11-16 08:00:19 +00:00
|
|
|
|
2018-10-17 16:09:40 +00:00
|
|
|
/* Look for routing ext header */
|
2018-10-18 15:48:08 +00:00
|
|
|
rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING);
|
2016-02-01 17:02:33 +00:00
|
|
|
|
2016-02-16 15:02:20 +00:00
|
|
|
dag = rpl_get_dag(&UIP_IP_BUF->destipaddr);
|
2017-12-13 20:00:33 +00:00
|
|
|
root_node = uip_sr_get_node(dag, &dag->dag_id);
|
|
|
|
dest_node = uip_sr_get_node(dag, &UIP_IP_BUF->destipaddr);
|
2016-02-16 14:11:37 +00:00
|
|
|
|
2018-10-17 16:09:40 +00:00
|
|
|
if((rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) ||
|
2016-02-16 15:02:20 +00:00
|
|
|
(dest_node != NULL && root_node != NULL &&
|
|
|
|
dest_node->parent == root_node)) {
|
2016-02-16 14:11:37 +00:00
|
|
|
/* Routing header found or the packet destined for a direct child of the root.
|
|
|
|
* The next hop should be already copied as the IPv6 destination
|
2017-06-10 12:35:39 +00:00
|
|
|
* address, via rpl_ext_header_srh_update. We turn this address into a link-local to enable
|
2016-02-01 17:02:33 +00:00
|
|
|
* forwarding to next hop */
|
|
|
|
uip_ipaddr_copy(ipaddr, &UIP_IP_BUF->destipaddr);
|
|
|
|
uip_create_linklocal_prefix(ipaddr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-11-16 08:00:19 +00:00
|
|
|
return 0;
|
2017-12-10 15:32:43 +00:00
|
|
|
#else /* RPL_WITH_NON_STORING */
|
|
|
|
return 0; /* SRH not found */
|
|
|
|
#endif /* RPL_WITH_NON_STORING */
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-02-01 17:02:33 +00:00
|
|
|
int
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_ext_header_srh_update(void)
|
2011-12-02 14:55:07 +00:00
|
|
|
{
|
2017-12-10 15:32:43 +00:00
|
|
|
#if RPL_WITH_NON_STORING
|
2018-10-17 16:09:40 +00:00
|
|
|
struct uip_routing_hdr *rh_header;
|
|
|
|
struct uip_rpl_srh_hdr *srh_header;
|
2016-02-01 17:02:33 +00:00
|
|
|
|
2018-10-17 16:09:40 +00:00
|
|
|
/* Look for routing ext header */
|
2018-10-18 15:48:08 +00:00
|
|
|
rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING);
|
2018-10-17 16:09:40 +00:00
|
|
|
|
|
|
|
if(rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) {
|
2016-02-01 17:02:33 +00:00
|
|
|
/* SRH found, now look for next hop */
|
|
|
|
uint8_t cmpri, cmpre;
|
|
|
|
uint8_t ext_len;
|
|
|
|
uint8_t padding;
|
|
|
|
uint8_t path_len;
|
|
|
|
uint8_t segments_left;
|
|
|
|
uip_ipaddr_t current_dest_addr;
|
|
|
|
|
2018-10-17 16:09:40 +00:00
|
|
|
srh_header = (struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN);
|
|
|
|
segments_left = rh_header->seg_left;
|
|
|
|
ext_len = rh_header->len * 8 + 8;
|
|
|
|
cmpri = srh_header->cmpr >> 4;
|
|
|
|
cmpre = srh_header->cmpr & 0x0f;
|
|
|
|
padding = srh_header->pad >> 4;
|
2016-02-01 17:02:33 +00:00
|
|
|
path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1;
|
|
|
|
(void)path_len;
|
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("read SRH, path len %u, segments left %u, Cmpri %u, Cmpre %u, ext len %u (padding %u)\n",
|
2016-02-01 17:02:33 +00:00
|
|
|
path_len, segments_left, cmpri, cmpre, ext_len, padding);
|
|
|
|
|
|
|
|
if(segments_left == 0) {
|
|
|
|
/* We are the final destination, do nothing */
|
|
|
|
} else {
|
|
|
|
uint8_t i = path_len - segments_left; /* The index of the next address to be visited */
|
2018-10-17 16:09:40 +00:00
|
|
|
uint8_t *addr_ptr = ((uint8_t *)rh_header) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri));
|
2016-02-01 17:02:33 +00:00
|
|
|
uint8_t cmpr = segments_left == 1 ? cmpre : cmpri;
|
|
|
|
|
|
|
|
/* As per RFC6554: swap the IPv6 destination address and address[i] */
|
|
|
|
|
|
|
|
/* First, copy the current IPv6 destination address */
|
|
|
|
uip_ipaddr_copy(¤t_dest_addr, &UIP_IP_BUF->destipaddr);
|
|
|
|
/* Second, update the IPv6 destination address with addresses[i] */
|
|
|
|
memcpy(((uint8_t *)&UIP_IP_BUF->destipaddr) + cmpr, addr_ptr, 16 - cmpr);
|
|
|
|
/* Third, write current_dest_addr to addresses[i] */
|
|
|
|
memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr);
|
|
|
|
|
|
|
|
/* Update segments left field */
|
2018-10-17 16:09:40 +00:00
|
|
|
rh_header->seg_left--;
|
2016-02-01 17:02:33 +00:00
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_INFO("SRH next hop ");
|
|
|
|
LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
|
|
|
|
LOG_INFO_("\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-12-10 15:32:43 +00:00
|
|
|
#else /* RPL_WITH_NON_STORING */
|
|
|
|
return 0; /* SRH not found */
|
|
|
|
#endif /* RPL_WITH_NON_STORING */
|
2016-02-01 17:02:33 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
count_matching_bytes(const void *p1, const void *p2, size_t n)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for(i = 0; i < n; i++) {
|
|
|
|
if(((uint8_t *)p1)[i] != ((uint8_t *)p2)[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
insert_srh_header(void)
|
|
|
|
{
|
2018-10-18 09:31:06 +00:00
|
|
|
/* Implementation of RFC6554 */
|
2016-02-01 17:02:33 +00:00
|
|
|
uint8_t path_len;
|
|
|
|
uint8_t ext_len;
|
|
|
|
uint8_t cmpri, cmpre; /* ComprI and ComprE fields of the RPL Source Routing Header */
|
|
|
|
uint8_t *hop_ptr;
|
|
|
|
uint8_t padding;
|
2017-12-13 20:00:33 +00:00
|
|
|
uip_sr_node_t *dest_node;
|
|
|
|
uip_sr_node_t *root_node;
|
|
|
|
uip_sr_node_t *node;
|
2016-02-01 17:02:33 +00:00
|
|
|
rpl_dag_t *dag;
|
|
|
|
uip_ipaddr_t node_addr;
|
|
|
|
|
2018-10-18 17:11:56 +00:00
|
|
|
/* Always insest SRH as first extension header */
|
|
|
|
struct uip_routing_hdr *rh_hdr = (struct uip_routing_hdr *)UIP_IP_PAYLOAD(0);
|
|
|
|
struct uip_rpl_srh_hdr *srh_hdr = (struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN);
|
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_INFO("SRH creating source routing header with destination ");
|
|
|
|
LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
|
|
|
|
LOG_INFO_("\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
|
|
|
|
/* Construct source route. We do not do this recursively to keep the runtime stack usage constant. */
|
|
|
|
|
|
|
|
/* Get link of the destination and root */
|
|
|
|
dag = rpl_get_dag(&UIP_IP_BUF->destipaddr);
|
|
|
|
|
|
|
|
if(dag == NULL) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("SRH DAG not found\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-12-02 14:55:07 +00:00
|
|
|
|
2017-12-13 20:00:33 +00:00
|
|
|
dest_node = uip_sr_get_node(dag, &UIP_IP_BUF->destipaddr);
|
2016-02-01 17:02:33 +00:00
|
|
|
if(dest_node == NULL) {
|
|
|
|
/* The destination is not found, skip SRH insertion */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:00:33 +00:00
|
|
|
root_node = uip_sr_get_node(dag, &dag->dag_id);
|
2016-02-01 17:02:33 +00:00
|
|
|
if(root_node == NULL) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("SRH root node not found\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:00:33 +00:00
|
|
|
if(!uip_sr_is_addr_reachable(dag, &UIP_IP_BUF->destipaddr)) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("SRH no path found to destination\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute path length and compression factors (we use cmpri == cmpre) */
|
|
|
|
path_len = 0;
|
|
|
|
node = dest_node->parent;
|
|
|
|
/* For simplicity, we use cmpri = cmpre */
|
|
|
|
cmpri = 15;
|
|
|
|
cmpre = 15;
|
2016-02-15 18:04:40 +00:00
|
|
|
|
2016-02-16 14:14:06 +00:00
|
|
|
if(node == root_node) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("SRH no need to insert SRH\n");
|
2016-06-28 12:59:23 +00:00
|
|
|
return 1;
|
2016-02-15 18:04:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 17:02:33 +00:00
|
|
|
while(node != NULL && node != root_node) {
|
|
|
|
|
2017-12-13 20:00:33 +00:00
|
|
|
NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
|
2016-02-01 17:02:33 +00:00
|
|
|
|
|
|
|
/* How many bytes in common between all nodes in the path? */
|
|
|
|
cmpri = MIN(cmpri, count_matching_bytes(&node_addr, &UIP_IP_BUF->destipaddr, 16));
|
|
|
|
cmpre = cmpri;
|
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("SRH Hop ");
|
|
|
|
LOG_DBG_6ADDR(&node_addr);
|
|
|
|
LOG_DBG_("\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
node = node->parent;
|
|
|
|
path_len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extension header length: fixed headers + (n-1) * (16-ComprI) + (16-ComprE)*/
|
|
|
|
ext_len = RPL_RH_LEN + RPL_SRH_LEN
|
|
|
|
+ (path_len - 1) * (16 - cmpre)
|
|
|
|
+ (16 - cmpri);
|
|
|
|
|
|
|
|
padding = ext_len % 8 == 0 ? 0 : (8 - (ext_len % 8));
|
|
|
|
ext_len += padding;
|
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("SRH Path len: %u, ComprI %u, ComprE %u, ext len %u (padding %u)\n",
|
2016-02-01 17:02:33 +00:00
|
|
|
path_len, cmpri, cmpre, ext_len, padding);
|
|
|
|
|
|
|
|
/* Check if there is enough space to store the extension header */
|
2018-10-17 16:09:40 +00:00
|
|
|
if(uip_len + ext_len > UIP_LINK_MTU) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("Packet too long: impossible to add source routing header (%u bytes)\n", ext_len);
|
2017-10-12 07:28:15 +00:00
|
|
|
return 0;
|
2016-02-01 17:02:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 14:58:51 +00:00
|
|
|
/* Move existing ext headers and payload ext_len further */
|
2018-10-18 15:54:34 +00:00
|
|
|
memmove(uip_buf + UIP_IPH_LEN + uip_ext_len + ext_len,
|
|
|
|
uip_buf + UIP_IPH_LEN + uip_ext_len, uip_len - UIP_IPH_LEN);
|
|
|
|
memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len);
|
2016-02-01 17:02:33 +00:00
|
|
|
|
2018-10-17 16:09:40 +00:00
|
|
|
/* Insert source routing header (as first ext header) */
|
2018-10-18 17:11:56 +00:00
|
|
|
rh_hdr->next = UIP_IP_BUF->proto;
|
2016-02-01 17:02:33 +00:00
|
|
|
UIP_IP_BUF->proto = UIP_PROTO_ROUTING;
|
|
|
|
|
|
|
|
/* Initialize IPv6 Routing Header */
|
2018-10-18 17:11:56 +00:00
|
|
|
rh_hdr->len = (ext_len - 8) / 8;
|
|
|
|
rh_hdr->routing_type = RPL_RH_TYPE_SRH;
|
|
|
|
rh_hdr->seg_left = path_len;
|
2016-02-01 17:02:33 +00:00
|
|
|
|
|
|
|
/* Initialize RPL Source Routing Header */
|
2018-10-18 17:11:56 +00:00
|
|
|
srh_hdr->cmpr = (cmpri << 4) + cmpre;
|
|
|
|
srh_hdr->pad = padding << 4;
|
2016-02-01 17:02:33 +00:00
|
|
|
|
|
|
|
/* Initialize addresses field (the actual source route).
|
|
|
|
* From last to first. */
|
|
|
|
node = dest_node;
|
2018-10-18 17:11:56 +00:00
|
|
|
hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding; /* Pointer where to write the next hop compressed address */
|
2016-02-01 17:02:33 +00:00
|
|
|
|
|
|
|
while(node != NULL && node->parent != root_node) {
|
2017-12-13 20:00:33 +00:00
|
|
|
NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
|
2016-02-01 17:02:33 +00:00
|
|
|
|
|
|
|
hop_ptr -= (16 - cmpri);
|
|
|
|
memcpy(hop_ptr, ((uint8_t*)&node_addr) + cmpri, 16 - cmpri);
|
|
|
|
|
|
|
|
node = node->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The next hop (i.e. node whose parent is the root) is placed as the current IPv6 destination */
|
2017-12-13 20:00:33 +00:00
|
|
|
NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node);
|
2016-02-01 17:02:33 +00:00
|
|
|
uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &node_addr);
|
|
|
|
|
2018-10-18 09:20:34 +00:00
|
|
|
/* Update the IPv6 length field */
|
2018-10-17 16:09:40 +00:00
|
|
|
uipbuf_add_ext_hdr(ext_len);
|
2018-10-18 09:20:34 +00:00
|
|
|
uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
|
2016-02-01 17:02:33 +00:00
|
|
|
|
|
|
|
return 1;
|
2011-12-02 14:55:07 +00:00
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-02-01 17:02:33 +00:00
|
|
|
static int
|
|
|
|
update_hbh_header(void)
|
2011-11-16 08:00:19 +00:00
|
|
|
{
|
|
|
|
rpl_instance_t *instance;
|
2014-01-31 08:38:42 +00:00
|
|
|
rpl_parent_t *parent;
|
2018-10-18 16:29:37 +00:00
|
|
|
struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
|
|
|
|
struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(0) + 2);
|
2011-11-16 08:00:19 +00:00
|
|
|
|
2018-10-18 16:29:37 +00:00
|
|
|
if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && rpl_opt->opt_type == UIP_EXT_HDR_OPT_RPL) {
|
|
|
|
if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8)
|
|
|
|
|| rpl_opt->opt_len != RPL_HDR_OPT_LEN) {
|
2011-11-23 13:36:50 +00:00
|
|
|
|
2018-10-18 16:29:37 +00:00
|
|
|
LOG_ERR("Hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len);
|
2016-06-13 10:04:19 +00:00
|
|
|
return 0; /* Drop */
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
2016-06-13 10:04:19 +00:00
|
|
|
|
2018-10-18 16:29:37 +00:00
|
|
|
instance = rpl_get_instance(rpl_opt->instance);
|
2011-11-16 08:00:19 +00:00
|
|
|
if(instance == NULL || !instance->used || !instance->current_dag->joined) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("Unable to add/update hop-by-hop extension header: incorrect instance\n");
|
2016-06-13 10:04:19 +00:00
|
|
|
return 0; /* Drop */
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_INFO("Updating RPL option\n");
|
2016-06-13 10:04:19 +00:00
|
|
|
/* Update sender rank and instance, will update flags next */
|
2018-10-18 16:29:37 +00:00
|
|
|
rpl_opt->senderrank = UIP_HTONS(instance->current_dag->rank);
|
|
|
|
rpl_opt->instance = instance->instance_id;
|
2012-11-26 18:33:54 +00:00
|
|
|
|
2016-02-01 17:02:33 +00:00
|
|
|
if(RPL_IS_STORING(instance)) { /* In non-storing mode, downwards traffic does not have the HBH option */
|
|
|
|
/* Check the direction of the down flag, as per Section 11.2.2.3,
|
2016-06-13 10:04:19 +00:00
|
|
|
which states that if a packet is going down it should in
|
|
|
|
general not go back up again. If this happens, a
|
|
|
|
RPL_HDR_OPT_FWD_ERR should be flagged. */
|
2018-10-18 16:29:37 +00:00
|
|
|
if((rpl_opt->flags & RPL_HDR_OPT_DOWN)) {
|
2016-02-01 17:02:33 +00:00
|
|
|
if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) {
|
2018-10-18 16:29:37 +00:00
|
|
|
rpl_opt->flags |= RPL_HDR_OPT_FWD_ERR;
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_WARN("RPL forwarding error\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
/* We should send back the packet to the originating parent,
|
2016-06-13 10:04:19 +00:00
|
|
|
but it is not feasible yet, so we send a No-Path DAO instead */
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_WARN("RPL generate No-Path DAO\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
parent = rpl_get_parent((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
if(parent != NULL) {
|
|
|
|
dao_output_target(parent, &UIP_IP_BUF->destipaddr, RPL_ZERO_LIFETIME);
|
|
|
|
}
|
|
|
|
/* Drop packet */
|
|
|
|
return 0;
|
2014-01-31 08:38:42 +00:00
|
|
|
}
|
2013-08-11 22:06:12 +00:00
|
|
|
} else {
|
2016-02-01 17:02:33 +00:00
|
|
|
/* Set the down extension flag correctly as described in Section
|
2016-06-13 10:04:19 +00:00
|
|
|
11.2 of RFC6550. If the packet progresses along a DAO route,
|
|
|
|
the down flag should be set. */
|
2016-02-01 17:02:33 +00:00
|
|
|
if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) {
|
|
|
|
/* No route was found, so this packet will go towards the RPL
|
2016-06-13 10:04:19 +00:00
|
|
|
root. If so, we should not set the down flag. */
|
2018-10-18 16:29:37 +00:00
|
|
|
rpl_opt->flags &= ~RPL_HDR_OPT_DOWN;
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("RPL option going up\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
} else {
|
|
|
|
/* A DAO route was found so we set the down flag. */
|
2018-10-18 16:29:37 +00:00
|
|
|
rpl_opt->flags |= RPL_HDR_OPT_DOWN;
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("RPL option going down\n");
|
2016-02-01 17:02:33 +00:00
|
|
|
}
|
2013-08-11 22:06:12 +00:00
|
|
|
}
|
2012-11-26 18:33:54 +00:00
|
|
|
}
|
2016-02-01 17:02:33 +00:00
|
|
|
}
|
2016-06-13 10:04:19 +00:00
|
|
|
|
|
|
|
return 1;
|
2016-02-01 17:02:33 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2016-06-13 10:04:19 +00:00
|
|
|
insert_hbh_header(const rpl_instance_t *instance)
|
2016-02-01 17:02:33 +00:00
|
|
|
{
|
2018-10-18 16:29:37 +00:00
|
|
|
struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0);
|
|
|
|
struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2));
|
2016-02-01 17:02:33 +00:00
|
|
|
|
|
|
|
/* Insert hop-by-hop header */
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_DBG("Creating hop-by-hop option\n");
|
2018-10-17 16:09:40 +00:00
|
|
|
if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_LINK_MTU) {
|
2018-06-14 16:33:48 +00:00
|
|
|
LOG_ERR("Packet too long: impossible to add hop-by-hop option\n");
|
2014-01-31 08:38:42 +00:00
|
|
|
return 0;
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
2016-02-01 17:02:33 +00:00
|
|
|
|
2018-10-17 14:58:51 +00:00
|
|
|
/* Move existing ext headers and payload RPL_HOP_BY_HOP_LEN further */
|
|
|
|
memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0), uip_len - UIP_IPH_LEN);
|
|
|
|
memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN);
|
2016-02-01 17:02:33 +00:00
|
|
|
|
2018-10-17 16:09:40 +00:00
|
|
|
/* Insert HBH header (as first ext header) */
|
2018-10-18 16:29:37 +00:00
|
|
|
hbh_hdr->next = UIP_IP_BUF->proto;
|
2016-02-01 17:02:33 +00:00
|
|
|
UIP_IP_BUF->proto = UIP_PROTO_HBHO;
|
|
|
|
|
|
|
|
/* Initialize HBH option */
|
2018-10-18 16:29:37 +00:00
|
|
|
hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8;
|
|
|
|
rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL;
|
|
|
|
rpl_opt->opt_len = RPL_HDR_OPT_LEN;
|
|
|
|
rpl_opt->flags = 0;
|
|
|
|
rpl_opt->senderrank = UIP_HTONS(instance->current_dag->rank);
|
|
|
|
rpl_opt->instance = instance->instance_id;
|
2018-10-17 16:09:40 +00:00
|
|
|
|
|
|
|
uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN);
|
2018-10-18 09:20:34 +00:00
|
|
|
uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
|
2011-11-16 08:00:19 +00:00
|
|
|
|
2016-06-13 10:04:19 +00:00
|
|
|
/* Update header before returning */
|
|
|
|
return update_hbh_header();
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-11-16 08:00:19 +00:00
|
|
|
void
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_ext_header_remove(void)
|
2011-11-16 08:00:19 +00:00
|
|
|
{
|
2018-10-17 16:09:40 +00:00
|
|
|
uint8_t *prev_proto_ptr;
|
|
|
|
uint8_t protocol;
|
|
|
|
uint8_t ext_len;
|
|
|
|
uint8_t *next_header;
|
|
|
|
struct uip_ext_hdr *ext_ptr;
|
|
|
|
struct uip_ext_hdr_opt *opt_ptr;
|
|
|
|
|
2018-10-18 15:48:08 +00:00
|
|
|
next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true);
|
2018-10-17 16:09:40 +00:00
|
|
|
ext_ptr = (struct uip_ext_hdr *)next_header;
|
|
|
|
prev_proto_ptr = &UIP_IP_BUF->proto;
|
|
|
|
while(next_header != NULL && uip_is_proto_ext_hdr(protocol)) {
|
|
|
|
opt_ptr = (struct uip_ext_hdr_opt *)(next_header + 2);
|
|
|
|
if(protocol == UIP_PROTO_ROUTING || (protocol == UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) {
|
|
|
|
/* Remove ext header */
|
|
|
|
*prev_proto_ptr = ext_ptr->next;
|
|
|
|
ext_len = ext_ptr->len * 8 + 8;
|
|
|
|
uipbuf_add_ext_hdr(-ext_len);
|
|
|
|
/* Update length field and rest of packer to the "left" */
|
2018-10-18 09:20:34 +00:00
|
|
|
uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
|
2018-10-18 15:48:08 +00:00
|
|
|
memmove(next_header, next_header + ext_len, uip_len - (next_header - uip_buf));
|
2018-10-17 16:09:40 +00:00
|
|
|
/* Update loop variables */
|
|
|
|
protocol = *prev_proto_ptr;
|
|
|
|
} else {
|
|
|
|
/* move to the ext hdr */
|
2018-10-18 15:48:08 +00:00
|
|
|
next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - uip_buf), &protocol, false);
|
2018-10-17 16:09:40 +00:00
|
|
|
ext_ptr = (struct uip_ext_hdr *)next_header;
|
|
|
|
prev_proto_ptr = &ext_ptr->next;
|
2011-11-23 13:36:50 +00:00
|
|
|
}
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-25 10:47:02 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-06-13 10:04:19 +00:00
|
|
|
int
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_ext_header_update(void)
|
2011-11-16 08:00:19 +00:00
|
|
|
{
|
2016-02-03 18:12:48 +00:00
|
|
|
if(default_instance == NULL || default_instance->current_dag == NULL
|
|
|
|
|| uip_is_addr_linklocal(&UIP_IP_BUF->destipaddr) || uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
|
2016-06-13 10:04:19 +00:00
|
|
|
return 1;
|
2011-11-16 08:00:19 +00:00
|
|
|
}
|
2011-12-02 15:16:42 +00:00
|
|
|
|
2016-06-13 10:04:19 +00:00
|
|
|
if(default_instance->current_dag->rank == ROOT_RANK(default_instance)) {
|
|
|
|
/* At the root, remove headers if any, and insert SRH or HBH
|
2017-03-08 12:42:56 +00:00
|
|
|
* (SRH is inserted only if the destination is in the DODAG) */
|
2017-06-10 12:35:39 +00:00
|
|
|
rpl_ext_header_remove();
|
2017-03-08 12:42:56 +00:00
|
|
|
if(rpl_get_dag(&UIP_IP_BUF->destipaddr) != NULL) {
|
|
|
|
/* dest is in a DODAG; the packet is going down. */
|
|
|
|
if(RPL_IS_NON_STORING(default_instance)) {
|
|
|
|
return insert_srh_header();
|
|
|
|
} else {
|
|
|
|
return insert_hbh_header(default_instance);
|
|
|
|
}
|
2016-06-13 10:04:19 +00:00
|
|
|
} else {
|
2017-03-08 12:42:56 +00:00
|
|
|
/* dest is outside of DODAGs; no ext header is needed. */
|
|
|
|
return 1;
|
2016-02-01 17:02:33 +00:00
|
|
|
}
|
2016-06-13 10:04:19 +00:00
|
|
|
} else {
|
|
|
|
if(uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr)
|
|
|
|
&& UIP_IP_BUF->ttl == uip_ds6_if.cur_hop_limit) {
|
|
|
|
/* Insert HBH option at source. Checking the address is not sufficient because
|
|
|
|
* in non-storing mode, a packet may go up and then down the same path again */
|
|
|
|
return insert_hbh_header(default_instance);
|
2016-02-01 17:02:33 +00:00
|
|
|
} else {
|
2016-06-13 10:04:19 +00:00
|
|
|
/* Update HBH option at forwarders */
|
2016-02-01 17:02:33 +00:00
|
|
|
return update_hbh_header();
|
|
|
|
}
|
2013-07-31 20:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-30 09:01:20 +00:00
|
|
|
|
|
|
|
/** @}*/
|