Contiki 3.x
rpl-private.h
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * \file
32  * Private declarations for ContikiRPL.
33  * \author
34  * Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
35  */
36 
37 #ifndef RPL_PRIVATE_H
38 #define RPL_PRIVATE_H
39 
40 #include "net/rpl/rpl.h"
41 
42 #include "lib/list.h"
43 #include "net/ip/uip.h"
44 #include "sys/clock.h"
45 #include "sys/ctimer.h"
46 #include "net/ipv6/uip-ds6.h"
47 #include "net/ipv6/uip-ds6-route.h"
48 #include "net/rpl/rpl-ns.h"
50 
51 /*---------------------------------------------------------------------------*/
52 /** \brief Is IPv6 address addr the link-local, all-RPL-nodes
53  multicast address? */
54 #define uip_is_addr_linklocal_rplnodes_mcast(addr) \
55  ((addr)->u8[0] == 0xff) && \
56  ((addr)->u8[1] == 0x02) && \
57  ((addr)->u16[1] == 0) && \
58  ((addr)->u16[2] == 0) && \
59  ((addr)->u16[3] == 0) && \
60  ((addr)->u16[4] == 0) && \
61  ((addr)->u16[5] == 0) && \
62  ((addr)->u16[6] == 0) && \
63  ((addr)->u8[14] == 0) && \
64  ((addr)->u8[15] == 0x1a))
65 
66 /** \brief Set IP address addr to the link-local, all-rpl-nodes
67  multicast address. */
68 #define uip_create_linklocal_rplnodes_mcast(addr) \
69  uip_ip6addr((addr), 0xff02, 0, 0, 0, 0, 0, 0, 0x001a)
70 /*---------------------------------------------------------------------------*/
71 /* RPL message types */
72 #define RPL_CODE_DIS 0x00 /* DAG Information Solicitation */
73 #define RPL_CODE_DIO 0x01 /* DAG Information Option */
74 #define RPL_CODE_DAO 0x02 /* Destination Advertisement Option */
75 #define RPL_CODE_DAO_ACK 0x03 /* DAO acknowledgment */
76 #define RPL_CODE_SEC_DIS 0x80 /* Secure DIS */
77 #define RPL_CODE_SEC_DIO 0x81 /* Secure DIO */
78 #define RPL_CODE_SEC_DAO 0x82 /* Secure DAO */
79 #define RPL_CODE_SEC_DAO_ACK 0x83 /* Secure DAO ACK */
80 
81 /* RPL control message options. */
82 #define RPL_OPTION_PAD1 0
83 #define RPL_OPTION_PADN 1
84 #define RPL_OPTION_DAG_METRIC_CONTAINER 2
85 #define RPL_OPTION_ROUTE_INFO 3
86 #define RPL_OPTION_DAG_CONF 4
87 #define RPL_OPTION_TARGET 5
88 #define RPL_OPTION_TRANSIT 6
89 #define RPL_OPTION_SOLICITED_INFO 7
90 #define RPL_OPTION_PREFIX_INFO 8
91 #define RPL_OPTION_TARGET_DESC 9
92 
93 #define RPL_DAO_K_FLAG 0x80 /* DAO ACK requested */
94 #define RPL_DAO_D_FLAG 0x40 /* DODAG ID present */
95 
96 #define RPL_DAO_ACK_UNCONDITIONAL_ACCEPT 0
97 #define RPL_DAO_ACK_ACCEPT 1 /* 1 - 127 is OK but not good */
98 #define RPL_DAO_ACK_UNABLE_TO_ACCEPT 128 /* >127 is fail */
99 #define RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT 255 /* root can not accept */
100 
101 #define RPL_DAO_ACK_TIMEOUT -1
102 
103 /*---------------------------------------------------------------------------*/
104 /* RPL IPv6 extension header option. */
105 #define RPL_HDR_OPT_LEN 4
106 #define RPL_HOP_BY_HOP_LEN (RPL_HDR_OPT_LEN + 2 + 2)
107 #define RPL_RH_LEN 4
108 #define RPL_SRH_LEN 4
109 #define RPL_RH_TYPE_SRH 3
110 #define RPL_HDR_OPT_DOWN 0x80
111 #define RPL_HDR_OPT_DOWN_SHIFT 7
112 #define RPL_HDR_OPT_RANK_ERR 0x40
113 #define RPL_HDR_OPT_RANK_ERR_SHIFT 6
114 #define RPL_HDR_OPT_FWD_ERR 0x20
115 #define RPL_HDR_OPT_FWD_ERR_SHIFT 5
116 /*---------------------------------------------------------------------------*/
117 /* Default values for RPL constants and variables. */
118 
119 /* DAO transmissions are always delayed by RPL_DAO_DELAY +/- RPL_DAO_DELAY/2 */
120 #ifdef RPL_CONF_DAO_DELAY
121 #define RPL_DAO_DELAY RPL_CONF_DAO_DELAY
122 #else /* RPL_CONF_DAO_DELAY */
123 #define RPL_DAO_DELAY (CLOCK_SECOND * 4)
124 #endif /* RPL_CONF_DAO_DELAY */
125 
126 /* Delay between reception of a no-path DAO and actual route removal */
127 #ifdef RPL_CONF_NOPATH_REMOVAL_DELAY
128 #define RPL_NOPATH_REMOVAL_DELAY RPL_CONF_NOPATH_REMOVAL_DELAY
129 #else /* RPL_CONF_NOPATH_REMOVAL_DELAY */
130 #define RPL_NOPATH_REMOVAL_DELAY 60
131 #endif /* RPL_CONF_NOPATH_REMOVAL_DELAY */
132 
133 #ifdef RPL_CONF_DAO_MAX_RETRANSMISSIONS
134 #define RPL_DAO_MAX_RETRANSMISSIONS RPL_CONF_DAO_MAX_RETRANSMISSIONS
135 #else
136 #define RPL_DAO_MAX_RETRANSMISSIONS 5
137 #endif /* RPL_CONF_DAO_MAX_RETRANSMISSIONS */
138 
139 #ifdef RPL_CONF_DAO_RETRANSMISSION_TIMEOUT
140 #define RPL_DAO_RETRANSMISSION_TIMEOUT RPL_CONF_DAO_RETRANSMISSION_TIMEOUT
141 #else
142 #define RPL_DAO_RETRANSMISSION_TIMEOUT (5 * CLOCK_SECOND)
143 #endif /* RPL_CONF_DAO_RETRANSMISSION_TIMEOUT */
144 
145 /* Special value indicating immediate removal. */
146 #define RPL_ZERO_LIFETIME 0
147 
148 #define RPL_LIFETIME(instance, lifetime) \
149  ((unsigned long)(instance)->lifetime_unit * (lifetime))
150 
151 #ifndef RPL_CONF_MIN_HOPRANKINC
152 /* RFC6550 defines the default MIN_HOPRANKINC as 256.
153  * However, we use MRHOF as a default Objective Function (RFC6719),
154  * which recommends setting MIN_HOPRANKINC with care, in particular
155  * when used with ETX as a metric. ETX is computed as a fixed point
156  * real with a divisor of 128 (RFC6719, RFC6551). We choose to also
157  * use 128 for RPL_MIN_HOPRANKINC, resulting in a rank equal to the
158  * ETX path cost. Larger values may also be desirable, as discussed
159  * in section 6.1 of RFC6719. */
160 #if RPL_OF_OCP == RPL_OCP_MRHOF
161 #define RPL_MIN_HOPRANKINC 128
162 #else /* RPL_OF_OCP == RPL_OCP_MRHOF */
163 #define RPL_MIN_HOPRANKINC 256
164 #endif /* RPL_OF_OCP == RPL_OCP_MRHOF */
165 #else /* RPL_CONF_MIN_HOPRANKINC */
166 #define RPL_MIN_HOPRANKINC RPL_CONF_MIN_HOPRANKINC
167 #endif /* RPL_CONF_MIN_HOPRANKINC */
168 
169 #ifndef RPL_CONF_MAX_RANKINC
170 #define RPL_MAX_RANKINC (7 * RPL_MIN_HOPRANKINC)
171 #else /* RPL_CONF_MAX_RANKINC */
172 #define RPL_MAX_RANKINC RPL_CONF_MAX_RANKINC
173 #endif /* RPL_CONF_MAX_RANKINC */
174 
175 #define DAG_RANK(fixpt_rank, instance) \
176  ((fixpt_rank) / (instance)->min_hoprankinc)
177 
178 /* Rank of a virtual root node that coordinates DAG root nodes. */
179 #define BASE_RANK 0
180 
181 /* Rank of a root node. */
182 #define ROOT_RANK(instance) (instance)->min_hoprankinc
183 
184 #define INFINITE_RANK 0xffff
185 
186 /*---------------------------------------------------------------------------*/
187 #define RPL_INSTANCE_LOCAL_FLAG 0x80
188 #define RPL_INSTANCE_D_FLAG 0x40
189 
190 /* Values that tell where a route came from. */
191 #define RPL_ROUTE_FROM_INTERNAL 0
192 #define RPL_ROUTE_FROM_UNICAST_DAO 1
193 #define RPL_ROUTE_FROM_MULTICAST_DAO 2
194 #define RPL_ROUTE_FROM_DIO 3
195 
196 /* DAG Mode of Operation */
197 #define RPL_MOP_NO_DOWNWARD_ROUTES 0
198 #define RPL_MOP_NON_STORING 1
199 #define RPL_MOP_STORING_NO_MULTICAST 2
200 #define RPL_MOP_STORING_MULTICAST 3
201 
202 /* RPL Mode of operation */
203 #ifdef RPL_CONF_MOP
204 #define RPL_MOP_DEFAULT RPL_CONF_MOP
205 #else /* RPL_CONF_MOP */
206 #if RPL_CONF_MULTICAST
207 #define RPL_MOP_DEFAULT RPL_MOP_STORING_MULTICAST
208 #else
209 #define RPL_MOP_DEFAULT RPL_MOP_STORING_NO_MULTICAST
210 #endif /* UIP_IPV6_MULTICAST_RPL */
211 #endif /* RPL_CONF_MOP */
212 
213 /*
214  * Embed support for storing mode
215  */
216 #ifdef RPL_CONF_WITH_STORING
217 #define RPL_WITH_STORING RPL_CONF_WITH_STORING
218 #else /* RPL_CONF_WITH_STORING */
219 /* By default: embed support for non-storing if and only if the configured MOP is not non-storing */
220 #define RPL_WITH_STORING (RPL_MOP_DEFAULT != RPL_MOP_NON_STORING)
221 #endif /* RPL_CONF_WITH_STORING */
222 
223 /*
224  * Embed support for non-storing mode
225  */
226 #ifdef RPL_CONF_WITH_NON_STORING
227 #define RPL_WITH_NON_STORING RPL_CONF_WITH_NON_STORING
228 #else /* RPL_CONF_WITH_NON_STORING */
229 /* By default: embed support for non-storing if and only if the configured MOP is non-storing */
230 #define RPL_WITH_NON_STORING (RPL_MOP_DEFAULT == RPL_MOP_NON_STORING)
231 #endif /* RPL_CONF_WITH_NON_STORING */
232 
233 #if RPL_WITH_STORING && (UIP_DS6_ROUTE_NB == 0)
234 #error "RPL with storing mode included but #routes == 0. Set UIP_CONF_MAX_ROUTES accordingly."
235 #if !RPL_WITH_NON_STORING && (RPL_NS_LINK_NUM > 0)
236 #error "You might also want to set RPL_NS_CONF_LINK_NUM to 0."
237 #endif
238 #endif
239 
240 #if RPL_WITH_NON_STORING && (RPL_NS_LINK_NUM == 0)
241 #error "RPL with non-storing mode included but #links == 0. Set RPL_NS_CONF_LINK_NUM accordingly."
242 #if !RPL_WITH_STORING && (UIP_DS6_ROUTE_NB > 0)
243 #error "You might also want to set UIP_CONF_MAX_ROUTES to 0."
244 #endif
245 #endif
246 
247 #define RPL_IS_STORING(instance) (RPL_WITH_STORING && ((instance) != NULL) && ((instance)->mop > RPL_MOP_NON_STORING))
248 #define RPL_IS_NON_STORING(instance) (RPL_WITH_NON_STORING && ((instance) != NULL) && ((instance)->mop == RPL_MOP_NON_STORING))
249 
250 /* Emit a pre-processor error if the user configured multicast with bad MOP */
251 #if RPL_CONF_MULTICAST && (RPL_MOP_DEFAULT != RPL_MOP_STORING_MULTICAST)
252 #error "RPL Multicast requires RPL_MOP_DEFAULT==3. Check contiki-conf.h"
253 #endif
254 
255 /* Multicast Route Lifetime as a multiple of the lifetime unit */
256 #ifdef RPL_CONF_MCAST_LIFETIME
257 #define RPL_MCAST_LIFETIME RPL_CONF_MCAST_LIFETIME
258 #else
259 #define RPL_MCAST_LIFETIME 3
260 #endif
261 
262 /* DIS related */
263 #define RPL_DIS_SEND 1
264 
265 /*---------------------------------------------------------------------------*/
266 /* Lollipop counters */
267 
268 #define RPL_LOLLIPOP_MAX_VALUE 255
269 #define RPL_LOLLIPOP_CIRCULAR_REGION 127
270 #define RPL_LOLLIPOP_SEQUENCE_WINDOWS 16
271 #define RPL_LOLLIPOP_INIT (RPL_LOLLIPOP_MAX_VALUE - RPL_LOLLIPOP_SEQUENCE_WINDOWS + 1)
272 #define RPL_LOLLIPOP_INCREMENT(counter) \
273  do { \
274  if((counter) > RPL_LOLLIPOP_CIRCULAR_REGION) { \
275  (counter) = ((counter) + 1) & RPL_LOLLIPOP_MAX_VALUE; \
276  } else { \
277  (counter) = ((counter) + 1) & RPL_LOLLIPOP_CIRCULAR_REGION; \
278  } \
279  } while(0)
280 
281 #define RPL_LOLLIPOP_IS_INIT(counter) \
282  ((counter) > RPL_LOLLIPOP_CIRCULAR_REGION)
283 /*---------------------------------------------------------------------------*/
284 /* Logical representation of a DAG Information Object (DIO.) */
285 struct rpl_dio {
286  uip_ipaddr_t dag_id;
287  rpl_ocp_t ocp;
288  rpl_rank_t rank;
289  uint8_t grounded;
290  uint8_t mop;
291  uint8_t preference;
292  uint8_t version;
293  uint8_t instance_id;
294  uint8_t dtsn;
295  uint8_t dag_intdoubl;
296  uint8_t dag_intmin;
297  uint8_t dag_redund;
298  uint8_t default_lifetime;
299  uint16_t lifetime_unit;
300  rpl_rank_t dag_max_rankinc;
301  rpl_rank_t dag_min_hoprankinc;
302  rpl_prefix_t destination_prefix;
303  rpl_prefix_t prefix_info;
304  struct rpl_metric_container mc;
305 };
306 typedef struct rpl_dio rpl_dio_t;
307 
308 #if RPL_CONF_STATS
309 /* Statistics for fault management. */
310 struct rpl_stats {
311  uint16_t mem_overflows;
312  uint16_t local_repairs;
313  uint16_t global_repairs;
314  uint16_t malformed_msgs;
315  uint16_t resets;
316  uint16_t parent_switch;
317  uint16_t forward_errors;
318  uint16_t loop_errors;
319  uint16_t loop_warnings;
320  uint16_t root_repairs;
321 };
322 typedef struct rpl_stats rpl_stats_t;
323 
324 extern rpl_stats_t rpl_stats;
325 #endif
326 
327 
328 /*---------------------------------------------------------------------------*/
329 /* RPL macros. */
330 
331 #if RPL_CONF_STATS
332 #define RPL_STAT(code) (code)
333 #else
334 #define RPL_STAT(code)
335 #endif /* RPL_CONF_STATS */
336 /*---------------------------------------------------------------------------*/
337 /* Instances */
338 extern rpl_instance_t instance_table[];
339 extern rpl_instance_t *default_instance;
340 
341 /* ICMPv6 functions for RPL. */
342 void dis_output(uip_ipaddr_t *addr);
343 void dio_output(rpl_instance_t *, uip_ipaddr_t *uc_addr);
344 void dao_output(rpl_parent_t *, uint8_t lifetime);
345 void dao_output_target(rpl_parent_t *, uip_ipaddr_t *, uint8_t lifetime);
346 void dao_ack_output(rpl_instance_t *, uip_ipaddr_t *, uint8_t, uint8_t);
347 void rpl_icmp6_register_handlers(void);
348 uip_ds6_nbr_t *rpl_icmp6_update_nbr_table(uip_ipaddr_t *from,
349  nbr_table_reason_t r, void *data);
350 
351 /* RPL logic functions. */
352 void rpl_join_dag(uip_ipaddr_t *from, rpl_dio_t *dio);
353 void rpl_join_instance(uip_ipaddr_t *from, rpl_dio_t *dio);
354 void rpl_local_repair(rpl_instance_t *instance);
355 void rpl_process_dio(uip_ipaddr_t *, rpl_dio_t *);
356 int rpl_process_parent_event(rpl_instance_t *, rpl_parent_t *);
357 
358 /* DAG object management. */
359 rpl_dag_t *rpl_alloc_dag(uint8_t, uip_ipaddr_t *);
360 rpl_instance_t *rpl_alloc_instance(uint8_t);
361 void rpl_free_dag(rpl_dag_t *);
362 void rpl_free_instance(rpl_instance_t *);
363 void rpl_purge_dags(void);
364 
365 /* DAG parent management function. */
366 rpl_parent_t *rpl_add_parent(rpl_dag_t *, rpl_dio_t *dio, uip_ipaddr_t *);
367 rpl_parent_t *rpl_find_parent(rpl_dag_t *, uip_ipaddr_t *);
368 rpl_parent_t *rpl_find_parent_any_dag(rpl_instance_t *instance, uip_ipaddr_t *addr);
369 void rpl_nullify_parent(rpl_parent_t *);
370 void rpl_remove_parent(rpl_parent_t *);
371 void rpl_move_parent(rpl_dag_t *dag_src, rpl_dag_t *dag_dst, rpl_parent_t *parent);
372 rpl_parent_t *rpl_select_parent(rpl_dag_t *dag);
373 rpl_dag_t *rpl_select_dag(rpl_instance_t *instance,rpl_parent_t *parent);
374 void rpl_recalculate_ranks(void);
375 
376 /* RPL routing table functions. */
377 void rpl_remove_routes(rpl_dag_t *dag);
378 void rpl_remove_routes_by_nexthop(uip_ipaddr_t *nexthop, rpl_dag_t *dag);
379 uip_ds6_route_t *rpl_add_route(rpl_dag_t *dag, uip_ipaddr_t *prefix,
380  int prefix_len, uip_ipaddr_t *next_hop);
381 void rpl_purge_routes(void);
382 
383 /* Objective function. */
384 rpl_of_t *rpl_find_of(rpl_ocp_t);
385 
386 /* Timer functions. */
387 void rpl_schedule_dao(rpl_instance_t *);
388 void rpl_schedule_dao_immediately(rpl_instance_t *);
389 void rpl_schedule_unicast_dio_immediately(rpl_instance_t *instance);
390 void rpl_cancel_dao(rpl_instance_t *instance);
391 void rpl_schedule_probing(rpl_instance_t *instance);
392 
393 void rpl_reset_dio_timer(rpl_instance_t *);
394 void rpl_reset_periodic_timer(void);
395 
396 /* Route poisoning. */
397 void rpl_poison_routes(rpl_dag_t *, rpl_parent_t *);
398 
399 
400 rpl_instance_t *rpl_get_default_instance(void);
401 
402 #endif /* RPL_PRIVATE_H */
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
Header file for IPv6-related data structures.
Header file for routing table manipulation.
An entry in the nbr cache.
Definition: uip-ds6-nbr.h:70
This header file contains configuration directives for uIPv6 multicast support.
Header file for the callback timer
Linked list manipulation routines.
Header file for the uIP TCP/IP stack.
RPL non-storing mode specific functions.
An entry in the routing table.