Contiki 2.5
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  * Author: Joakim Eriksson, Nicolas Tsiftes
32  */
33 
34 #ifndef RPL_PRIVATE_H
35 #define RPL_PRIVATE_H
36 
37 /*
38  * ContikiRPL - an implementation of the routing protocol for low power and
39  * lossy networks. See: draft-ietf-roll-rpl-17.
40  *
41  * --
42  * The DIOs handle prefix information option for setting global IP addresses
43  * on the nodes, but the current handling is not awaiting the join of the DAG
44  * so it does not currently support multiple DAGs.
45  */
46 
47 #include "net/rpl/rpl.h"
48 
49 #include "lib/list.h"
50 #include "net/uip.h"
51 #include "sys/clock.h"
52 #include "sys/ctimer.h"
53 #include "net/uip-ds6.h"
54 
55 /*---------------------------------------------------------------------------*/
56 /** \brief Is IPv6 address a the link local all rpl nodes multicast address */
57 #define uip_is_addr_linklocal_rplnodes_mcast(a) \
58  ((((a)->u8[0]) == 0xff) && \
59  (((a)->u8[1]) == 0x02) && \
60  (((a)->u16[1]) == 0) && \
61  (((a)->u16[2]) == 0) && \
62  (((a)->u16[3]) == 0) && \
63  (((a)->u16[4]) == 0) && \
64  (((a)->u16[5]) == 0) && \
65  (((a)->u16[6]) == 0) && \
66  (((a)->u8[14]) == 0) && \
67  (((a)->u8[15]) == 0x1a))
68 
69 /** \brief set IP address a to the link local all-rpl nodes multicast address */
70 #define uip_create_linklocal_rplnodes_mcast(a) uip_ip6addr(a, 0xff02, 0, 0, 0, 0, 0, 0, 0x001a)
71 /*---------------------------------------------------------------------------*/
72 /* RPL message types */
73 #define RPL_CODE_DIS 0x00 /* DAG Information Solicitation */
74 #define RPL_CODE_DIO 0x01 /* DAG Information Option */
75 #define RPL_CODE_DAO 0x02 /* Destination Advertisement Option */
76 #define RPL_CODE_DAO_ACK 0x03 /* DAO acknowledgment */
77 #define RPL_CODE_SEC_DIS 0x80 /* Secure DIS */
78 #define RPL_CODE_SEC_DIO 0x81 /* Secure DIO */
79 #define RPL_CODE_SEC_DAO 0x82 /* Secure DAO */
80 #define RPL_CODE_SEC_DAO_ACK 0x83 /* Secure DAO ACK */
81 
82 /* RPL control message options. */
83 #define RPL_OPTION_PAD1 0
84 #define RPL_OPTION_PADN 1
85 #define RPL_OPTION_DAG_METRIC_CONTAINER 2
86 #define RPL_OPTION_ROUTE_INFO 3
87 #define RPL_OPTION_DAG_CONF 4
88 #define RPL_OPTION_TARGET 5
89 #define RPL_OPTION_TRANSIT 6
90 #define RPL_OPTION_SOLICITED_INFO 7
91 #define RPL_OPTION_PREFIX_INFO 8
92 #define RPL_OPTION_TARGET_DESC 9
93 
94 #define RPL_DAO_K_FLAG 0x80 /* DAO ACK requested */
95 #define RPL_DAO_D_FLAG 0x40 /* DODAG ID present */
96 /*---------------------------------------------------------------------------*/
97 /* Default values for RPL constants and variables. */
98 
99 /* The default value for the DAO timer. */
100 #define DEFAULT_DAO_LATENCY (CLOCK_SECOND * 8)
101 
102 /* Special value indicating immediate removal. */
103 #define ZERO_LIFETIME 0
104 
105 /* Default route lifetime unit. */
106 #define RPL_DEFAULT_LIFETIME_UNIT 0xffff
107 
108 /* Default route lifetime as a multiple of the lifetime unit. */
109 #define RPL_DEFAULT_LIFETIME 0xff
110 
111 #define RPL_LIFETIME(dag, lifetime) \
112  ((unsigned long)(dag)->lifetime_unit * lifetime)
113 
114 #ifndef RPL_CONF_MIN_HOPRANKINC
115 #define DEFAULT_MIN_HOPRANKINC 256
116 #else
117 #define DEFAULT_MIN_HOPRANKINC RPL_CONF_MIN_HOPRANKINC
118 #endif
119 #define DEFAULT_MAX_RANKINC (3 * DEFAULT_MIN_HOPRANKINC)
120 
121 #define DAG_RANK(fixpt_rank, dag) ((fixpt_rank) / (dag)->min_hoprankinc)
122 
123 /* Rank of a virtual root node that coordinates DAG root nodes. */
124 #define BASE_RANK 0
125 
126 /* Rank of a root node. */
127 #define ROOT_RANK(dag) (dag)->min_hoprankinc
128 
129 #define INFINITE_RANK 0xffff
130 
131 #define INITIAL_LINK_METRIC NEIGHBOR_INFO_ETX2FIX(5)
132 
133 /* Represents 2^n ms. */
134 /* Default value according to the specification is 3 which
135  means 8 milliseconds, but that is an unreasonable value if
136  using power-saving / duty-cycling */
137 #ifdef RPL_CONF_DIO_INTERVAL_MIN
138 #define DEFAULT_DIO_INTERVAL_MIN RPL_CONF_DIO_INTERVAL_MIN
139 #else
140 #define DEFAULT_DIO_INTERVAL_MIN 12
141 #endif
142 
143 /* Maximum amount of timer doublings. */
144 #ifdef RPL_CONF_DIO_INTERVAL_DOUBLINGS
145 #define DEFAULT_DIO_INTERVAL_DOUBLINGS RPL_CONF_DIO_INTERVAL_DOUBLINGS
146 #else
147 #define DEFAULT_DIO_INTERVAL_DOUBLINGS 8
148 #endif
149 
150 /* Default DIO redundancy. */
151 #ifdef RPL_CONF_DIO_REDUNDANCY
152 #define DEFAULT_DIO_REDUNDANCY RPL_CONF_DIO_REDUNDANCY
153 #else
154 #define DEFAULT_DIO_REDUNDANCY 10
155 #endif
156 
157 /* Expire DAOs from neighbors that do not respond in this time. (seconds) */
158 #define DAO_EXPIRATION_TIMEOUT 60
159 /*---------------------------------------------------------------------------*/
160 #define RPL_INSTANCE_LOCAL_FLAG 0x80
161 #define RPL_INSTANCE_D_FLAG 0x40
162 
163 /* Values that tell where a route came from. */
164 #define RPL_ROUTE_FROM_INTERNAL 0
165 #define RPL_ROUTE_FROM_UNICAST_DAO 1
166 #define RPL_ROUTE_FROM_MULTICAST_DAO 2
167 #define RPL_ROUTE_FROM_DIO 3
168 
169 /* DAG Mode of Operation */
170 #define RPL_MOP_NO_DOWNWARD_ROUTES 0
171 #define RPL_MOP_NON_STORING 1
172 #define RPL_MOP_STORING_NO_MULTICAST 2
173 #define RPL_MOP_STORING_MULTICAST 3
174 
175 #ifdef RPL_CONF_MOP
176 #define RPL_MOP_DEFAULT RPL_CONF_MOP
177 #else
178 #define RPL_MOP_DEFAULT RPL_MOP_STORING_NO_MULTICAST
179 #endif
180 
181 /*
182  * The ETX in the metric container is expressed as a fixed-point value
183  * whose integer part can be obtained by dividing the value by
184  * RPL_DAG_MC_ETX_DIVISOR.
185  */
186 #define RPL_DAG_MC_ETX_DIVISOR 128
187 
188 /* DIS related */
189 #define RPL_DIS_SEND 1
190 #ifdef RPL_DIS_INTERVAL_CONF
191 #define RPL_DIS_INTERVAL RPL_DIS_INTERVAL_CONF
192 #else
193 #define RPL_DIS_INTERVAL 60
194 #endif
195 #define RPL_DIS_START_DELAY 5
196 /*---------------------------------------------------------------------------*/
197 /* Logical representation of a DAG Information Object (DIO.) */
198 struct rpl_dio {
199  uip_ipaddr_t dag_id;
200  rpl_ocp_t ocp;
201  rpl_rank_t rank;
202  uint8_t grounded;
203  uint8_t mop;
204  uint8_t preference;
205  uint8_t version;
206  uint8_t instance_id;
207  uint8_t dtsn;
208  uint8_t dag_intdoubl;
209  uint8_t dag_intmin;
210  uint8_t dag_redund;
211  rpl_lifetime_t default_lifetime;
212  uint16_t lifetime_unit;
213  rpl_rank_t dag_max_rankinc;
214  rpl_rank_t dag_min_hoprankinc;
215  rpl_prefix_t destination_prefix;
216  rpl_prefix_t prefix_info;
217  struct rpl_metric_container mc;
218 };
219 typedef struct rpl_dio rpl_dio_t;
220 
221 #if RPL_CONF_STATS
222 /* Statistics for fault management. */
223 struct rpl_stats {
224  uint16_t mem_overflows;
225  uint16_t local_repairs;
226  uint16_t global_repairs;
227  uint16_t malformed_msgs;
228  uint16_t resets;
229  uint16_t parent_switch;
230 };
231 typedef struct rpl_stats rpl_stats_t;
232 
233 extern rpl_stats_t rpl_stats;
234 #endif
235 /*---------------------------------------------------------------------------*/
236 /* RPL macros. */
237 
238 #if RPL_CONF_STATS
239 #define RPL_STAT(code) (code)
240 #else
241 #define RPL_STAT(code)
242 #endif /* RPL_CONF_STATS */
243 /*---------------------------------------------------------------------------*/
244 /* ICMPv6 functions for RPL. */
245 void dis_output(uip_ipaddr_t *addr);
246 void dio_output(rpl_dag_t *, uip_ipaddr_t *uc_addr);
247 void dao_output(rpl_parent_t *, rpl_lifetime_t lifetime);
248 void dao_ack_output(rpl_dag_t *, uip_ipaddr_t *, uint8_t);
249 void uip_rpl_input(void);
250 
251 /* RPL logic functions. */
252 void rpl_join_dag(rpl_dag_t *);
253 void rpl_local_repair(rpl_dag_t *dag);
254 int rpl_set_default_route(rpl_dag_t *dag, uip_ipaddr_t *from);
255 void rpl_process_dio(uip_ipaddr_t *, rpl_dio_t *);
256 int rpl_process_parent_event(rpl_dag_t *, rpl_parent_t *);
257 
258 /* DAG object management. */
259 rpl_dag_t *rpl_alloc_dag(uint8_t);
260 void rpl_free_dag(rpl_dag_t *);
261 
262 /* DAG parent management function. */
263 rpl_parent_t *rpl_add_parent(rpl_dag_t *, rpl_dio_t *dio, uip_ipaddr_t *);
264 rpl_parent_t *rpl_find_parent(rpl_dag_t *, uip_ipaddr_t *);
265 int rpl_remove_parent(rpl_dag_t *, rpl_parent_t *);
266 rpl_parent_t *rpl_select_parent(rpl_dag_t *dag);
267 void rpl_recalculate_ranks(void);
268 
269 /* RPL routing table functions. */
270 void rpl_remove_routes(rpl_dag_t *dag);
271 uip_ds6_route_t *rpl_add_route(rpl_dag_t *dag, uip_ipaddr_t *prefix,
272  int prefix_len, uip_ipaddr_t *next_hop);
273 void rpl_purge_routes(void);
274 
275 /* Objective function. */
276 rpl_of_t *rpl_find_of(rpl_ocp_t);
277 
278 /* Timer functions. */
279 void rpl_schedule_dao(rpl_dag_t *);
280 void rpl_reset_dio_timer(rpl_dag_t *, uint8_t);
281 void rpl_reset_periodic_timer(void);
282 
283 /* Route poisoning. */
284 void rpl_poison_routes(rpl_dag_t *, rpl_parent_t *);
285 
286 #endif /* RPL_PRIVATE_H */