Contiki 2.6

rpl-private.h

00001 /*
00002  * Copyright (c) 2010, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * \file
00032  *   Private declarations for ContikiRPL.
00033  * \author
00034  *   Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
00035  */
00036 
00037 #ifndef RPL_PRIVATE_H
00038 #define RPL_PRIVATE_H
00039 
00040 #include "net/rpl/rpl.h"
00041 
00042 #include "lib/list.h"
00043 #include "net/uip.h"
00044 #include "sys/clock.h"
00045 #include "sys/ctimer.h"
00046 #include "net/uip-ds6.h"
00047 
00048 /*---------------------------------------------------------------------------*/
00049 /** \brief Is IPv6 address addr the link-local, all-RPL-nodes
00050     multicast address? */
00051 #define uip_is_addr_linklocal_rplnodes_mcast(addr)          \
00052   ((addr)->u8[0] == 0xff) &&                                \
00053   ((addr)->u8[1] == 0x02) &&                                \
00054   ((addr)->u16[1] == 0) &&                                  \
00055   ((addr)->u16[2] == 0) &&                                  \
00056   ((addr)->u16[3] == 0) &&                                  \
00057   ((addr)->u16[4] == 0) &&                                  \
00058   ((addr)->u16[5] == 0) &&                                  \
00059   ((addr)->u16[6] == 0) &&                                  \
00060   ((addr)->u8[14] == 0) &&                                  \
00061   ((addr)->u8[15] == 0x1a))
00062 
00063 /** \brief Set IP address addr to the link-local, all-rpl-nodes
00064     multicast address. */
00065 #define uip_create_linklocal_rplnodes_mcast(addr)       \
00066   uip_ip6addr((addr), 0xff02, 0, 0, 0, 0, 0, 0, 0x001a)
00067 /*---------------------------------------------------------------------------*/
00068 /* RPL message types */
00069 #define RPL_CODE_DIS                   0x00   /* DAG Information Solicitation */
00070 #define RPL_CODE_DIO                   0x01   /* DAG Information Option */
00071 #define RPL_CODE_DAO                   0x02   /* Destination Advertisement Option */
00072 #define RPL_CODE_DAO_ACK               0x03   /* DAO acknowledgment */
00073 #define RPL_CODE_SEC_DIS               0x80   /* Secure DIS */
00074 #define RPL_CODE_SEC_DIO               0x81   /* Secure DIO */
00075 #define RPL_CODE_SEC_DAO               0x82   /* Secure DAO */
00076 #define RPL_CODE_SEC_DAO_ACK           0x83   /* Secure DAO ACK */
00077 
00078 /* RPL control message options. */
00079 #define RPL_OPTION_PAD1                  0
00080 #define RPL_OPTION_PADN                  1
00081 #define RPL_OPTION_DAG_METRIC_CONTAINER  2
00082 #define RPL_OPTION_ROUTE_INFO            3
00083 #define RPL_OPTION_DAG_CONF              4
00084 #define RPL_OPTION_TARGET                5
00085 #define RPL_OPTION_TRANSIT               6
00086 #define RPL_OPTION_SOLICITED_INFO        7
00087 #define RPL_OPTION_PREFIX_INFO           8
00088 #define RPL_OPTION_TARGET_DESC           9
00089 
00090 #define RPL_DAO_K_FLAG                   0x80 /* DAO ACK requested */
00091 #define RPL_DAO_D_FLAG                   0x40 /* DODAG ID present */
00092 /*---------------------------------------------------------------------------*/
00093 /* RPL IPv6 extension header option. */
00094 #define RPL_HDR_OPT_LEN                 4
00095 #define RPL_HOP_BY_HOP_LEN              (RPL_HDR_OPT_LEN + 2 + 2)
00096 #define RPL_HDR_OPT_DOWN                0x80
00097 #define RPL_HDR_OPT_DOWN_SHIFT          7
00098 #define RPL_HDR_OPT_RANK_ERR            0x40
00099 #define RPL_HDR_OPT_RANK_ERR_SHIFT      6
00100 #define RPL_HDR_OPT_FWD_ERR             0x20
00101 #define RPL_HDR_OPT_FWD_ERR_SHIFT       5
00102 /*---------------------------------------------------------------------------*/
00103 /* Default values for RPL constants and variables. */
00104 
00105 /* The default value for the DAO timer. */
00106 #define RPL_DAO_LATENCY                 (CLOCK_SECOND * 4)
00107 
00108 /* Special value indicating immediate removal. */
00109 #define RPL_ZERO_LIFETIME               0
00110 
00111 /* Default route lifetime unit. */
00112 #define RPL_DEFAULT_LIFETIME_UNIT       0xffff
00113 
00114 /* Default route lifetime as a multiple of the lifetime unit. */
00115 #define RPL_DEFAULT_LIFETIME            0xff
00116 
00117 #define RPL_LIFETIME(instance, lifetime) \
00118           ((unsigned long)(instance)->lifetime_unit * (lifetime))
00119 
00120 #ifndef RPL_CONF_MIN_HOPRANKINC
00121 #define RPL_MIN_HOPRANKINC          256
00122 #else
00123 #define RPL_MIN_HOPRANKINC          RPL_CONF_MIN_HOPRANKINC
00124 #endif
00125 #define RPL_MAX_RANKINC             (7 * RPL_MIN_HOPRANKINC)
00126 
00127 #define DAG_RANK(fixpt_rank, instance) \
00128   ((fixpt_rank) / (instance)->min_hoprankinc)
00129 
00130 /* Rank of a virtual root node that coordinates DAG root nodes. */
00131 #define BASE_RANK                       0
00132 
00133 /* Rank of a root node. */
00134 #define ROOT_RANK(instance)             (instance)->min_hoprankinc
00135 
00136 #define INFINITE_RANK                   0xffff
00137 
00138 #define INITIAL_LINK_METRIC             NEIGHBOR_INFO_ETX2FIX(5)
00139 
00140 /* Represents 2^n ms. */
00141 /* Default value according to the specification is 3 which
00142    means 8 milliseconds, but that is an unreasonable value if
00143    using power-saving / duty-cycling    */
00144 #ifdef RPL_CONF_DIO_INTERVAL_MIN
00145 #define RPL_DIO_INTERVAL_MIN        RPL_CONF_DIO_INTERVAL_MIN
00146 #else
00147 #define RPL_DIO_INTERVAL_MIN        12
00148 #endif
00149 
00150 /* Maximum amount of timer doublings. */
00151 #ifdef RPL_CONF_DIO_INTERVAL_DOUBLINGS
00152 #define RPL_DIO_INTERVAL_DOUBLINGS  RPL_CONF_DIO_INTERVAL_DOUBLINGS
00153 #else
00154 #define RPL_DIO_INTERVAL_DOUBLINGS  8
00155 #endif
00156 
00157 /* Default DIO redundancy. */
00158 #ifdef RPL_CONF_DIO_REDUNDANCY
00159 #define RPL_DIO_REDUNDANCY          RPL_CONF_DIO_REDUNDANCY
00160 #else
00161 #define RPL_DIO_REDUNDANCY          10
00162 #endif
00163 
00164 /* Expire DAOs from neighbors that do not respond in this time. (seconds) */
00165 #define DAO_EXPIRATION_TIMEOUT          60
00166 /*---------------------------------------------------------------------------*/
00167 #define RPL_INSTANCE_LOCAL_FLAG         0x80
00168 #define RPL_INSTANCE_D_FLAG             0x40
00169 
00170 /* Values that tell where a route came from. */
00171 #define RPL_ROUTE_FROM_INTERNAL         0
00172 #define RPL_ROUTE_FROM_UNICAST_DAO      1
00173 #define RPL_ROUTE_FROM_MULTICAST_DAO    2
00174 #define RPL_ROUTE_FROM_DIO              3
00175 
00176 /* DAG Mode of Operation */
00177 #define RPL_MOP_NO_DOWNWARD_ROUTES      0
00178 #define RPL_MOP_NON_STORING             1
00179 #define RPL_MOP_STORING_NO_MULTICAST    2
00180 #define RPL_MOP_STORING_MULTICAST       3
00181 
00182 #ifdef  RPL_CONF_MOP
00183 #define RPL_MOP_DEFAULT                 RPL_CONF_MOP
00184 #else
00185 #define RPL_MOP_DEFAULT                 RPL_MOP_STORING_NO_MULTICAST
00186 #endif
00187 
00188 /*
00189  * The ETX in the metric container is expressed as a fixed-point value 
00190  * whose integer part can be obtained by dividing the value by 
00191  * RPL_DAG_MC_ETX_DIVISOR.
00192  */
00193 #define RPL_DAG_MC_ETX_DIVISOR          128
00194 
00195 /* DIS related */
00196 #define RPL_DIS_SEND                    1
00197 #ifdef  RPL_DIS_INTERVAL_CONF
00198 #define RPL_DIS_INTERVAL                RPL_DIS_INTERVAL_CONF
00199 #else
00200 #define RPL_DIS_INTERVAL                60
00201 #endif
00202 #define RPL_DIS_START_DELAY             5
00203 /*---------------------------------------------------------------------------*/
00204 /* Lollipop counters */
00205 
00206 #define RPL_LOLLIPOP_MAX_VALUE           255
00207 #define RPL_LOLLIPOP_CIRCULAR_REGION     127
00208 #define RPL_LOLLIPOP_SEQUENCE_WINDOWS    16
00209 #define RPL_LOLLIPOP_INIT                (RPL_LOLLIPOP_MAX_VALUE - RPL_LOLLIPOP_SEQUENCE_WINDOWS + 1)
00210 #define RPL_LOLLIPOP_INCREMENT(counter)                                 \
00211   ((counter) > RPL_LOLLIPOP_CIRCULAR_REGION ?                           \
00212    ++(counter) & RPL_LOLLIPOP_MAX_VALUE :                               \
00213    ++(counter) & RPL_LOLLIPOP_CIRCULAR_REGION)
00214 
00215 #define RPL_LOLLIPOP_IS_INIT(counter)           \
00216   ((counter) > RPL_LOLLIPOP_CIRCULAR_REGION)
00217 /*---------------------------------------------------------------------------*/
00218 /* Logical representation of a DAG Information Object (DIO.) */
00219 struct rpl_dio {
00220   uip_ipaddr_t dag_id;
00221   rpl_ocp_t ocp;
00222   rpl_rank_t rank;
00223   uint8_t grounded;
00224   uint8_t mop;
00225   uint8_t preference;
00226   uint8_t version;
00227   uint8_t instance_id;
00228   uint8_t dtsn;
00229   uint8_t dag_intdoubl;
00230   uint8_t dag_intmin;
00231   uint8_t dag_redund;
00232   uint8_t default_lifetime;
00233   uint16_t lifetime_unit;
00234   rpl_rank_t dag_max_rankinc;
00235   rpl_rank_t dag_min_hoprankinc;
00236   rpl_prefix_t destination_prefix;
00237   rpl_prefix_t prefix_info;
00238   struct rpl_metric_container mc;
00239 };
00240 typedef struct rpl_dio rpl_dio_t;
00241 
00242 #if RPL_CONF_STATS
00243 /* Statistics for fault management. */
00244 struct rpl_stats {
00245   uint16_t mem_overflows;
00246   uint16_t local_repairs;
00247   uint16_t global_repairs;
00248   uint16_t malformed_msgs;
00249   uint16_t resets;
00250   uint16_t parent_switch;
00251 };
00252 typedef struct rpl_stats rpl_stats_t;
00253 
00254 extern rpl_stats_t rpl_stats;
00255 #endif
00256 /*---------------------------------------------------------------------------*/
00257 /* RPL macros. */
00258 
00259 #if RPL_CONF_STATS
00260 #define RPL_STAT(code)  (code) 
00261 #else
00262 #define RPL_STAT(code)
00263 #endif /* RPL_CONF_STATS */
00264 /*---------------------------------------------------------------------------*/
00265 /* Instances */
00266 extern rpl_instance_t instance_table[];
00267 extern rpl_instance_t *default_instance;
00268 
00269 /* ICMPv6 functions for RPL. */
00270 void dis_output(uip_ipaddr_t *addr);
00271 void dio_output(rpl_instance_t *, uip_ipaddr_t *uc_addr);
00272 void dao_output(rpl_parent_t *, uint8_t lifetime);
00273 void dao_ack_output(rpl_instance_t *, uip_ipaddr_t *, uint8_t);
00274 
00275 /* RPL logic functions. */
00276 void rpl_join_dag(uip_ipaddr_t *from, rpl_dio_t *dio);
00277 void rpl_join_instance(uip_ipaddr_t *from, rpl_dio_t *dio);
00278 void rpl_local_repair(rpl_instance_t *instance);
00279 void rpl_process_dio(uip_ipaddr_t *, rpl_dio_t *);
00280 int rpl_process_parent_event(rpl_instance_t *, rpl_parent_t *);
00281 
00282 /* DAG object management. */
00283 rpl_dag_t *rpl_alloc_dag(uint8_t, uip_ipaddr_t *);
00284 rpl_instance_t *rpl_alloc_instance(uint8_t);
00285 void rpl_free_dag(rpl_dag_t *);
00286 void rpl_free_instance(rpl_instance_t *);
00287 
00288 /* DAG parent management function. */
00289 rpl_parent_t *rpl_add_parent(rpl_dag_t *, rpl_dio_t *dio, uip_ipaddr_t *);
00290 rpl_parent_t *rpl_find_parent(rpl_dag_t *, uip_ipaddr_t *);
00291 rpl_parent_t *rpl_find_parent_any_dag(rpl_instance_t *instance, uip_ipaddr_t *addr);
00292 void rpl_nullify_parent(rpl_dag_t *, rpl_parent_t *);
00293 void rpl_remove_parent(rpl_dag_t *, rpl_parent_t *);
00294 void rpl_move_parent(rpl_dag_t *dag_src, rpl_dag_t *dag_dst, rpl_parent_t *parent);
00295 rpl_parent_t *rpl_select_parent(rpl_dag_t *dag);
00296 rpl_dag_t *rpl_select_dag(rpl_instance_t *instance,rpl_parent_t *parent);
00297 void rpl_recalculate_ranks(void);
00298 
00299 /* RPL routing table functions. */
00300 void rpl_remove_routes(rpl_dag_t *dag);
00301 void rpl_remove_routes_by_nexthop(uip_ipaddr_t *nexthop, rpl_dag_t *dag);
00302 uip_ds6_route_t *rpl_add_route(rpl_dag_t *dag, uip_ipaddr_t *prefix,
00303                                int prefix_len, uip_ipaddr_t *next_hop);
00304 void rpl_purge_routes(void);
00305 
00306 /* Objective function. */
00307 rpl_of_t *rpl_find_of(rpl_ocp_t);
00308 
00309 /* Timer functions. */
00310 void rpl_schedule_dao(rpl_instance_t *);
00311 void rpl_reset_dio_timer(rpl_instance_t *);
00312 void rpl_reset_periodic_timer(void);
00313 
00314 /* Route poisoning. */
00315 void rpl_poison_routes(rpl_dag_t *, rpl_parent_t *);
00316 
00317 #endif /* RPL_PRIVATE_H */