diff --git a/Makefile.include b/Makefile.include index d9196aa5f..52c811a8b 100644 --- a/Makefile.include +++ b/Makefile.include @@ -53,14 +53,15 @@ THREADS = mt.c LIBS = memb.c mmem.c timer.c list.c etimer.c energest.c rtimer.c stimer.c \ print-stats.c ifft.c crc16.c random.c checkpoint.c ringbuf.c DEV = nullradio.c -NET = netstack.c +NET = netstack.c uip-debug.c ifdef UIP_CONF_IPV6 -CFLAGS += -DUIP_CONF_IPV6 + CFLAGS += -DUIP_CONF_IPV6=1 UIP = uip6.c tcpip.c psock.c uip-udp-packet.c uip-split.c \ resolv.c tcpdump.c uiplib.c NET += $(UIP) uip-icmp6.c uip-nd6.c \ sicslowpan.c neighbor-attr.c neighbor-info.c uip-ds6.c + include $(CONTIKI)/core/net/rpl/Makefile.rpl else # UIP_CONF_IPV6 UIP = uip.c uiplib.c resolv.c tcpip.c psock.c hc.c uip-split.c uip-fw.c \ uip-fw-drv.c uip_arp.c tcpdump.c uip-neighbor.c uip-udp-packet.c \ @@ -80,7 +81,7 @@ endif CONTIKI_SOURCEFILES += $(CONTIKIFILES) CONTIKIDIRS += ${addprefix $(CONTIKI)/core/,dev lib net net/mac net/rime \ - sys cfs ctk lib/ctk loader . } + net/rpl sys cfs ctk lib/ctk loader . } oname = ${patsubst %.c,%.o,${patsubst %.S,%.o,$(1)}} diff --git a/core/net/rpl/Makefile.rpl b/core/net/rpl/Makefile.rpl new file mode 100644 index 000000000..9a69f9513 --- /dev/null +++ b/core/net/rpl/Makefile.rpl @@ -0,0 +1 @@ +CONTIKI_SOURCEFILES += rpl.c rpl-dag.c rpl-icmp6.c rpl-timers.c rpl-of0.c diff --git a/core/net/rpl/rpl-dag.c b/core/net/rpl/rpl-dag.c new file mode 100644 index 000000000..2ce1df240 --- /dev/null +++ b/core/net/rpl/rpl-dag.c @@ -0,0 +1,646 @@ +/** + * \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. + * + * $Id: rpl-dag.c,v 1.1 2010/04/30 13:43:53 joxe Exp $ + */ +/** + * \file + * Logic for Directed Acyclic Graphs in RPL. + * + * \author Joakim Eriksson , Nicolas Tsiftes + */ + +#include "net/rpl/rpl.h" + +#include "net/uip.h" +#include "net/rime/ctimer.h" +#include "lib/list.h" +#include "lib/memb.h" + +#include +/* #include */ + +#define DEBUG DEBUG_ANNOTATE + +#include "net/uip-debug.h" + +/************************************************************************/ +#ifdef RPL_CONF_OBJECTIVE_FUNCTION +#define RPL_OF RPL_CONF_OBJECTIVE_FUNCTION +#else +extern rpl_of_t rpl_of0; +#define RPL_OF rpl_of0 +#endif /* RPL_CONF_OBJECTIVE_FUNCTION */ + +#ifndef RPL_CONF_MAX_DAG_ENTRIES +#define RPL_MAX_DAG_ENTRIES 2 +#else +#define RPL_MAX_DAG_ENTRIES RPL_CONF_MAX_DAG_ENTRIES +#endif /* !RPL_CONF_MAX_DAG_ENTRIES */ + +#ifndef RPL_CONF_MAX_NEIGHBORS +#define RPL_MAX_NEIGHBORS 10 +#else +#define RPL_MAX_NEIGHBORS RPL_CONF_MAX_NEIGHBORS +#endif /* !RPL_CONF_MAX_NEIGHBORS */ +/************************************************************************/ +/* RPL definitions. */ + +#ifndef RPL_CONF_GROUNDED +#define RPL_GROUNDED 0 +#else +#define RPL_GROUNDED RPL_CONF_GROUNDED +#endif /* !RPL_CONF_GROUNDED */ + +#ifndef RPL_CONF_DIO_INTERVAL_MIN +#define RPL_DIO_INTERVAL_MIN DEFAULT_DIO_INTERVAL_MIN +#else +#define RPL_DIO_INTERVAL_MIN RPL_CONF_DIO_INTERVAL_MIN +#endif /* !RPL_CONF_DIO_INTERVAL_MIN */ + +#ifndef RPL_CONF_DIO_INTERVAL_DOUBLINGS +#define RPL_DIO_INTERVAL_DOUBLINGS DEFAULT_DIO_INTERVAL_DOUBLINGS +#else +#define RPL_DIO_INTERVAL_DOUBLINGS RPL_CONF_DIO_INTERVAL_DOUBLINGS +#endif /* !RPL_CONF_DIO_INTERVAL_DOUBLINGS */ +/************************************************************************/ +/* Allocate neighbors from the same static MEMB chunk to reduce memory waste. */ +MEMB(neighbor_memb, struct rpl_neighbor, RPL_MAX_NEIGHBORS); + +static rpl_dag_t dag_table[RPL_MAX_DAG_ENTRIES]; +/************************************************************************/ +static void +poison_routes(rpl_dag_t *dag, rpl_neighbor_t *exception) +{ + rpl_neighbor_t *p, *p2; + + PRINTF("RPL: Poisoning routes\n"); + + for(p = list_head(dag->neighbors); p != NULL;) { + if(p != exception && RPL_NEIGHBOR_IS_PARENT(dag, p)) { + ANNOTATE("#L %u 0\n", p->addr.u8[sizeof(p->addr) - 1]); + + /* Send no-DAOs to old parents. */ + dao_output(p, ZERO_LIFETIME); + + p2 = p->next; + rpl_remove_neighbor(dag, p); + p = p2; + } else { + p = p->next; + } + } +} +/************************************************************************/ +int +rpl_set_root(uip_ipaddr_t *dag_id) +{ + rpl_dag_t *dag; + + dag = rpl_get_dag(RPL_DEFAULT_OCP); + if(dag != NULL) { + PRINTF("RPL: Dropping a joined DAG when setting this node as root"); + rpl_free_dag(dag); + } + + dag = rpl_alloc_dag(); + if(dag == NULL) { + PRINTF("RPL: Failed to allocate a DAG\n"); + return -1; + } + + dag->joined = 1; + dag->grounded = RPL_GROUNDED; + dag->rank = ROOT_RANK; + dag->of = rpl_find_of(RPL_DEFAULT_OCP); + dag->best_parent = NULL; + + memcpy(&dag->dag_id, dag_id, sizeof(dag->dag_id)); + + dag->dio_intdoubl = DEFAULT_DIO_INTERVAL_DOUBLINGS; + dag->dio_intmin = DEFAULT_DIO_INTERVAL_MIN; + dag->dio_redundancy = DEFAULT_DIO_REDUNDANCY; + + PRINTF("RPL: Node set to be a DAG root with DAG ID "); + PRINT6ADDR(&dag->dag_id); + PRINTF("\n"); + + rpl_reset_dio_timer(dag, 1); + + return 0; +} +/************************************************************************/ +int +rpl_set_default_route(rpl_dag_t *dag, uip_ipaddr_t *from) +{ + if(dag->def_route != NULL) { + PRINTF("RPL: Removing default route through "); + PRINT6ADDR(&dag->def_route->ipaddr); + PRINTF("\n"); + uip_ds6_defrt_rm(dag->def_route); + } + + if(from != NULL) { + PRINTF("RPL: Adding default route through "); + PRINT6ADDR(from); + PRINTF("\n"); + dag->def_route = uip_ds6_defrt_add(from, 100000); + if(dag->def_route == NULL) { + return 0; + } + } + + return 1; +} +/************************************************************************/ +rpl_dag_t * +rpl_alloc_dag(void) +{ + int i; + + for(i = 0; i < RPL_MAX_DAG_ENTRIES; i++) { + if(dag_table[i].used == 0) { + memset(&dag_table[i], 0, sizeof(dag_table[0])); + dag_table[i].neighbors = &dag_table[i].neighbor_list; + dag_table[i].def_route = NULL; + list_init(dag_table[i].neighbors); + return &dag_table[i]; + } + } + return NULL; +} +/************************************************************************/ +void +rpl_free_dag(rpl_dag_t *dag) +{ + uip_ds6_route_t *rep; + + PRINTF("RPL: Leaving the DAG "); + PRINT6ADDR(&dag->dag_id); + PRINTF("\n"); + + rep = uip_ds6_route_lookup(&dag->dag_id); + if(rep != NULL) { + uip_ds6_route_rm(rep); + } + + rpl_set_default_route(dag, NULL); + + poison_routes(dag, NULL); + + ctimer_stop(&dag->dio_timer); + ctimer_stop(&dag->dao_timer); + dag->used = 0; + dag->joined = 0; +} +/************************************************************************/ +rpl_neighbor_t * +rpl_add_neighbor(rpl_dag_t *dag, uip_ipaddr_t *addr) +{ + rpl_neighbor_t *n; + + n = memb_alloc(&neighbor_memb); + if(n == NULL) { + return NULL; + } + + memcpy(&n->addr, addr, sizeof(n->addr)); + n->local_confidence = 0; + n->dag = dag; + + list_add(dag->neighbors, n); + + /* Draw a line between the node and its parent in Cooja. */ + ANNOTATE("#L %u 1\n", + addr->u8[sizeof(*addr) - 1]); + + return n; +} +/************************************************************************/ +rpl_neighbor_t * +rpl_find_neighbor(rpl_dag_t *dag, uip_ipaddr_t *addr) +{ + rpl_neighbor_t *n; + + for(n = list_head(dag->neighbors); n != NULL; n = n->next) { + if(uip_ipaddr_cmp(&n->addr, addr)) { + return n; + } + } + + return NULL; +} + +/************************************************************************/ +rpl_neighbor_t * +rpl_first_parent(rpl_dag_t *dag) +{ + //return list_head(dag->parents); + + rpl_neighbor_t *n; + + for(n = list_head(dag->neighbors); n != NULL; n = n->next) { + if(RPL_NEIGHBOR_IS_PARENT(dag, n)) { + return n; + } + } + return n; +} +/************************************************************************/ +rpl_neighbor_t * +rpl_find_best_parent(rpl_dag_t *dag) +{ + rpl_neighbor_t *n; + rpl_neighbor_t *best; + + best = NULL; + for(n = list_head(dag->neighbors); n != NULL; n = n->next) { + if(RPL_NEIGHBOR_IS_PARENT(dag, n)) { + if(best == NULL) { + best = n; + } else { + best = dag->of->best_parent(best, n); + } + } + } + dag->best_parent = best; /* Cached value. */ + return best; +} +/************************************************************************/ +int +rpl_remove_neighbor(rpl_dag_t *dag, rpl_neighbor_t *parent) +{ + uip_ds6_defrt_t *defrt; + + ANNOTATE("#L %u 0\n", + parent->addr.u8[sizeof(parent->addr) - 1]); + + /* Remove uIPv6 routes that have this neighbor as the next hop. **/ + uip_ds6_route_rm_by_nexthop(&parent->addr); + defrt = uip_ds6_defrt_lookup(&parent->addr); + if(defrt != NULL) { + PRINTF("RPL: Removing default route "); + PRINT6ADDR(&parent->addr); + PRINTF("\n"); + uip_ds6_defrt_rm(defrt); + dag->def_route = NULL; + } + + PRINTF("RPL: Removing neighbor "); + PRINT6ADDR(&parent->addr); + PRINTF("\n"); + + list_remove(dag->neighbors, parent); + memb_free(&neighbor_memb, parent); + return 0; +} +/************************************************************************/ +rpl_dag_t * +rpl_get_dag(int instance_id) +{ + int i; + for(i = 0; i < RPL_MAX_DAG_ENTRIES; i++) { + if(dag_table[i].joined && (instance_id == RPL_ANY_INSTANCE || + dag_table[i].instance_id == instance_id)) { + return &dag_table[i]; + } + } + return NULL; +} +/************************************************************************/ +rpl_dag_t * +rpl_find_dag(unsigned char aucIndex) +{ + int i; + + for(i = aucIndex; i < RPL_MAX_DAG_ENTRIES; i++) { + if(dag_table[i].joined ) { + return &dag_table[i]; + } + } + return NULL; +} +/************************************************************************/ +rpl_of_t * +rpl_find_of(rpl_ocp_t ocp) +{ + if(RPL_OF.ocp == ocp) { + return &RPL_OF; + } + return NULL; +} +/************************************************************************/ +void +rpl_join_dag(rpl_dag_t *dag) +{ + dag->joined = 1; + + rpl_reset_dio_timer(dag, 1); +} +/************************************************************************/ +static void +join_dag(uip_ipaddr_t *from, rpl_dio_t *dio) +{ + rpl_dag_t *dag; + rpl_neighbor_t *n; + rpl_of_t *of; + + dag = rpl_alloc_dag(); + if(dag == NULL) { + PRINTF("RPL: Failed to allocate a DAG object!\n"); + return; + } + + n = rpl_add_neighbor(dag, from); + PRINTF("RPL: Adding "); + PRINT6ADDR(from); + PRINTF(" as a parent: "); + if(n == NULL) { + PRINTF("failed\n"); + return; + } + PRINTF("succeeded\n"); + + n->local_confidence = 0; /* Extract packet LQI here. */ + n->rank = dio->dag_rank; + + /* Determine the objective function by using the + objective code point of the DIO. */ + of = rpl_find_of(dio->ocp); + if(of == NULL) { + PRINTF("RPL: DIO for DAG instance %d does not specify a supported OF\n", + dio->instance_id); + return; + } + + dag->used = 1; + dag->of = of; + dag->preference = dio->preference; + dag->grounded = dio->grounded; + dag->instance_id = dio->instance_id; + dag->rank = dag->of->increment_rank(dio->dag_rank, n); + dag->min_rank = dag->rank; /* So far this is the lowest rank we know */ + dag->sequence_number = dio->sequence_number; + dag->dio_intdoubl = dio->dag_intdoubl; + dag->dio_intmin = dio->dag_intmin; + dag->dio_redundancy = dio->dag_redund; + + dag->max_rankinc = dio->dag_max_rankinc; + dag->min_hoprankinc = dio->dag_min_hoprankinc; + + memcpy(&dag->dag_id, &dio->dag_id, sizeof(dio->dag_id)); + + rpl_join_dag(dag); + + PRINTF("RPL: Joined DAG with instance ID %d, rank %d, DAG ID ", + dio->instance_id, dag->rank); + PRINT6ADDR(&dag->dag_id); + PRINTF("\n"); + + rpl_set_default_route(dag, from); + + if(dio->dst_adv_trigger) { + rpl_schedule_dao(dag); + } else { + PRINTF("RPL: dst_adv_trigger not set in incoming DIO!\n"); + } +} +/************************************************************************/ +static void +global_repair(uip_ipaddr_t *from, rpl_dag_t *dag, rpl_dio_t *dio) +{ + rpl_neighbor_t *n; + + poison_routes(dag, NULL); + dag->sequence_number = dio->sequence_number; + if((n = rpl_add_neighbor(dag, from)) == NULL) { + PRINTF("RPL: Failed to add a parent during the global repair\n"); + dag->rank = INFINITE_RANK; + } else { + rpl_set_default_route(dag, from); + dag->rank = dag->of->increment_rank(dio->dag_rank, n); + rpl_reset_dio_timer(dag, 1); + } + PRINTF("RPL: Participating in a global DAG repair. New DAG sequence number: %d NewRank: %d\n", + dag->sequence_number, dag->rank); + +} +/************************************************************************/ +int +rpl_repair_dag(rpl_dag_t *dag) +{ + if(dag->rank == ROOT_RANK) { + dag->sequence_number++; + rpl_reset_dio_timer(dag, 1); + return 1; + } + return 0; +} +/************************************************************************/ +void +rpl_process_dio(uip_ipaddr_t *from, rpl_dio_t *dio) +{ + rpl_dag_t *dag; + rpl_neighbor_t *n; + uint8_t new_rank; + uint8_t new_parent; + + /* if(from->u8[15] != 0xe7) { */ + /* printf("last byte did not match e7 %x\n", from->u8[15]); */ + /* return; */ + /* } */ + + dag = rpl_get_dag(dio->instance_id); + if(dag == NULL) { + /* Always join the first possible DAG that is not of INF_RANK. */ + if(dio->dag_rank != INFINITE_RANK) { + join_dag(from, dio); + } else { + PRINTF("RPL: Ignoring DIO from node with infinite rank: "); + PRINT6ADDR(from); + PRINTF("\n"); + } + return; + } + + if(dag->instance_id != dio->instance_id) { + /* We avoid joining more than one RPL instance. */ + PRINTF("RPL: Cannot join another RPL instance\n"); + return; + } + + if(dio->sequence_number > dag->sequence_number) { + global_repair(from, dag, dio); + return; + } + + /* This DIO pertains to a DAG that we are already part of. */ + n = rpl_find_neighbor(dag, from); + if(n != NULL) { + if(dio->dst_adv_trigger) { + rpl_schedule_dao(dag); + } else { + PRINTF("RPL: dst_adv_trigger is not set in incoming DIO\n"); + } + + if(n->rank > dio->dag_rank) { + n->rank = dio->dag_rank; + rpl_reset_dio_timer(dag, 1); + } else if(n->rank < dio->dag_rank) { + PRINTF("RPL: Existing parent "); + PRINT6ADDR(from); + PRINTF(" got a higher rank (%d -> %d)\n", + n->rank, dio->dag_rank); + n->rank = dio->dag_rank; + if(RPL_PARENT_COUNT(dag) > 1) { + /* Since we have alternative parents, we can simply drop this one. */ + rpl_remove_neighbor(dag, n); + } else if(dag->of->increment_rank(dio->dag_rank, n) <= dag->min_rank + dag->max_rankinc) { + dag->rank = dag->of->increment_rank(dio->dag_rank, n); + PRINTF("RPL: New rank is %i, max is %i\n", dag->rank, dag->min_rank + dag->max_rankinc); + rpl_set_default_route(dag, &n->addr); + } else { + PRINTF("RPL: Cannot find acceptable best neighbor\n"); + /* do local repair - jump down the DAG */ + poison_routes(dag, NULL); + dag->rank = INFINITE_RANK; + } + rpl_reset_dio_timer(dag, 1); + } else { + /* Assume consistency and increase the DIO counter. */ + PRINTF("RPL: Received a consistent DIO\n"); + dag->dio_counter++; + } + } + + if(dio->dag_rank < dag->rank) { + /* Message from a node closer to the root, but we might still be out of allowed rank-range */ + if(dag->min_rank + dag->max_rankinc < dag->of->increment_rank(dio->dag_rank, n)) { + PRINTF("RPL: Could not add parent, resulting rank too high\n"); + return; + } + new_parent = 0; + if(n == NULL) { + n = rpl_add_neighbor(dag, from); + if(n == NULL) { + PRINTF("RPL: Could not add parent\n"); + return; + } + + n->rank = dio->dag_rank; + PRINTF("RPL: New parent with rank %d ", n->rank); + PRINT6ADDR(from); + PRINTF("\n"); + new_parent = 1; + } + + new_rank = dag->of->increment_rank(dio->dag_rank, n); + if(new_rank < dag->rank) { + PRINTF("RPL: Moving up within the DAG from rank %d to %d\n", + dag->rank, new_rank); + dag->rank = new_rank; + dag->min_rank = new_rank; /* So far this is the lowest rank we know */ + rpl_reset_dio_timer(dag, 1); + + /* Remove old def-route and add the new */ + /* fix handling of destination prefix */ + if(dag->grounded) { + rpl_set_default_route(dag, from); + } + + if(new_parent) { + poison_routes(dag, n); + } + } + } else if(dio->dag_rank == dag->rank) { + /* Message from a sibling. */ + } else { + /* Message from a node at a longer distance from the root. If the + node is in the parent list, we just remove it. */ + if(n != NULL && n->rank < dio->dag_rank) { + PRINTF("RPL: Parent "); + PRINT6ADDR(&n->addr); + PRINTF(" has increased in rank from %d to %d. Removing it.\n", + n->rank, dio->dag_rank); + rpl_remove_neighbor(dag, n); + if(RPL_PARENT_COUNT(dag) == 0) { + dag->rank = INFINITE_RANK; + } + } + } +} +/************************************************************************/ +void +rpl_ds6_neighbor_callback(uip_ds6_nbr_t *nbr) +{ + if(nbr->isused) { + PRINTF("RPL: Neighbor state %u: ", nbr->state); + PRINT6ADDR(&nbr->ipaddr); + PRINTF("\n"); + } else { + rpl_dag_t *dag; + rpl_neighbor_t *n; + n = NULL; + + PRINTF("RPL: Removed neighbor "); + PRINT6ADDR(&nbr->ipaddr); + PRINTF("\n"); + + dag = rpl_get_dag(RPL_ANY_INSTANCE); + if(dag != NULL) { + n = rpl_find_neighbor(dag, &nbr->ipaddr); + if(n != NULL) { + rpl_remove_neighbor(dag, n); + } + } + if(dag->def_route != NULL && + uip_ipaddr_cmp(&dag->def_route->ipaddr, &n->addr)) { + n = rpl_find_best_parent(dag); + if(n != NULL && dag->of->increment_rank(n->rank, n) <= dag->min_rank + dag->max_rankinc) { + dag->rank = dag->of->increment_rank(n->rank, n); + if(dag->rank < dag->min_rank) { + dag->min_rank = dag->rank; + } + PRINTF("New rank is %i, max is %i\n", dag->rank, dag->min_rank + dag->max_rankinc); + rpl_set_default_route(dag, &n->addr); + } else { + PRINTF("RPL: Cannot find the best neighbor\n"); + /* do local repair - jump down the DAG */ + poison_routes(dag, NULL); + dag->rank = INFINITE_RANK; + rpl_reset_dio_timer(dag, 1); + } + } + } +} diff --git a/core/net/rpl/rpl-icmp6.c b/core/net/rpl/rpl-icmp6.c new file mode 100644 index 000000000..563aa6a94 --- /dev/null +++ b/core/net/rpl/rpl-icmp6.c @@ -0,0 +1,535 @@ + +/** + * \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. + * + * $Id: rpl-icmp6.c,v 1.1 2010/04/30 13:43:53 joxe Exp $ + */ +/** + * \file + * ICMP6 I/O for RPL control messages. + * + * \author Joakim Eriksson , Nicolas Tsiftes + * Contributors: Niclas Finne , + * Mathieu Pouillot + */ + +#include "net/tcpip.h" +#include "net/uip.h" +#include "net/uip-ds6.h" +#include "net/uip-icmp6.h" +#include "net/rpl/rpl.h" +#include "net/rime/packetbuf.h" + +#include + +#define DEBUG DEBUG_ANNOTATE + +#include "net/uip-debug.h" + +#define RPL_DIO_GROUNDED 0x80 +#define RPL_DIO_DEST_ADV_SUPPORTED 0x40 +#define RPL_DIO_DEST_ADV_TRIGGER 0x20 +#define RPL_DIO_DEST_ADV_STORED 0x10 +#define RPL_DIO_DAG_PREFERENCE_MASK 0x07 + + +#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]) + +static void dis_input(void); +static void dio_input(void); +static void dao_input(void); + +static int +get_global_addr(uip_ipaddr_t *addr) +{ + int i; + for(i = 0; i < UIP_DS6_ADDR_NB; i++) { + int state = uip_ds6_if.addr_list[i].state; + 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 | + (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3]; +} +/*---------------------------------------------------------------------------*/ +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; +} +/*---------------------------------------------------------------------------*/ +static void +dis_input(void) +{ + rpl_dag_t *dag; + + /* DAG Information Solicitation */ + PRINTF("RPL: Received a DIS from "); + PRINT6ADDR(&UIP_IP_BUF->srcipaddr); + PRINTF("\n"); + + dag = rpl_get_dag(RPL_ANY_INSTANCE); + if(dag != NULL) { + if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) { + PRINTF("RPL: Multicast DIS => reset DIO timer\n"); + rpl_reset_dio_timer(dag, 0); + } else { + PRINTF("RPL: Unicast DIS, reply to sender\n"); + dio_output(dag, &UIP_IP_BUF->srcipaddr); + } + } +} +/*---------------------------------------------------------------------------*/ +void +dis_output(uip_ipaddr_t *addr) +{ + unsigned char *buffer; + uip_ipaddr_t tmpaddr; + + /* DAG Information Solicitation */ + + /* Add a padding to be compliant with ICMPv6 */ + buffer = UIP_ICMP_PAYLOAD; + buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0; + + if(addr == NULL) { + PRINTF("RPL: Sending a DIS\n"); + uip_create_linklocal_allrouters_mcast(&tmpaddr); + addr = &tmpaddr; + } else { + PRINTF("RPL: Sending a unicast DIS\n"); + } + uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 4); + +} +/*---------------------------------------------------------------------------*/ +#if 0 +void +dis_u_output(uip_ipaddr_t *addr) +{ + unsigned char *buffer; + + /* DAG Information Solicitation */ + PRINTF("RPL: Sending a unicast DIS\n"); + + /* Add a padding to be compliant ICMPv6 */ + buffer = UIP_ICMP_PAYLOAD; + buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0; + + uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 4); +} +#endif +/*----------------------------------------------------------------------------*/ +static void +dio_input(void) +{ + unsigned char *buffer; + uint8_t buffer_length; + rpl_dio_t dio; + uint8_t subopt_type; + int i; + int len; + rpl_prefix_t prefix; + uip_ipaddr_t from; + uip_ds6_nbr_t *nbr; + + dio.dag_intdoubl = DEFAULT_DIO_INTERVAL_DOUBLINGS; + dio.dag_intmin = DEFAULT_DIO_INTERVAL_MIN; + dio.dag_redund = DEFAULT_DIO_REDUNDANCY; + + 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 */ + stimer_set(&(nbr->reachable), UIP_CONF_ND6_REACHABLE_TIME); + 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"); + } + + + buffer_length = uip_len - uip_l2_l3_icmp_hdr_len; + + /* Process the DIO base option. */ + i = 0; + buffer = UIP_ICMP_PAYLOAD; + dio.grounded = buffer[i] & RPL_DIO_GROUNDED; + dio.dst_adv_trigger = buffer[i] & RPL_DIO_DEST_ADV_TRIGGER; + dio.dst_adv_supported = buffer[i] & RPL_DIO_DEST_ADV_SUPPORTED; + dio.preference = buffer[i++] & RPL_DIO_DAG_PREFERENCE_MASK; + dio.sequence_number = buffer[i++]; + + dio.dag_rank = (buffer[i] << 8) + buffer[i + 1]; + i += 2; + dio.instance_id = buffer[i++]; + dio.dtsn = buffer[i++]; + + memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id)); + i += sizeof(dio.dag_id); + + /* Check if there are any DIO suboptions. */ + for(; i < buffer_length; i += len) { + subopt_type = buffer[i]; + if(subopt_type == RPL_DIO_SUBOPT_PAD1) { + len = 1; + } else { + /* Suboption with a three-byte header. */ + len = (buffer[i + 1] << 8) + buffer[i + 2] + 3; + } + + switch(subopt_type) { + case RPL_DIO_SUBOPT_DAG_MC: + if(len < 7) { + PRINTF("RPL: Invalid DAG MC, len = %d\n", len); + return; + } + break; + case RPL_DIO_SUBOPT_DEST_PREFIX: + if(len < 9) { + PRINTF("RPL: Invalid destination prefix option, len = %d\n", len); + return; + } + prefix.preference = (buffer[3] >> 3) & 0x3; + break; + case RPL_DIO_SUBOPT_DAG_CONF: + dio.dag_intdoubl = buffer[i + 3]; + dio.dag_intmin = buffer[i + 4]; + dio.dag_redund = buffer[i + 5]; + dio.dag_max_rankinc = buffer[i + 6]; + dio.dag_min_hoprankinc = buffer[i + 7]; + PRINTF("RPL: DIO trickle timer:dbl=%d, min=%d red=%d maxinc=%d mininc=%d\n", dio.dag_intdoubl, + dio.dag_intmin, dio.dag_redund, + dio.dag_max_rankinc, dio.dag_min_hoprankinc); + break; + case RPL_DIO_SUBOPT_OCP: + dio.ocp = buffer[i + 3] << 8 | buffer[i + 4]; + PRINTF("RPL: DAG OCP Sub-opt received OCP = %d\n", dio.ocp); + break; + } + } + + rpl_process_dio(&from, &dio); +} +/*---------------------------------------------------------------------------*/ +void +dio_output(rpl_dag_t *dag, uip_ipaddr_t *uc_addr) +{ + unsigned char *buffer; + int pos; + uip_ipaddr_t addr; + + /* DAG Information Solicitation */ + PRINTF("RPL: Sending a DIO with rank: %d\n", dag->rank); + pos = 0; + + buffer = UIP_ICMP_PAYLOAD; + buffer[0] = dag->preference; + if(dag->grounded) { + buffer[0] |= RPL_DIO_GROUNDED; + } + /* Set dst_adv_trigger and dst_adv_supported. */ + buffer[0] |= RPL_DIO_DEST_ADV_SUPPORTED | RPL_DIO_DEST_ADV_TRIGGER; + + buffer[1] = dag->sequence_number; + + pos = 2; + buffer[pos++] = dag->rank >> 8; + buffer[pos++] = dag->rank & 0xff; + buffer[pos++] = dag->instance_id; + buffer[pos++] = dag->dtsn; + + memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id)); + pos += 16; + + /* The objective function object must appear first. */ + buffer[pos++] = RPL_DIO_SUBOPT_OCP; + buffer[pos++] = 0; + buffer[pos++] = 2; + buffer[pos++] = 0; + buffer[pos++] = 0; + + /* alignment here ??? */ + + /* always add a sub-option for DAG configuration */ + buffer[pos++] = RPL_DIO_SUBOPT_DAG_CONF; + buffer[pos++] = 0; + buffer[pos++] = 5; + buffer[pos++] = dag->dio_intdoubl; + buffer[pos++] = dag->dio_intmin; + buffer[pos++] = dag->dio_redundancy; + buffer[pos++] = dag->max_rankinc; + buffer[pos++] = dag->min_hoprankinc; + + /* buffer[len++] = RPL_DIO_SUBOPT_PAD1; */ + + /* Unicast requests get unicast replies! */ + if(uc_addr == NULL) { + PRINTF("RPL: Sending a multicast-DIO with rank %d\n", dag->rank); + uip_create_linklocal_allrouters_mcast(&addr); + uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DIO, pos); + } else { + PRINTF("RPL: Sending unicast-DIO with rank %d to ", dag->rank); + PRINT6ADDR(uc_addr); + PRINTF("\n"); + uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos); + } +} +/*---------------------------------------------------------------------------*/ +static void +dao_input(void) +{ + rpl_dag_t *dag; + unsigned char *buffer; + uint16_t sequence; + uint8_t instance_id; + uint32_t lifetime; + uint8_t rank; + uint8_t prefixlen; + uint32_t route_tag; + uip_ipaddr_t prefix; + uip_ds6_route_t *rep; + rpl_neighbor_t *n; + int pos; + + /* Destination Advertisement Object */ + PRINTF("RPL: Received a DAO from "); + PRINT6ADDR(&UIP_IP_BUF->srcipaddr); + PRINTF("\n"); + + buffer = UIP_ICMP_PAYLOAD; + + pos = 0; + sequence = (buffer[pos] << 8) | buffer[pos + 1]; + pos += 2; + + rank = (buffer[pos] << 8) | buffer[pos + 1]; + pos += 2; + + /* pos = 4 */ + instance_id = buffer[pos++]; + + route_tag = buffer[pos++]; + prefixlen = buffer[pos++]; + /* ignore RRCount for now: rrcount = buffer[pos++]; */ + pos++; + /* pos = 8 */ + lifetime = get32(buffer, pos); + pos += 4; + + dag = rpl_get_dag(instance_id); + if(dag == NULL) { + PRINTF("RPL: Ignoring a DAO for a different DAG instance (%d)\n", + instance_id); + return; + } + + PRINTF("RPL: Incoming DAO rank is %d, my rank is %d\n", rank, dag->rank); + if(rank < dag->rank) { + return; + } + + PRINTF("RPL: DAO rank: %u, lifetime: %lu, prefix length: %u\n", rank, + (unsigned long)lifetime, prefixlen); + +/* n = rpl_find_neighbor(dag, &UIP_IP_BUF->srcipaddr); */ +/* if(n == NULL) { */ +/* n = rpl_add_neighbor(dag, &UIP_IP_BUF->srcipaddr); */ +/* if(n == NULL) { */ +/* PRINTF("RPL: Failed to add a neighbor entry for a DAO\n"); */ +/* return; */ +/* } */ +/* } */ +/* n->rank = INFINITE_RANK; */ + + memset(&prefix, 0, sizeof(prefix)); + memcpy(&prefix, buffer + pos, prefixlen / CHAR_BIT); + + rep = rpl_add_route(dag, &prefix, prefixlen, + &UIP_IP_BUF->srcipaddr); + if(rep == NULL) { + PRINTF("RPL: Could not add a route while receiving a DAO\n"); + return; + } + + if(lifetime == ZERO_LIFETIME) { + /* No-DAO received; invoke the route purging routine. */ + rpl_purge_routes(); + return; + } + + rep->state.lifetime = lifetime; + + if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) { + rep->state.learned_from = RPL_ROUTE_FROM_MULTICAST_DAO; + } else { + rep->state.learned_from = RPL_ROUTE_FROM_UNICAST_DAO; + if((n = rpl_find_best_parent(dag)) != NULL) { + PRINTF("RPL: Forwarding DAO to parent "); + PRINT6ADDR(&n->addr); + PRINTF("\n"); + uip_icmp6_send(&n->addr, ICMP6_RPL, RPL_CODE_DAO, + 14 + (prefixlen / CHAR_BIT)); + } + } +} +/*---------------------------------------------------------------------------*/ +void +dao_output(rpl_neighbor_t *n, uint32_t lifetime) +{ + rpl_dag_t *dag; + unsigned char *buffer; + static uint16_t sequence; + uint8_t prefixlen; + uip_ipaddr_t addr; + uip_ipaddr_t prefix; + int pos; + + /* Destination Advertisement Object */ + + if(n == NULL) { + dag = rpl_get_dag(RPL_ANY_INSTANCE); + if(dag == NULL) { + PRINTF("RPL: Did not join a DAG before receiving DAO\n"); + return; + } + } else { + dag = n->dag; + } + + buffer = UIP_ICMP_PAYLOAD; + + ++sequence; + pos = 0; + buffer[pos++] = sequence >> 8; + buffer[pos++] = sequence & 0xff; + + // uip_ds6_select_src(&prefix, ); + if(get_global_addr(&prefix)) { + /* all ok */ + } else { + /* not ok */ + } + + PRINTF("RPL: Sending DAO with prefix "); + PRINT6ADDR(&prefix); + PRINTF(" to "); + if(n != NULL) { + PRINT6ADDR(&n->addr); + } else { + PRINTF("multicast address"); + } + PRINTF("\n"); + + prefixlen = sizeof(dag->dag_id) * CHAR_BIT; + + buffer[pos++] = dag->rank >> 8; + buffer[pos++] = dag->rank & 0xff; + + /* pos = 4 */ + buffer[pos++] = dag->instance_id; + /* Route tag. Unspecified in draft-ietf-roll-rpl-06. */ + buffer[pos++] = 0; + + buffer[pos++] = prefixlen; + /* Reverse route count. Not used because reverse routing is unscalable + beyond a few hops. */ + buffer[pos++] = 0; + /* pos = 8 */ + /* DAO Lifetime. */ + set32(buffer, pos, lifetime); + pos += 4; + + /* pos = 12 or 14 */ + memcpy(buffer + pos, &prefix, prefixlen / CHAR_BIT); + pos += (prefixlen / CHAR_BIT); + + if(n == NULL) { + uip_create_linklocal_allnodes_mcast(&addr); + } else { + uip_ipaddr_copy(&addr, &n->addr); + } + uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DAO, pos); +} +/*---------------------------------------------------------------------------*/ +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; + default: + PRINTF("RPL: received an unknown ICMP6 code (%u)\n", UIP_ICMP_BUF->icode); + break; + } + + uip_len = 0; +} diff --git a/core/net/rpl/rpl-of0.c b/core/net/rpl/rpl-of0.c new file mode 100644 index 000000000..f5ba5fea3 --- /dev/null +++ b/core/net/rpl/rpl-of0.c @@ -0,0 +1,97 @@ +/** + * \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. + * + * $Id: rpl-of0.c,v 1.1 2010/04/30 13:43:53 joxe Exp $ + */ +/** + * \file + * An implementation of RPL's objective function 0. + * + * \author Joakim Eriksson , Nicolas Tsiftes + */ + +#include "net/rpl/rpl.h" + +#define DEBUG DEBUG_ANNOTATE +#include "net/uip-debug.h" + +static rpl_neighbor_t *best_parent(rpl_neighbor_t *, rpl_neighbor_t *); +static rpl_rank_t increment_rank(rpl_rank_t, rpl_neighbor_t *); + +rpl_of_t rpl_of0 = { + best_parent, + increment_rank, + 0 +}; + +#define DEFAULT_RANK_INCREMENT 4 +#define MINIMUM_RANK_INCREMENT 1 +#define MAXIMUM_RANK_INCREMENT 16 +#define MAXIMUM_RANK_STRETCH 4 + +static rpl_rank_t +increment_rank(rpl_rank_t rank, rpl_neighbor_t *parent) +{ + if((rpl_rank_t)(rank + DEFAULT_RANK_INCREMENT) < rank) { + PRINTF("RPL: OF0 rank %d incremented to infinite rank due to wrapping\n", + rank); + return INFINITE_RANK; + } + return rank + DEFAULT_RANK_INCREMENT; +} + +static rpl_neighbor_t * +best_parent(rpl_neighbor_t *p1, rpl_neighbor_t *p2) +{ + PRINTF("RPL: Comparing parent "); + PRINT6ADDR(&p1->addr); + PRINTF(" (confidence %d, rank %d) with parent ", + p1->local_confidence, p1->rank); + PRINT6ADDR(&p2->addr); + PRINTF(" (confidence %d, rank %d)\n", + p2->local_confidence, p2->rank); + + /* TODO: Add other rules. */ + if(p1->local_confidence > p2->local_confidence) { + return p1; + } else if(p2->local_confidence > p1->local_confidence) { + return p2; + } + + if(p1->rank < p2->rank) { + return p1; + } + + return p2; +} diff --git a/core/net/rpl/rpl-timers.c b/core/net/rpl/rpl-timers.c new file mode 100644 index 000000000..4f58e235b --- /dev/null +++ b/core/net/rpl/rpl-timers.c @@ -0,0 +1,196 @@ +/** + * \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. + * + * $Id: rpl-timers.c,v 1.1 2010/04/30 13:43:53 joxe Exp $ + */ +/** + * \file + * RPL timer management. + * + * \author Joakim Eriksson , Nicolas Tsiftes + */ + +#include "contiki-conf.h" +#include "net/rime/ctimer.h" +#include "net/rpl/rpl.h" +#include "lib/random.h" + +#define DEBUG DEBUG_ANNOTATE +#include "net/uip-debug.h" + +/************************************************************************/ +static struct ctimer periodic_timer; + +static void handle_periodic_timer(void *ptr); +static void new_dio_interval(rpl_dag_t *dag); +static void handle_dio_timer(void *ptr); + +static uint16_t next_dis; +/************************************************************************/ +static void +handle_periodic_timer(void *ptr) +{ + rpl_purge_routes(); + + /* handle DIS */ +#ifdef RPL_DIS_SEND + next_dis++; + if(rpl_get_dag(RPL_ANY_INSTANCE) == NULL && next_dis >= RPL_DIS_INTERVAL) { + next_dis = 0; + dis_output(NULL); + } +#endif + ctimer_reset(&periodic_timer); +} +/************************************************************************/ +static void +new_dio_interval(rpl_dag_t *dag) +{ + unsigned long time; + + /* TODO!!! too small timer intervals for many cases */ + time = 1L << dag->dio_intcurrent; + /* need to convert from milliseconds to CLOCK_TICKS */ + time = (time * CLOCK_SECOND) / 1000; + dag->dio_next_delay = time; + /* random number between I/2 and I */ + time = time >> 1; + time += (time * random_rand()) / RANDOM_MAX; + dag->dio_next_delay -= time; + dag->dio_send = 1; + +#if RPL_CONF_STATS + /* keep some stats */ + dag->dio_totint++; + dag->dio_totrecv += dag->dio_counter; + ANNOTATE("#A rank=%d,ints=%d,snd=%d,rcv=%d,cint=%d\n", dag->rank, + dag->dio_totint, dag->dio_totsend, + dag->dio_totrecv,dag->dio_intcurrent); +#endif /* RPL_CONF_STATS */ + + /* reset the redundancy counter */ + dag->dio_counter = 0; + + /* schedule the timer */ + PRINTF("RPL: Scheduling DIO timer %lu ticks in future (Interval)\n", time); + ctimer_set(&dag->dio_timer, time & 0xffff, &handle_dio_timer, dag); +} +/************************************************************************/ +static void +handle_dio_timer(void *ptr) +{ + rpl_dag_t *dag; + + dag = (rpl_dag_t *)ptr; + + PRINTF("RPL: DIO Timer triggered\n"); + + if(dag->dio_send) { + /* send DIO if counter is less than desired redundancy */ + if(dag->dio_counter < dag->dio_redundancy) { +#if RPL_CONF_STATS + dag->dio_totsend++; +#endif /* RPL_CONF_STATS */ + dio_output(dag, NULL); + } else { + PRINTF("RPL: Supressing DIO transmission (%d >= %d)\n", + dag->dio_counter, dag->dio_redundancy); + } + dag->dio_send = 0; + PRINTF("RPL: Scheduling DIO timer %u ticks in future (sent)\n", + dag->dio_next_delay); + ctimer_set(&dag->dio_timer, dag->dio_next_delay, handle_dio_timer, dag); + } else { + /* check if we need to double interval */ + if(dag->dio_intcurrent < dag->dio_intmin + dag->dio_intdoubl) { + dag->dio_intcurrent++; + PRINTF("RPL: DIO Timer interval doubled %d\n", dag->dio_intcurrent); + } + new_dio_interval(dag); + } +} +/************************************************************************/ +void +rpl_reset_periodic_timer(void) +{ + next_dis = RPL_DIS_INTERVAL - RPL_DIS_START_DELAY; + ctimer_set(&periodic_timer, CLOCK_SECOND, handle_periodic_timer, NULL); +} +/************************************************************************/ +/* Resets the DIO timer in the DAG and starts-up the interval */ +void +rpl_reset_dio_timer(rpl_dag_t *dag, uint8_t force) +{ + /* only reset if not just reset or started */ + if(force || dag->dio_intcurrent > dag->dio_intmin) { + dag->dio_counter = 0; + dag->dio_intcurrent = dag->dio_intmin; + new_dio_interval(dag); + } +} +/************************************************************************/ +static void +handle_dao_timer(void *ptr) +{ + rpl_dag_t *dag; + rpl_neighbor_t *n; + dag = (rpl_dag_t *)ptr; + /* Send the DAO to the best parent. rpl-07 section C.2 lists the + fan-out as being under investigation. */ + n = rpl_find_best_parent(dag); + if(n != NULL) { + PRINTF("RPL: handle_dao_timer - sending DAO\n"); + dao_output(n, DEFAULT_ROUTE_LIFETIME); + } + ctimer_stop(&dag->dao_timer); +} +/************************************************************************/ +void +rpl_schedule_dao(rpl_dag_t *dag) +{ + clock_time_t expiration_time; + expiration_time = etimer_expiration_time(&dag->dao_timer.etimer); + + if(!etimer_expired(&dag->dao_timer.etimer) && + (expiration_time - clock_time()) < (DEFAULT_DAO_LATENCY / dag->rank)) { + PRINTF("RPL: DAO timer already scheduled\n"); + } else { + PRINTF("RPL: Scheduling DAO timer %u ticks in the future (%u %u)\n", + (unsigned)DEFAULT_DAO_LATENCY / dag->rank, + (unsigned)DEFAULT_DAO_LATENCY, (unsigned)dag->rank); + ctimer_set(&dag->dao_timer, DEFAULT_DAO_LATENCY / dag->rank, + handle_dao_timer, dag); + } +} +/************************************************************************/ diff --git a/core/net/rpl/rpl.c b/core/net/rpl/rpl.c new file mode 100644 index 000000000..3167d281a --- /dev/null +++ b/core/net/rpl/rpl.c @@ -0,0 +1,171 @@ +/** + * \addtogroup uip6 + * @{ + */ +/* + * 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. + * + * $Id: rpl.c,v 1.1 2010/04/30 13:43:53 joxe Exp $ + */ +/** + * \file + * ContikiRPL, an implementation of IETF ROLL RPL. + * + * \author Joakim Eriksson , Nicolas Tsiftes + */ + +#include "net/uip.h" +#include "net/tcpip.h" +#include "net/uip-ds6.h" +#include "net/rpl/rpl.h" +#include "net/neighbor-info.h" + +#define DEBUG DEBUG_ANNOTATE +#include "net/uip-debug.h" + +#include + +/************************************************************************/ +#ifndef RPL_CONF_MAX_ROUTE_ENTRIES +#define RPL_MAX_ROUTE_ENTRIES 10 +#else +#define RPL_MAX_ROUTE_ENTRIES RPL_CONF_MAX_ROUTE_ENTRIES +#endif /* !RPL_CONF_MAX_ROUTE_ENTRIES */ + +/************************************************************************/ +extern uip_ds6_route_t uip_ds6_routing_table[UIP_DS6_ROUTE_NB]; + +void +rpl_purge_routes(void) +{ + int i; + + for(i = 0; i < UIP_DS6_ROUTE_NB; i++) { + if(uip_ds6_routing_table[i].isused) { + if(uip_ds6_routing_table[i].state.lifetime <= 1) { + uip_ds6_route_rm(&uip_ds6_routing_table[i]); + } else { + uip_ds6_routing_table[i].state.lifetime--; + } + } + } +} +/************************************************************************/ +uip_ds6_route_t * +rpl_add_route(rpl_dag_t *dag, uip_ipaddr_t *prefix, int prefix_len, + uip_ipaddr_t *next_hop) +{ + uip_ds6_route_t *rep; + + if((rep = uip_ds6_route_lookup(prefix)) == NULL) { + + if((rep = uip_ds6_route_add(prefix, prefix_len, next_hop, 0)) == NULL) { + PRINTF("RPL:ds6 No space for more route entries\n"); + return NULL; + } + + rep->state.dag = dag; + rep->state.lifetime = DEFAULT_ROUTE_LIFETIME; + rep->state.learned_from = RPL_ROUTE_FROM_INTERNAL; + + PRINTF("RPL: Added a route to "); + PRINT6ADDR(prefix); + PRINTF("/%d via ", prefix_len); + PRINT6ADDR(next_hop); + PRINTF("\n"); + } + return rep; +} +/************************************************************************/ +static void +neighbor_callback(const rimeaddr_t *addr, int known, int etx) +{ + uip_ipaddr_t ipaddr; + rpl_dag_t *dag; + rpl_neighbor_t *parent; + uip_ds6_route_t *rep; + + uip_ip6addr(&ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0); + uip_ds6_set_addr_iid(&ipaddr, (uip_lladdr_t *)addr); + PRINTF("RPL: Neighbor "); + PRINT6ADDR(&ipaddr); + PRINTF(" is %sknown. ETX = %d\n", known ? "" : "no longer ", etx); + + dag = rpl_get_dag(RPL_DEFAULT_INSTANCE); + if(dag == NULL) { + return; + } + + parent = rpl_find_neighbor(dag, &ipaddr); + if(parent == NULL) { + rep = uip_ds6_route_lookup(&ipaddr); + if(rep != NULL) { + if(!known) { + rep->state.lifetime = DAO_EXPIRATION_TIMEOUT; + rep->state.saved_lifetime = rep->state.lifetime; + } else { + rep->state.lifetime = rep->state.saved_lifetime; + } + } + } else if(!known) { + PRINTF("RPL: Removing parent "); + PRINT6ADDR(&parent->addr); + PRINTF(" because of bad connectivity (ETX %d)\n",etx); + rpl_remove_neighbor(dag, parent); + if(RPL_PARENT_COUNT(dag) == 0) { + rpl_free_dag(dag); + } else { + /* Select a new default route. */ + parent = rpl_find_best_parent(dag); + if(parent != NULL) { + rpl_set_default_route(dag, &parent->addr); + } + } + } else { + parent->local_confidence = ~0 - etx; + PRINTF("RPL: Updating the local confidence value for this neighbor to %d\n", + parent->local_confidence); + if(parent != dag->best_parent && + dag->of->best_parent(parent, dag->best_parent) == parent) { + dag->best_parent = parent; + rpl_set_default_route(dag, &parent->addr); + } + } +} +/************************************************************************/ +void +rpl_init(void) +{ + PRINTF("RPL started\n"); + + rpl_reset_periodic_timer(); + neighbor_info_subscribe(neighbor_callback); +} +/************************************************************************/ diff --git a/core/net/rpl/rpl.h b/core/net/rpl/rpl.h new file mode 100644 index 000000000..9e6a7f37b --- /dev/null +++ b/core/net/rpl/rpl.h @@ -0,0 +1,276 @@ +/* + * 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. + * + * Author: Joakim Eriksson, Nicolas Tsiftes + * + * $Id: rpl.h,v 1.1 2010/04/30 13:43:53 joxe Exp $ + */ + +#ifndef RPL_H +#define RPL_H + +#include "lib/list.h" +#include "net/uip.h" +#include "sys/clock.h" +#include "net/rime/ctimer.h" +#include "net/uip-ds6.h" + +/* set to 1 for some statistics on trickle / DIO */ +#ifndef RPL_CONF_STATS +#define RPL_CONF_STATS 1 +#endif /* RPL_CONF_STATS */ + +/* The RPL Codes for the message types */ +#define RPL_CODE_DIS 1 /* DIS message */ +#define RPL_CODE_DIO 2 /* DIO message */ +#define RPL_CODE_DAO 4 /* DAO message */ + +/* RPL DIO suboption types */ +#define RPL_DIO_SUBOPT_PAD1 0 /* Pad1 */ +#define RPL_DIO_SUBOPT_PADN 1 /* PadN */ +#define RPL_DIO_SUBOPT_DAG_MC 2 /* DAG metric container */ +#define RPL_DIO_SUBOPT_DEST_PREFIX 3 /* Destination prefix */ +#define RPL_DIO_SUBOPT_DAG_CONF 4 /* DAG configuration */ +#define RPL_DIO_SUBOPT_OCP 5 /* OCP */ + +/*---------------------------------------------------------------------------*/ +/* Default values for RPL constants and variables. */ + +#define DEFAULT_DAO_LATENCY (CLOCK_SECOND * 10) + +/* Special value indicating immediate removal. */ +#define ZERO_LIFETIME 0 + +/* Special value indicating that a DAO should not expire. */ +#define INFINITE_LIFETIME 0xffffffff + +/* Default route lifetime in seconds. */ +#define DEFAULT_ROUTE_LIFETIME INFINITE_LIFETIME + +/* Rank of a node outside the LLN. */ +#define BASE_RANK 0 + +/* Rank of a root node. */ +#define ROOT_RANK 1 + +#define INFINITE_RANK 0xffff + +#define RPL_DEFAULT_INSTANCE 0 +#define RPL_ANY_INSTANCE -1 + +#define RPL_DEFAULT_OCP 0 + +/* TODO: pick these from OCP later? */ +#define DEFAULT_MAX_RANKINC 16 +#define DEFAULT_MIN_HOPRANKINC 4 + +/* Represents 2^n ms. */ +#define DEFAULT_DIO_INTERVAL_MIN 10 + +/* Maximum amount of timer doublings. */ +#define DEFAULT_DIO_INTERVAL_DOUBLINGS 8 + +/* Desired DIO redundancy. */ +#define DEFAULT_DIO_REDUNDANCY 1 + +/* Expire DAOs from neighbors that do not respond in this time. (seconds) */ +#define DAO_EXPIRATION_TIMEOUT 60 +/*---------------------------------------------------------------------------*/ + +#define RPL_ROUTE_FROM_INTERNAL 0 +#define RPL_ROUTE_FROM_UNICAST_DAO 1 +#define RPL_ROUTE_FROM_MULTICAST_DAO 2 +#define RPL_ROUTE_FROM_DIO 3 + +/* DAG Metric Container Object Types, to be confirmed by IANA. */ +#define RPL_DAG_MC_NSA 1 +#define RPL_DAG_MC_NE 2 +#define RPL_DAG_MC_HC 3 +#define RPL_DAG_MC_THROUGHPUT 4 +#define RPL_DAG_MC_LATENCY 5 +#define RPL_DAG_MC_LQL 6 +#define RPL_DAG_MC_ETX 7 +#define RPL_DAG_MC_LC 8 + +/* DIS related */ +#define RPL_DIS_SEND 1 +#define RPL_DIS_INTERVAL 60 +#define RPL_DIS_START_DELAY 5 + +typedef uint16_t rpl_rank_t; +typedef uint16_t rpl_ocp_t; + +struct rpl_neighbor { + struct rpl_neighbor *next; + void *dag; + uip_ipaddr_t addr; + rpl_rank_t rank; + uint8_t local_confidence; +}; + +typedef struct rpl_neighbor rpl_neighbor_t; + +struct rpl_of { + rpl_neighbor_t *(*best_parent)(rpl_neighbor_t *, rpl_neighbor_t *); + rpl_rank_t (*increment_rank)(rpl_rank_t, rpl_neighbor_t *); + rpl_ocp_t ocp; +}; + + +typedef struct rpl_of rpl_of_t; + +/* Logical representation of a DAG Information Object (DIO.) */ +struct rpl_dio { + uip_ipaddr_t dag_id; + rpl_ocp_t ocp; + rpl_rank_t dag_rank; + uint8_t grounded; + uint8_t dst_adv_trigger; + uint8_t dst_adv_supported; + uint8_t preference; + uint8_t sequence_number; + uint8_t instance_id; + uint8_t dtsn; + uint8_t dag_intdoubl; + uint8_t dag_intmin; + uint8_t dag_redund; + uint8_t dag_max_rankinc; + uint8_t dag_min_hoprankinc; +}; + +typedef struct rpl_dio rpl_dio_t; + +struct rpl_prefix { + uip_ipaddr_t prefix; + uint32_t lifetime; + uint8_t length; + uint8_t preference; +}; + +typedef struct rpl_prefix rpl_prefix_t; + +/* Directed Acyclic Graph */ +struct rpl_dag { + /* DAG configuration */ + rpl_of_t *of; + uip_ipaddr_t dag_id; + /* this is the current def-router that is set - used for routing "upwards" */ + uip_ds6_defrt_t *def_route; + rpl_rank_t rank; + rpl_rank_t min_rank; /* should be nullified per dodag iteration! */ + uint8_t dtsn; + uint8_t instance_id; + uint8_t sequence_number; + uint8_t preference; + uint8_t grounded; + uint8_t dio_intdoubl; + uint8_t dio_intmin; + uint8_t dio_redundancy; + uint8_t max_rankinc; + uint8_t min_hoprankinc; + uint8_t used; + + /* live data for the DAG */ + uint8_t joined; + uint8_t dio_intcurrent; + uint8_t dio_send; /* for keeping track of which mode the timer is in */ + uint8_t dio_counter; +#if RPL_CONF_STATS + uint16_t dio_totint; + uint16_t dio_totsend; + uint16_t dio_totrecv; +#endif /* RPL_CONF_STATS */ + uint16_t dio_next_delay; /* delay for completion of dio interval */ + struct ctimer dio_timer; + struct ctimer dao_timer; + rpl_neighbor_t *best_parent; + void *neighbor_list; + list_t neighbors; +}; + +typedef struct rpl_dag rpl_dag_t; + +/*---------------------------------------------------------------------------*/ +/* RPL macro functions. */ +#define RPL_PARENT_COUNT(dag) \ + list_length((dag)->neighbors) +#define RPL_NEIGHBOR_IS_CHILD(dag, neighbor) \ + ((neighbor)->rank > (dag)->rank) +#define RPL_NEIGHBOR_IS_SIBLING(dag, neighbor) \ + ((neighbor)->rank == (dag)->rank) +#define RPL_NEIGHBOR_IS_PARENT(dag, neighbor) \ + ((neighbor)->rank < (dag)->rank) +/*---------------------------------------------------------------------------*/ +/* ICMPv6 functions for RPL. */ +void dis_output(uip_ipaddr_t *addr); +void dio_output(rpl_dag_t *, uip_ipaddr_t *uc_addr); +void dao_output(rpl_neighbor_t *, uint32_t lifetime); +void uip_rpl_input(void); + +/* RPL logic functions. */ +int rpl_set_root(uip_ipaddr_t *); +int rpl_repair_dag(rpl_dag_t *dag); +int rpl_set_default_route(rpl_dag_t *dag, uip_ipaddr_t *from); +void rpl_process_dio(uip_ipaddr_t *, rpl_dio_t *); + +/* DAG allocation and deallocation. */ +rpl_dag_t *rpl_alloc_dag(void); +void rpl_free_dag(rpl_dag_t *); + +/* DAG parent management function. */ +rpl_neighbor_t *rpl_add_neighbor(rpl_dag_t *, uip_ipaddr_t *); +rpl_neighbor_t *rpl_find_neighbor(rpl_dag_t *, uip_ipaddr_t *); +int rpl_remove_neighbor(rpl_dag_t *, rpl_neighbor_t *); +rpl_neighbor_t *rpl_first_parent(rpl_dag_t *dag); +rpl_neighbor_t *rpl_find_best_parent(rpl_dag_t *dag); + +void rpl_join_dag(rpl_dag_t *); +rpl_dag_t *rpl_get_dag(int instance_id); +rpl_dag_t *rpl_find_dag(unsigned char aucIndex); + +/* RPL routing table functions. */ +uip_ds6_route_t *rpl_add_route(rpl_dag_t *dag, uip_ipaddr_t *prefix, + int prefix_len, uip_ipaddr_t *next_hop); +void rpl_purge_routes(void); + +/* Objective function. */ +rpl_of_t *rpl_find_of(rpl_ocp_t); + +/* Timer functions. */ +void rpl_schedule_dao(rpl_dag_t *); +void rpl_reset_dio_timer(rpl_dag_t *, uint8_t); +void rpl_reset_periodic_timer(void); + +/* Route poisoning. */ +void rpl_poison_routes(rpl_dag_t *, rpl_neighbor_t *); +/*---------------------------------------------------------------------------*/ +void rpl_init(void); + +#endif /* RPL_H */