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"
48 
49 /*---------------------------------------------------------------------------*/
50 /** \brief Is IPv6 address addr the link-local, all-RPL-nodes
51  multicast address? */
52 #define uip_is_addr_linklocal_rplnodes_mcast(addr) \
53  ((addr)->u8[0] == 0xff) && \
54  ((addr)->u8[1] == 0x02) && \
55  ((addr)->u16[1] == 0) && \
56  ((addr)->u16[2] == 0) && \
57  ((addr)->u16[3] == 0) && \
58  ((addr)->u16[4] == 0) && \
59  ((addr)->u16[5] == 0) && \
60  ((addr)->u16[6] == 0) && \
61  ((addr)->u8[14] == 0) && \
62  ((addr)->u8[15] == 0x1a))
63 
64 /** \brief Set IP address addr to the link-local, all-rpl-nodes
65  multicast address. */
66 #define uip_create_linklocal_rplnodes_mcast(addr) \
67  uip_ip6addr((addr), 0xff02, 0, 0, 0, 0, 0, 0, 0x001a)
68 /*---------------------------------------------------------------------------*/
69 /* RPL message types */
70 #define RPL_CODE_DIS 0x00 /* DAG Information Solicitation */
71 #define RPL_CODE_DIO 0x01 /* DAG Information Option */
72 #define RPL_CODE_DAO 0x02 /* Destination Advertisement Option */
73 #define RPL_CODE_DAO_ACK 0x03 /* DAO acknowledgment */
74 #define RPL_CODE_SEC_DIS 0x80 /* Secure DIS */
75 #define RPL_CODE_SEC_DIO 0x81 /* Secure DIO */
76 #define RPL_CODE_SEC_DAO 0x82 /* Secure DAO */
77 #define RPL_CODE_SEC_DAO_ACK 0x83 /* Secure DAO ACK */
78 
79 /* RPL control message options. */
80 #define RPL_OPTION_PAD1 0
81 #define RPL_OPTION_PADN 1
82 #define RPL_OPTION_DAG_METRIC_CONTAINER 2
83 #define RPL_OPTION_ROUTE_INFO 3
84 #define RPL_OPTION_DAG_CONF 4
85 #define RPL_OPTION_TARGET 5
86 #define RPL_OPTION_TRANSIT 6
87 #define RPL_OPTION_SOLICITED_INFO 7
88 #define RPL_OPTION_PREFIX_INFO 8
89 #define RPL_OPTION_TARGET_DESC 9
90 
91 #define RPL_DAO_K_FLAG 0x80 /* DAO ACK requested */
92 #define RPL_DAO_D_FLAG 0x40 /* DODAG ID present */
93 /*---------------------------------------------------------------------------*/
94 /* RPL IPv6 extension header option. */
95 #define RPL_HDR_OPT_LEN 4
96 #define RPL_HOP_BY_HOP_LEN (RPL_HDR_OPT_LEN + 2 + 2)
97 #define RPL_HDR_OPT_DOWN 0x80
98 #define RPL_HDR_OPT_DOWN_SHIFT 7
99 #define RPL_HDR_OPT_RANK_ERR 0x40
100 #define RPL_HDR_OPT_RANK_ERR_SHIFT 6
101 #define RPL_HDR_OPT_FWD_ERR 0x20
102 #define RPL_HDR_OPT_FWD_ERR_SHIFT 5
103 /*---------------------------------------------------------------------------*/
104 /* Default values for RPL constants and variables. */
105 
106 /* The default value for the DAO timer. */
107 #ifdef RPL_CONF_DAO_LATENCY
108 #define RPL_DAO_LATENCY RPL_CONF_DAO_LATENCY
109 #else /* RPL_CONF_DAO_LATENCY */
110 #define RPL_DAO_LATENCY (CLOCK_SECOND * 4)
111 #endif /* RPL_DAO_LATENCY */
112 
113 /* Special value indicating immediate removal. */
114 #define RPL_ZERO_LIFETIME 0
115 
116 #define RPL_LIFETIME(instance, lifetime) \
117  ((unsigned long)(instance)->lifetime_unit * (lifetime))
118 
119 #ifndef RPL_CONF_MIN_HOPRANKINC
120 #define RPL_MIN_HOPRANKINC 256
121 #else
122 #define RPL_MIN_HOPRANKINC RPL_CONF_MIN_HOPRANKINC
123 #endif
124 #define RPL_MAX_RANKINC (7 * RPL_MIN_HOPRANKINC)
125 
126 #define DAG_RANK(fixpt_rank, instance) \
127  ((fixpt_rank) / (instance)->min_hoprankinc)
128 
129 /* Rank of a virtual root node that coordinates DAG root nodes. */
130 #define BASE_RANK 0
131 
132 /* Rank of a root node. */
133 #define ROOT_RANK(instance) (instance)->min_hoprankinc
134 
135 #define INFINITE_RANK 0xffff
136 
137 
138 /* Expire DAOs from neighbors that do not respond in this time. (seconds) */
139 #define DAO_EXPIRATION_TIMEOUT 60
140 /*---------------------------------------------------------------------------*/
141 #define RPL_INSTANCE_LOCAL_FLAG 0x80
142 #define RPL_INSTANCE_D_FLAG 0x40
143 
144 /* Values that tell where a route came from. */
145 #define RPL_ROUTE_FROM_INTERNAL 0
146 #define RPL_ROUTE_FROM_UNICAST_DAO 1
147 #define RPL_ROUTE_FROM_MULTICAST_DAO 2
148 #define RPL_ROUTE_FROM_DIO 3
149 
150 /* DAG Mode of Operation */
151 #define RPL_MOP_NO_DOWNWARD_ROUTES 0
152 #define RPL_MOP_NON_STORING 1
153 #define RPL_MOP_STORING_NO_MULTICAST 2
154 #define RPL_MOP_STORING_MULTICAST 3
155 
156 #ifdef RPL_CONF_MOP
157 #define RPL_MOP_DEFAULT RPL_CONF_MOP
158 #else /* RPL_CONF_MOP */
159 #if RPL_CONF_MULTICAST
160 #define RPL_MOP_DEFAULT RPL_MOP_STORING_MULTICAST
161 #else
162 #define RPL_MOP_DEFAULT RPL_MOP_STORING_NO_MULTICAST
163 #endif /* UIP_IPV6_MULTICAST_RPL */
164 #endif /* RPL_CONF_MOP */
165 
166 /* Emit a pre-processor error if the user configured multicast with bad MOP */
167 #if RPL_CONF_MULTICAST && (RPL_MOP_DEFAULT != RPL_MOP_STORING_MULTICAST)
168 #error "RPL Multicast requires RPL_MOP_DEFAULT==3. Check contiki-conf.h"
169 #endif
170 
171 /* Multicast Route Lifetime as a multiple of the lifetime unit */
172 #ifdef RPL_CONF_MCAST_LIFETIME
173 #define RPL_MCAST_LIFETIME RPL_CONF_MCAST_LIFETIME
174 #else
175 #define RPL_MCAST_LIFETIME 3
176 #endif
177 
178 /*
179  * The ETX in the metric container is expressed as a fixed-point value
180  * whose integer part can be obtained by dividing the value by
181  * RPL_DAG_MC_ETX_DIVISOR.
182  */
183 #define RPL_DAG_MC_ETX_DIVISOR 256
184 
185 /* DIS related */
186 #define RPL_DIS_SEND 1
187 #ifdef RPL_DIS_INTERVAL_CONF
188 #define RPL_DIS_INTERVAL RPL_DIS_INTERVAL_CONF
189 #else
190 #define RPL_DIS_INTERVAL 60
191 #endif
192 #define RPL_DIS_START_DELAY 5
193 /*---------------------------------------------------------------------------*/
194 /* Lollipop counters */
195 
196 #define RPL_LOLLIPOP_MAX_VALUE 255
197 #define RPL_LOLLIPOP_CIRCULAR_REGION 127
198 #define RPL_LOLLIPOP_SEQUENCE_WINDOWS 16
199 #define RPL_LOLLIPOP_INIT (RPL_LOLLIPOP_MAX_VALUE - RPL_LOLLIPOP_SEQUENCE_WINDOWS + 1)
200 #define RPL_LOLLIPOP_INCREMENT(counter) \
201  do { \
202  if((counter) > RPL_LOLLIPOP_CIRCULAR_REGION) { \
203  (counter) = ((counter) + 1) & RPL_LOLLIPOP_MAX_VALUE; \
204  } else { \
205  (counter) = ((counter) + 1) & RPL_LOLLIPOP_CIRCULAR_REGION; \
206  } \
207  } while(0)
208 
209 #define RPL_LOLLIPOP_IS_INIT(counter) \
210  ((counter) > RPL_LOLLIPOP_CIRCULAR_REGION)
211 /*---------------------------------------------------------------------------*/
212 /* Logical representation of a DAG Information Object (DIO.) */
213 struct rpl_dio {
214  uip_ipaddr_t dag_id;
215  rpl_ocp_t ocp;
216  rpl_rank_t rank;
217  uint8_t grounded;
218  uint8_t mop;
219  uint8_t preference;
220  uint8_t version;
221  uint8_t instance_id;
222  uint8_t dtsn;
223  uint8_t dag_intdoubl;
224  uint8_t dag_intmin;
225  uint8_t dag_redund;
226  uint8_t default_lifetime;
227  uint16_t lifetime_unit;
228  rpl_rank_t dag_max_rankinc;
229  rpl_rank_t dag_min_hoprankinc;
230  rpl_prefix_t destination_prefix;
231  rpl_prefix_t prefix_info;
232  struct rpl_metric_container mc;
233 };
234 typedef struct rpl_dio rpl_dio_t;
235 
236 #if RPL_CONF_STATS
237 /* Statistics for fault management. */
238 struct rpl_stats {
239  uint16_t mem_overflows;
240  uint16_t local_repairs;
241  uint16_t global_repairs;
242  uint16_t malformed_msgs;
243  uint16_t resets;
244  uint16_t parent_switch;
245 };
246 typedef struct rpl_stats rpl_stats_t;
247 
248 extern rpl_stats_t rpl_stats;
249 #endif
250 /*---------------------------------------------------------------------------*/
251 /* RPL macros. */
252 
253 #if RPL_CONF_STATS
254 #define RPL_STAT(code) (code)
255 #else
256 #define RPL_STAT(code)
257 #endif /* RPL_CONF_STATS */
258 /*---------------------------------------------------------------------------*/
259 /* Instances */
260 extern rpl_instance_t instance_table[];
261 extern rpl_instance_t *default_instance;
262 
263 /* ICMPv6 functions for RPL. */
264 void dis_output(uip_ipaddr_t *addr);
265 void dio_output(rpl_instance_t *, uip_ipaddr_t *uc_addr);
266 void dao_output(rpl_parent_t *, uint8_t lifetime);
267 void dao_output_target(rpl_parent_t *, uip_ipaddr_t *, uint8_t lifetime);
268 void dao_ack_output(rpl_instance_t *, uip_ipaddr_t *, uint8_t);
269 void rpl_icmp6_register_handlers(void);
270 
271 /* RPL logic functions. */
272 void rpl_join_dag(uip_ipaddr_t *from, rpl_dio_t *dio);
273 void rpl_join_instance(uip_ipaddr_t *from, rpl_dio_t *dio);
274 void rpl_local_repair(rpl_instance_t *instance);
275 void rpl_process_dio(uip_ipaddr_t *, rpl_dio_t *);
276 int rpl_process_parent_event(rpl_instance_t *, rpl_parent_t *);
277 
278 /* DAG object management. */
279 rpl_dag_t *rpl_alloc_dag(uint8_t, uip_ipaddr_t *);
280 rpl_instance_t *rpl_alloc_instance(uint8_t);
281 void rpl_free_dag(rpl_dag_t *);
282 void rpl_free_instance(rpl_instance_t *);
283 
284 /* DAG parent management function. */
285 rpl_parent_t *rpl_add_parent(rpl_dag_t *, rpl_dio_t *dio, uip_ipaddr_t *);
286 rpl_parent_t *rpl_find_parent(rpl_dag_t *, uip_ipaddr_t *);
287 rpl_parent_t *rpl_find_parent_any_dag(rpl_instance_t *instance, uip_ipaddr_t *addr);
288 void rpl_nullify_parent(rpl_parent_t *);
289 void rpl_remove_parent(rpl_parent_t *);
290 void rpl_move_parent(rpl_dag_t *dag_src, rpl_dag_t *dag_dst, rpl_parent_t *parent);
291 rpl_parent_t *rpl_select_parent(rpl_dag_t *dag);
292 rpl_dag_t *rpl_select_dag(rpl_instance_t *instance,rpl_parent_t *parent);
293 void rpl_recalculate_ranks(void);
294 
295 /* RPL routing table functions. */
296 void rpl_remove_routes(rpl_dag_t *dag);
297 void rpl_remove_routes_by_nexthop(uip_ipaddr_t *nexthop, rpl_dag_t *dag);
298 uip_ds6_route_t *rpl_add_route(rpl_dag_t *dag, uip_ipaddr_t *prefix,
299  int prefix_len, uip_ipaddr_t *next_hop);
300 void rpl_purge_routes(void);
301 
302 /* Lock a parent in the neighbor cache. */
303 void rpl_lock_parent(rpl_parent_t *p);
304 
305 /* Objective function. */
306 rpl_of_t *rpl_find_of(rpl_ocp_t);
307 
308 /* Timer functions. */
309 void rpl_schedule_dao(rpl_instance_t *);
310 void rpl_schedule_dao_immediately(rpl_instance_t *);
311 void rpl_cancel_dao(rpl_instance_t *instance);
312 
313 void rpl_reset_dio_timer(rpl_instance_t *);
314 void rpl_reset_periodic_timer(void);
315 
316 /* Route poisoning. */
317 void rpl_poison_routes(rpl_dag_t *, rpl_parent_t *);
318 
319 #endif /* RPL_PRIVATE_H */
Linked list manipulation routines.
An entry in the routing table.
This header file contains configuration directives for uIPv6 multicast support.
Header file for the uIP TCP/IP stack.
Network interface and stateless autoconfiguration (RFC 4862)
Header file for the callback timer