Contiki-NG
rpl-icmp6.c
Go to the documentation of this file.
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  */
32 
33 /**
34  * \file
35  * ICMP6 I/O for RPL control messages.
36  *
37  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
38  * Contributors: Niclas Finne <nfi@sics.se>, Joel Hoglund <joel@sics.se>,
39  * Mathieu Pouillot <m.pouillot@watteco.com>
40  * George Oikonomou <oikonomou@users.sourceforge.net> (multicast)
41  */
42 
43 /**
44  * \addtogroup uip
45  * @{
46  */
47 
48 #include "net/ipv6/tcpip.h"
49 #include "net/ipv6/uip.h"
50 #include "net/ipv6/uip-ds6.h"
51 #include "net/ipv6/uip-nd6.h"
52 #include "net/ipv6/uip-sr.h"
53 #include "net/ipv6/uip-icmp6.h"
54 #include "net/routing/rpl-classic/rpl-private.h"
55 #include "net/packetbuf.h"
57 #include "random.h"
58 
59 #include "sys/log.h"
60 
61 #include <limits.h>
62 #include <string.h>
63 
64 #define LOG_MODULE "RPL"
65 #define LOG_LEVEL LOG_LEVEL_RPL
66 
67 /*---------------------------------------------------------------------------*/
68 #define RPL_DIO_GROUNDED 0x80
69 #define RPL_DIO_MOP_SHIFT 3
70 #define RPL_DIO_MOP_MASK 0x38
71 #define RPL_DIO_PREFERENCE_MASK 0x07
72 
73 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
74 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
75 #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len])
76 /*---------------------------------------------------------------------------*/
77 static void dis_input(void);
78 static void dio_input(void);
79 static void dao_input(void);
80 static void dao_ack_input(void);
81 
82 static void dao_output_target_seq(rpl_parent_t *parent, uip_ipaddr_t *prefix,
83  uint8_t lifetime, uint8_t seq_no);
84 
85 /* some debug callbacks useful when debugging RPL networks */
86 #ifdef RPL_DEBUG_DIO_INPUT
87 void RPL_DEBUG_DIO_INPUT(uip_ipaddr_t *, rpl_dio_t *);
88 #endif
89 
90 #ifdef RPL_DEBUG_DAO_OUTPUT
91 void RPL_DEBUG_DAO_OUTPUT(rpl_parent_t *);
92 #endif
93 
94 static uint8_t dao_sequence = RPL_LOLLIPOP_INIT;
95 
96 #if RPL_WITH_MULTICAST
97 static uip_mcast6_route_t *mcast_group;
98 #endif
99 /*---------------------------------------------------------------------------*/
100 /* Initialise RPL ICMPv6 message handlers */
101 UIP_ICMP6_HANDLER(dis_handler, ICMP6_RPL, RPL_CODE_DIS, dis_input);
102 UIP_ICMP6_HANDLER(dio_handler, ICMP6_RPL, RPL_CODE_DIO, dio_input);
103 UIP_ICMP6_HANDLER(dao_handler, ICMP6_RPL, RPL_CODE_DAO, dao_input);
104 UIP_ICMP6_HANDLER(dao_ack_handler, ICMP6_RPL, RPL_CODE_DAO_ACK, dao_ack_input);
105 /*---------------------------------------------------------------------------*/
106 
107 #if RPL_WITH_DAO_ACK
108 static uip_ds6_route_t *
109 find_route_entry_by_dao_ack(uint8_t seq)
110 {
111  uip_ds6_route_t *re;
112  re = uip_ds6_route_head();
113  while(re != NULL) {
114  if(re->state.dao_seqno_out == seq && RPL_ROUTE_IS_DAO_PENDING(re)) {
115  /* found it! */
116  return re;
117  }
118  re = uip_ds6_route_next(re);
119  }
120  return NULL;
121 }
122 #endif /* RPL_WITH_DAO_ACK */
123 
124 #if RPL_WITH_STORING
125 /* prepare for forwarding of DAO */
126 static uint8_t
127 prepare_for_dao_fwd(uint8_t sequence, uip_ds6_route_t *rep)
128 {
129  /* not pending - or pending but not a retransmission */
130  RPL_LOLLIPOP_INCREMENT(dao_sequence);
131 
132  /* set DAO pending and sequence numbers */
133  rep->state.dao_seqno_in = sequence;
134  rep->state.dao_seqno_out = dao_sequence;
135  RPL_ROUTE_SET_DAO_PENDING(rep);
136  return dao_sequence;
137 }
138 #endif /* RPL_WITH_STORING */
139 /*---------------------------------------------------------------------------*/
140 static int
141 get_global_addr(uip_ipaddr_t *addr)
142 {
143  int i;
144  int state;
145  uip_ipaddr_t *prefix = NULL;
146  uint8_t prefix_length = 0;
147  rpl_dag_t *dag = rpl_get_any_dag();
148 
149  if(dag != NULL && dag->prefix_info.length != 0) {
150  prefix = &dag->prefix_info.prefix;
151  prefix_length = dag->prefix_info.length;
152  }
153 
154  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
155  state = uip_ds6_if.addr_list[i].state;
156  if(uip_ds6_if.addr_list[i].isused &&
157  state == ADDR_PREFERRED &&
158  !uip_is_addr_linklocal(&uip_ds6_if.addr_list[i].ipaddr) &&
159  (prefix == NULL || uip_ipaddr_prefixcmp(prefix, &uip_ds6_if.addr_list[i].ipaddr, prefix_length))) {
160  memcpy(addr, &uip_ds6_if.addr_list[i].ipaddr, sizeof(uip_ipaddr_t));
161  return 1;
162  }
163  }
164  return 0;
165 }
166 /*---------------------------------------------------------------------------*/
167 static uint32_t
168 get32(uint8_t *buffer, int pos)
169 {
170  return ((uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
171  (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3]);
172 }
173 /*---------------------------------------------------------------------------*/
174 static void
175 set32(uint8_t *buffer, int pos, uint32_t value)
176 {
177  buffer[pos++] = value >> 24;
178  buffer[pos++] = (value >> 16) & 0xff;
179  buffer[pos++] = (value >> 8) & 0xff;
180  buffer[pos++] = value & 0xff;
181 }
182 /*---------------------------------------------------------------------------*/
183 static uint16_t
184 get16(uint8_t *buffer, int pos)
185 {
186  return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
187 }
188 /*---------------------------------------------------------------------------*/
189 static void
190 set16(uint8_t *buffer, int pos, uint16_t value)
191 {
192  buffer[pos++] = value >> 8;
193  buffer[pos++] = value & 0xff;
194 }
195 /*---------------------------------------------------------------------------*/
197 rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason, void *data)
198 {
200 
201  if((nbr = uip_ds6_nbr_lookup(from)) == NULL) {
202  if((nbr = uip_ds6_nbr_add(from, (uip_lladdr_t *)
203  packetbuf_addr(PACKETBUF_ADDR_SENDER),
204  0, NBR_REACHABLE, reason, data)) != NULL) {
205  LOG_INFO("Neighbor added to neighbor cache ");
206  LOG_INFO_6ADDR(from);
207  LOG_INFO_(", ");
208  LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
209  LOG_INFO_("\n");
210  }
211  }
212 
213  return nbr;
214 }
215 /*---------------------------------------------------------------------------*/
216 static void
217 dis_input(void)
218 {
219  rpl_instance_t *instance;
220  rpl_instance_t *end;
221 
222  /* DAG Information Solicitation */
223  LOG_INFO("Received a DIS from ");
224  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
225  LOG_INFO_("\n");
226 
227  for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES;
228  instance < end; ++instance) {
229  if(instance->used == 1) {
230  if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
231 #if RPL_LEAF_ONLY
232  LOG_INFO("LEAF ONLY Multicast DIS will NOT reset DIO timer\n");
233 #else /* !RPL_LEAF_ONLY */
234  LOG_DBG("Multicast DIS => reset DIO timer\n");
235  rpl_reset_dio_timer(instance);
236 #endif /* !RPL_LEAF_ONLY */
237  } else {
238  /* Check if this neighbor should be added according to the policy. */
239  if(rpl_icmp6_update_nbr_table(&UIP_IP_BUF->srcipaddr,
240  NBR_TABLE_REASON_RPL_DIS, NULL) == NULL) {
241  LOG_ERR("Out of Memory, not sending unicast DIO, DIS from ");
242  LOG_ERR_6ADDR(&UIP_IP_BUF->srcipaddr);
243  LOG_ERR_(", ");
244  LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
245  LOG_ERR_("\n");
246  } else {
247  LOG_DBG("Unicast DIS, reply to sender\n");
248  dio_output(instance, &UIP_IP_BUF->srcipaddr);
249  }
250  /* } */
251  }
252  }
253  }
254  uip_clear_buf();
255 }
256 /*---------------------------------------------------------------------------*/
257 void
258 dis_output(uip_ipaddr_t *addr)
259 {
260  unsigned char *buffer;
261  uip_ipaddr_t tmpaddr;
262 
263  /*
264  * DAG Information Solicitation - 2 bytes reserved
265  * 0 1 2
266  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
267  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268  * | Flags | Reserved | Option(s)...
269  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270  */
271 
272  buffer = UIP_ICMP_PAYLOAD;
273  buffer[0] = buffer[1] = 0;
274 
275  if(addr == NULL) {
277  addr = &tmpaddr;
278  }
279 
280  LOG_INFO("Sending a DIS to ");
281  LOG_INFO_6ADDR(addr);
282  LOG_INFO_("\n");
283 
284  uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 2);
285 }
286 /*---------------------------------------------------------------------------*/
287 static void
288 dio_input(void)
289 {
290  unsigned char *buffer;
291  uint8_t buffer_length;
292  rpl_dio_t dio;
293  uint8_t subopt_type;
294  int i;
295  int len;
296  uip_ipaddr_t from;
297 
298  memset(&dio, 0, sizeof(dio));
299 
300  /* Set default values in case the DIO configuration option is missing. */
301  dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
302  dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
303  dio.dag_redund = RPL_DIO_REDUNDANCY;
304  dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
305  dio.dag_max_rankinc = RPL_MAX_RANKINC;
306  dio.ocp = RPL_OF_OCP;
307  dio.default_lifetime = RPL_DEFAULT_LIFETIME;
308  dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
309 
310  uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
311 
312  /* DAG Information Object */
313  LOG_INFO("Received a DIO from ");
314  LOG_INFO_6ADDR(&from);
315  LOG_INFO_("\n");
316 
317  buffer_length = uip_len - uip_l3_icmp_hdr_len;
318 
319  /* Process the DIO base option. */
320  i = 0;
321  buffer = UIP_ICMP_PAYLOAD;
322 
323  dio.instance_id = buffer[i++];
324  dio.version = buffer[i++];
325  dio.rank = get16(buffer, i);
326  i += 2;
327 
328  LOG_DBG("Incoming DIO (id, ver, rank) = (%u,%u,%u)\n",
329  (unsigned)dio.instance_id,
330  (unsigned)dio.version,
331  (unsigned)dio.rank);
332 
333  dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
334  dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
335  dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
336 
337  dio.dtsn = buffer[i++];
338  /* two reserved bytes */
339  i += 2;
340 
341  memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id));
342  i += sizeof(dio.dag_id);
343 
344  LOG_DBG("Incoming DIO (dag_id, pref) = (");
345  LOG_DBG_6ADDR(&dio.dag_id);
346  LOG_DBG_(", %u)\n", dio.preference);
347 
348  /* Check if there are any DIO suboptions. */
349  for(; i < buffer_length; i += len) {
350  subopt_type = buffer[i];
351  if(subopt_type == RPL_OPTION_PAD1) {
352  len = 1;
353  } else {
354  /* Suboption with a two-byte header + payload */
355  len = 2 + buffer[i + 1];
356  }
357 
358  if(len + i > buffer_length) {
359  LOG_WARN("Invalid DIO packet\n");
360  RPL_STAT(rpl_stats.malformed_msgs++);
361  goto discard;
362  }
363 
364  LOG_DBG("Incoming DIO (option, length) = (%u, %u)\n", subopt_type, len - 2);
365 
366  switch(subopt_type) {
367  case RPL_OPTION_DAG_METRIC_CONTAINER:
368  if(len < 6) {
369  LOG_WARN("Invalid DAG MC, len = %d\n", len);
370  RPL_STAT(rpl_stats.malformed_msgs++);
371  goto discard;
372  }
373  dio.mc.type = buffer[i + 2];
374  dio.mc.flags = buffer[i + 3] << 1;
375  dio.mc.flags |= buffer[i + 4] >> 7;
376  dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
377  dio.mc.prec = buffer[i + 4] & 0xf;
378  dio.mc.length = buffer[i + 5];
379 
380  if(dio.mc.type == RPL_DAG_MC_NONE) {
381  /* No metric container: do nothing */
382  } else if(dio.mc.type == RPL_DAG_MC_ETX) {
383  dio.mc.obj.etx = get16(buffer, i + 6);
384 
385  LOG_DBG("DAG MC: type %u, flags %u, aggr %u, prec %u, length %u, ETX %u\n",
386  (unsigned)dio.mc.type,
387  (unsigned)dio.mc.flags,
388  (unsigned)dio.mc.aggr,
389  (unsigned)dio.mc.prec,
390  (unsigned)dio.mc.length,
391  (unsigned)dio.mc.obj.etx);
392  } else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
393  dio.mc.obj.energy.flags = buffer[i + 6];
394  dio.mc.obj.energy.energy_est = buffer[i + 7];
395  } else {
396  LOG_WARN("Unhandled DAG MC type: %u\n", (unsigned)dio.mc.type);
397  goto discard;
398  }
399  break;
400  case RPL_OPTION_ROUTE_INFO:
401  if(len < 9) {
402  LOG_WARN("Invalid destination prefix option, len = %d\n", len);
403  RPL_STAT(rpl_stats.malformed_msgs++);
404  goto discard;
405  }
406 
407  /* The flags field includes the preference value. */
408  dio.destination_prefix.length = buffer[i + 2];
409  dio.destination_prefix.flags = buffer[i + 3];
410  dio.destination_prefix.lifetime = get32(buffer, i + 4);
411 
412  if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
413  dio.destination_prefix.length <= 128) {
414  LOG_INFO("Copying destination prefix\n");
415  memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
416  (dio.destination_prefix.length + 7) / 8);
417  } else {
418  LOG_WARN("Invalid route info option, len = %d\n", len);
419  RPL_STAT(rpl_stats.malformed_msgs++);
420  goto discard;
421  }
422 
423  break;
424  case RPL_OPTION_DAG_CONF:
425  if(len != 16) {
426  LOG_WARN("Invalid DAG configuration option, len = %d\n", len);
427  RPL_STAT(rpl_stats.malformed_msgs++);
428  goto discard;
429  }
430 
431  /* Path control field not yet implemented - at i + 2 */
432  dio.dag_intdoubl = buffer[i + 3];
433  dio.dag_intmin = buffer[i + 4];
434  dio.dag_redund = buffer[i + 5];
435  dio.dag_max_rankinc = get16(buffer, i + 6);
436  dio.dag_min_hoprankinc = get16(buffer, i + 8);
437  dio.ocp = get16(buffer, i + 10);
438  /* buffer + 12 is reserved */
439  dio.default_lifetime = buffer[i + 13];
440  dio.lifetime_unit = get16(buffer, i + 14);
441  LOG_INFO("DAG conf:dbl=%d, min=%d red=%d maxinc=%d mininc=%d ocp=%d d_l=%u l_u=%u\n",
442  dio.dag_intdoubl, dio.dag_intmin, dio.dag_redund,
443  dio.dag_max_rankinc, dio.dag_min_hoprankinc, dio.ocp,
444  dio.default_lifetime, dio.lifetime_unit);
445  break;
446  case RPL_OPTION_PREFIX_INFO:
447  if(len != 32) {
448  LOG_WARN("Invalid DAG prefix info, len != 32\n");
449  RPL_STAT(rpl_stats.malformed_msgs++);
450  goto discard;
451  }
452  dio.prefix_info.length = buffer[i + 2];
453  dio.prefix_info.flags = buffer[i + 3];
454  /* valid lifetime is ingnored for now - at i + 4 */
455  /* preferred lifetime stored in lifetime */
456  dio.prefix_info.lifetime = get32(buffer, i + 8);
457  /* 32-bit reserved at i + 12 */
458  LOG_INFO("Copying prefix information\n");
459  memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
460  break;
461  default:
462  LOG_WARN("Unsupported suboption type in DIO: %u\n",
463  (unsigned)subopt_type);
464  }
465  }
466 
467 #ifdef RPL_DEBUG_DIO_INPUT
468  RPL_DEBUG_DIO_INPUT(&from, &dio);
469 #endif
470 
471  rpl_process_dio(&from, &dio);
472 
473 discard:
474  uip_clear_buf();
475 }
476 /*---------------------------------------------------------------------------*/
477 void
478 dio_output(rpl_instance_t *instance, uip_ipaddr_t *uc_addr)
479 {
480  unsigned char *buffer;
481  int pos;
482  int is_root;
483  rpl_dag_t *dag = instance->current_dag;
484 #if !RPL_LEAF_ONLY
485  uip_ipaddr_t addr;
486 #endif /* !RPL_LEAF_ONLY */
487 
488 #if RPL_LEAF_ONLY
489  /* In leaf mode, we only send DIO messages as unicasts in response to
490  unicast DIS messages. */
491  if(uc_addr == NULL) {
492  LOG_DBG("LEAF ONLY have multicast addr: skip dio_output\n");
493  return;
494  }
495 #endif /* RPL_LEAF_ONLY */
496 
497  /* DAG Information Object */
498  pos = 0;
499 
500  buffer = UIP_ICMP_PAYLOAD;
501  buffer[pos++] = instance->instance_id;
502  buffer[pos++] = dag->version;
503  is_root = (dag->rank == ROOT_RANK(instance));
504 
505 #if RPL_LEAF_ONLY
506  LOG_DBG("LEAF ONLY DIO rank set to RPL_INFINITE_RANK\n");
507  set16(buffer, pos, RPL_INFINITE_RANK);
508 #else /* RPL_LEAF_ONLY */
509  set16(buffer, pos, dag->rank);
510 #endif /* RPL_LEAF_ONLY */
511  pos += 2;
512 
513  buffer[pos] = 0;
514  if(dag->grounded) {
515  buffer[pos] |= RPL_DIO_GROUNDED;
516  }
517 
518  buffer[pos] |= instance->mop << RPL_DIO_MOP_SHIFT;
519  buffer[pos] |= dag->preference & RPL_DIO_PREFERENCE_MASK;
520  pos++;
521 
522  buffer[pos++] = instance->dtsn_out;
523 
524  if(RPL_DIO_REFRESH_DAO_ROUTES && is_root && uc_addr == NULL) {
525  /* Request new DAO to refresh route. We do not do this for unicast DIO
526  * in order to avoid DAO messages after a DIS-DIO update,
527  * or upon unicast DIO probing. */
528  RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
529  }
530 
531  /* reserved 2 bytes */
532  buffer[pos++] = 0; /* flags */
533  buffer[pos++] = 0; /* reserved */
534 
535  memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
536  pos += 16;
537 
538 #if !RPL_LEAF_ONLY
539  if(instance->mc.type != RPL_DAG_MC_NONE) {
540  instance->of->update_metric_container(instance);
541 
542  buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
543  buffer[pos++] = 6;
544  buffer[pos++] = instance->mc.type;
545  buffer[pos++] = instance->mc.flags >> 1;
546  buffer[pos] = (instance->mc.flags & 1) << 7;
547  buffer[pos++] |= (instance->mc.aggr << 4) | instance->mc.prec;
548  if(instance->mc.type == RPL_DAG_MC_ETX) {
549  buffer[pos++] = 2;
550  set16(buffer, pos, instance->mc.obj.etx);
551  pos += 2;
552  } else if(instance->mc.type == RPL_DAG_MC_ENERGY) {
553  buffer[pos++] = 2;
554  buffer[pos++] = instance->mc.obj.energy.flags;
555  buffer[pos++] = instance->mc.obj.energy.energy_est;
556  } else {
557  LOG_ERR("Unable to send DIO because of unhandled DAG MC type %u\n",
558  (unsigned)instance->mc.type);
559  return;
560  }
561  }
562 #endif /* !RPL_LEAF_ONLY */
563 
564  /* Always add a DAG configuration option. */
565  buffer[pos++] = RPL_OPTION_DAG_CONF;
566  buffer[pos++] = 14;
567  buffer[pos++] = 0; /* No Auth, PCS = 0 */
568  buffer[pos++] = instance->dio_intdoubl;
569  buffer[pos++] = instance->dio_intmin;
570  buffer[pos++] = instance->dio_redundancy;
571  set16(buffer, pos, instance->max_rankinc);
572  pos += 2;
573  set16(buffer, pos, instance->min_hoprankinc);
574  pos += 2;
575  /* OCP is in the DAG_CONF option */
576  set16(buffer, pos, instance->of->ocp);
577  pos += 2;
578  buffer[pos++] = 0; /* reserved */
579  buffer[pos++] = instance->default_lifetime;
580  set16(buffer, pos, instance->lifetime_unit);
581  pos += 2;
582 
583  /* Check if we have a prefix to send also. */
584  if(dag->prefix_info.length > 0) {
585  buffer[pos++] = RPL_OPTION_PREFIX_INFO;
586  buffer[pos++] = 30; /* always 30 bytes + 2 long */
587  buffer[pos++] = dag->prefix_info.length;
588  buffer[pos++] = dag->prefix_info.flags;
589  set32(buffer, pos, dag->prefix_info.lifetime);
590  pos += 4;
591  set32(buffer, pos, dag->prefix_info.lifetime);
592  pos += 4;
593  memset(&buffer[pos], 0, 4);
594  pos += 4;
595  memcpy(&buffer[pos], &dag->prefix_info.prefix, 16);
596  pos += 16;
597  LOG_DBG("Sending prefix info in DIO for ");
598  LOG_DBG_6ADDR(&dag->prefix_info.prefix);
599  LOG_DBG_("\n");
600  } else {
601  LOG_DBG("No prefix to announce (len %d)\n",
602  dag->prefix_info.length);
603  }
604 
605 #if RPL_LEAF_ONLY
606  if(LOG_DBG_ENABLED) {
607  if(uc_addr == NULL) {
608  LOG_DBG("LEAF ONLY sending unicast-DIO from multicast-DIO\n");
609  }
610  }
611 
612  LOG_INFO("Sending unicast-DIO with rank %u to ",
613  (unsigned)dag->rank);
614  LOG_INFO_6ADDR(uc_addr);
615  LOG_INFO_("\n");
616  uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
617 #else /* RPL_LEAF_ONLY */
618  /* Unicast requests get unicast replies! */
619  if(uc_addr == NULL) {
620  LOG_INFO("Sending a multicast-DIO with rank %u\n",
621  (unsigned)instance->current_dag->rank);
623  uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DIO, pos);
624  } else {
625  LOG_INFO("Sending unicast-DIO with rank %u to ",
626  (unsigned)instance->current_dag->rank);
627  LOG_INFO_6ADDR(uc_addr);
628  LOG_INFO_("\n");
629  uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
630  }
631 #endif /* RPL_LEAF_ONLY */
632 }
633 /*---------------------------------------------------------------------------*/
634 static void
635 dao_input_storing(void)
636 {
637 #if RPL_WITH_STORING
638  uip_ipaddr_t dao_sender_addr;
639  rpl_dag_t *dag;
640  rpl_instance_t *instance;
641  unsigned char *buffer;
642  uint16_t sequence;
643  uint8_t instance_id;
644  uint8_t lifetime;
645  uint8_t prefixlen;
646  uint8_t flags;
647  uint8_t subopt_type;
648  /*
649  uint8_t pathcontrol;
650  uint8_t pathsequence;
651  */
652  uip_ipaddr_t prefix;
653  uip_ds6_route_t *rep;
654  uint8_t buffer_length;
655  int pos;
656  int len;
657  int i;
658  int learned_from;
659  rpl_parent_t *parent;
661  int is_root;
662 
663  prefixlen = 0;
664  parent = NULL;
665  memset(&prefix, 0, sizeof(prefix));
666 
667  uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
668 
669  buffer = UIP_ICMP_PAYLOAD;
670  buffer_length = uip_len - uip_l3_icmp_hdr_len;
671 
672  pos = 0;
673  instance_id = buffer[pos++];
674 
675  instance = rpl_get_instance(instance_id);
676 
677  lifetime = instance->default_lifetime;
678 
679  flags = buffer[pos++];
680  /* reserved */
681  pos++;
682  sequence = buffer[pos++];
683 
684  dag = instance->current_dag;
685  is_root = (dag->rank == ROOT_RANK(instance));
686 
687  /* Is the DAG ID present? */
688  if(flags & RPL_DAO_D_FLAG) {
689  if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
690  LOG_INFO("Ignoring a DAO for a DAG different from ours\n");
691  return;
692  }
693  pos += 16;
694  }
695 
696  learned_from = uip_is_addr_mcast(&dao_sender_addr) ?
697  RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
698 
699  /* Destination Advertisement Object */
700  LOG_DBG("Received a (%s) DAO with sequence number %u from ",
701  learned_from == RPL_ROUTE_FROM_UNICAST_DAO? "unicast": "multicast", sequence);
702  LOG_DBG_6ADDR(&dao_sender_addr);
703  LOG_DBG_("\n");
704 
705  if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
706  /* Check whether this is a DAO forwarding loop. */
707  parent = rpl_find_parent(dag, &dao_sender_addr);
708  /* check if this is a new DAO registration with an "illegal" rank */
709  /* if we already route to this node it is likely */
710  if(parent != NULL &&
711  DAG_RANK(parent->rank, instance) < DAG_RANK(dag->rank, instance)) {
712  LOG_WARN("Loop detected when receiving a unicast DAO from a node with a lower rank! (%u < %u)\n",
713  DAG_RANK(parent->rank, instance), DAG_RANK(dag->rank, instance));
714  parent->rank = RPL_INFINITE_RANK;
715  parent->flags |= RPL_PARENT_FLAG_UPDATED;
716  return;
717  }
718 
719  /* If we get the DAO from our parent, we also have a loop. */
720  if(parent != NULL && parent == dag->preferred_parent) {
721  LOG_WARN("Loop detected when receiving a unicast DAO from our parent\n");
722  parent->rank = RPL_INFINITE_RANK;
723  parent->flags |= RPL_PARENT_FLAG_UPDATED;
724  return;
725  }
726  }
727 
728  /* Check if there are any RPL options present. */
729  for(i = pos; i < buffer_length; i += len) {
730  subopt_type = buffer[i];
731  if(subopt_type == RPL_OPTION_PAD1) {
732  len = 1;
733  } else {
734  /* The option consists of a two-byte header and a payload. */
735  len = 2 + buffer[i + 1];
736  }
737 
738  switch(subopt_type) {
739  case RPL_OPTION_TARGET:
740  /* Handle the target option. */
741  prefixlen = buffer[i + 3];
742  memset(&prefix, 0, sizeof(prefix));
743  memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
744  break;
745  case RPL_OPTION_TRANSIT:
746  /* The path sequence and control are ignored. */
747  /* pathcontrol = buffer[i + 3];
748  pathsequence = buffer[i + 4];*/
749  lifetime = buffer[i + 5];
750  /* The parent address is also ignored. */
751  break;
752  }
753  }
754 
755  LOG_INFO("DAO lifetime: %u, prefix length: %u prefix: ",
756  (unsigned)lifetime, (unsigned)prefixlen);
757  LOG_INFO_6ADDR(&prefix);
758  LOG_INFO_("\n");
759 
760 #if RPL_WITH_MULTICAST
761  if(uip_is_addr_mcast_global(&prefix)) {
762  /*
763  * "rep" is used for a unicast route which we don't need now; so set NULL so
764  * that operations on "rep" will be skipped.
765  */
766  rep = NULL;
767  mcast_group = uip_mcast6_route_add(&prefix);
768  if(mcast_group) {
769  mcast_group->dag = dag;
770  mcast_group->lifetime = RPL_LIFETIME(instance, lifetime);
771  }
772  goto fwd_dao;
773  }
774 #endif
775 
776  rep = uip_ds6_route_lookup(&prefix);
777 
778  if(lifetime == RPL_ZERO_LIFETIME) {
779  LOG_INFO("No-Path DAO received\n");
780  /* No-Path DAO received; invoke the route purging routine. */
781  if(rep != NULL &&
782  !RPL_ROUTE_IS_NOPATH_RECEIVED(rep) &&
783  rep->length == prefixlen &&
784  uip_ds6_route_nexthop(rep) != NULL &&
785  uip_ipaddr_cmp(uip_ds6_route_nexthop(rep), &dao_sender_addr)) {
786  LOG_DBG("Setting expiration timer for prefix ");
787  LOG_DBG_6ADDR(&prefix);
788  LOG_DBG_("\n");
789  RPL_ROUTE_SET_NOPATH_RECEIVED(rep);
790  rep->state.lifetime = RPL_NOPATH_REMOVAL_DELAY;
791 
792  /* We forward the incoming No-Path DAO to our parent, if we have
793  one. */
794  if(dag->preferred_parent != NULL &&
795  rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
796  uint8_t out_seq;
797  out_seq = prepare_for_dao_fwd(sequence, rep);
798 
799  LOG_DBG("Forwarding No-path DAO to parent - out_seq:%d",
800  out_seq);
801  LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
802  LOG_DBG_("\n");
803 
804  buffer = UIP_ICMP_PAYLOAD;
805  buffer[3] = out_seq; /* add an outgoing seq no before fwd */
806  uip_icmp6_send(rpl_parent_get_ipaddr(dag->preferred_parent),
807  ICMP6_RPL, RPL_CODE_DAO, buffer_length);
808  }
809  }
810  /* independent if we remove or not - ACK the request */
811  if(flags & RPL_DAO_K_FLAG) {
812  /* indicate that we accepted the no-path DAO */
813  uip_clear_buf();
814  dao_ack_output(instance, &dao_sender_addr, sequence,
815  RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
816  }
817  return;
818  }
819 
820  LOG_INFO("Adding DAO route\n");
821 
822  /* Update and add neighbor - if no room - fail. */
823  if((nbr = rpl_icmp6_update_nbr_table(&dao_sender_addr, NBR_TABLE_REASON_RPL_DAO, instance)) == NULL) {
824  LOG_ERR("Out of Memory, dropping DAO from ");
825  LOG_ERR_6ADDR(&dao_sender_addr);
826  LOG_ERR_(", ");
827  LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
828  LOG_ERR_("\n");
829  if(flags & RPL_DAO_K_FLAG) {
830  /* signal the failure to add the node */
831  dao_ack_output(instance, &dao_sender_addr, sequence,
832  is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
833  RPL_DAO_ACK_UNABLE_TO_ACCEPT);
834  }
835  return;
836  }
837 
838  rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
839  if(rep == NULL) {
840  RPL_STAT(rpl_stats.mem_overflows++);
841  LOG_ERR("Could not add a route after receiving a DAO\n");
842  if(flags & RPL_DAO_K_FLAG) {
843  /* signal the failure to add the node */
844  dao_ack_output(instance, &dao_sender_addr, sequence,
845  is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
846  RPL_DAO_ACK_UNABLE_TO_ACCEPT);
847  }
848  return;
849  }
850 
851  /* set lifetime and clear NOPATH bit */
852  rep->state.lifetime = RPL_LIFETIME(instance, lifetime);
853  RPL_ROUTE_CLEAR_NOPATH_RECEIVED(rep);
854 
855 #if RPL_WITH_MULTICAST
856 fwd_dao:
857 #endif
858 
859  if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
860  int should_ack = 0;
861 
862  if(flags & RPL_DAO_K_FLAG) {
863  if(rep != NULL) {
864  /*
865  * check if this route is already installed and we can ack now!
866  * not pending - and same seq-no means that we can ack.
867  * (e.g. the route is installed already so it will not take any
868  * more room that it already takes - so should be ok!)
869  */
870  if((!RPL_ROUTE_IS_DAO_PENDING(rep) &&
871  rep->state.dao_seqno_in == sequence) ||
872  dag->rank == ROOT_RANK(instance)) {
873  should_ack = 1;
874  }
875  }
876  }
877 
878  if(dag->preferred_parent != NULL &&
879  rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
880  uint8_t out_seq = 0;
881  if(rep != NULL) {
882  /* if this is pending and we get the same seq no it is a retrans */
883  if(RPL_ROUTE_IS_DAO_PENDING(rep) &&
884  rep->state.dao_seqno_in == sequence) {
885  /* keep the same seq-no as before for parent also */
886  out_seq = rep->state.dao_seqno_out;
887  } else {
888  out_seq = prepare_for_dao_fwd(sequence, rep);
889  }
890  }
891 
892  LOG_DBG("Forwarding DAO to parent ");
893  LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
894  LOG_DBG_(" in seq: %d out seq: %d\n", sequence, out_seq);
895 
896  buffer = UIP_ICMP_PAYLOAD;
897  buffer[3] = out_seq; /* add an outgoing seq no before fwd */
898  uip_icmp6_send(rpl_parent_get_ipaddr(dag->preferred_parent),
899  ICMP6_RPL, RPL_CODE_DAO, buffer_length);
900  }
901  if(should_ack) {
902  LOG_DBG("Sending DAO ACK\n");
903  uip_clear_buf();
904  dao_ack_output(instance, &dao_sender_addr, sequence,
905  RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
906  }
907  }
908 #endif /* RPL_WITH_STORING */
909 }
910 /*---------------------------------------------------------------------------*/
911 static void
912 dao_input_nonstoring(void)
913 {
914 #if RPL_WITH_NON_STORING
915  uip_ipaddr_t dao_sender_addr;
916  uip_ipaddr_t dao_parent_addr;
917  rpl_dag_t *dag;
918  rpl_instance_t *instance;
919  unsigned char *buffer;
920  uint16_t sequence;
921  uint8_t instance_id;
922  uint8_t lifetime;
923  uint8_t prefixlen;
924  uint8_t flags;
925  uint8_t subopt_type;
926  uip_ipaddr_t prefix;
927  uint8_t buffer_length;
928  int pos;
929  int len;
930  int i;
931 
932  /* Destination Advertisement Object */
933  LOG_INFO("Received a DAO from ");
934  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
935  LOG_INFO_("\n");
936 
937  prefixlen = 0;
938 
939  uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
940  memset(&dao_parent_addr, 0, 16);
941 
942  buffer = UIP_ICMP_PAYLOAD;
943  buffer_length = uip_len - uip_l3_icmp_hdr_len;
944 
945  pos = 0;
946  instance_id = buffer[pos++];
947  instance = rpl_get_instance(instance_id);
948  lifetime = instance->default_lifetime;
949 
950  flags = buffer[pos++];
951  /* reserved */
952  pos++;
953  sequence = buffer[pos++];
954 
955  dag = instance->current_dag;
956  /* Is the DAG ID present? */
957  if(flags & RPL_DAO_D_FLAG) {
958  if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
959  LOG_INFO("Ignoring a DAO for a DAG different from ours\n");
960  return;
961  }
962  pos += 16;
963  }
964 
965  /* Check if there are any RPL options present. */
966  for(i = pos; i < buffer_length; i += len) {
967  subopt_type = buffer[i];
968  if(subopt_type == RPL_OPTION_PAD1) {
969  len = 1;
970  } else {
971  /* The option consists of a two-byte header and a payload. */
972  len = 2 + buffer[i + 1];
973  }
974 
975  switch(subopt_type) {
976  case RPL_OPTION_TARGET:
977  /* Handle the target option. */
978  prefixlen = buffer[i + 3];
979  memset(&prefix, 0, sizeof(prefix));
980  memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
981  break;
982  case RPL_OPTION_TRANSIT:
983  /* The path sequence and control are ignored. */
984  /* pathcontrol = buffer[i + 3];
985  pathsequence = buffer[i + 4];*/
986  lifetime = buffer[i + 5];
987  if(len >= 20) {
988  memcpy(&dao_parent_addr, buffer + i + 6, 16);
989  }
990  break;
991  }
992  }
993 
994  LOG_INFO("DAO lifetime: %u, prefix length: %u prefix: ",
995  (unsigned)lifetime, (unsigned)prefixlen);
996  LOG_INFO_6ADDR(&prefix);
997  LOG_INFO_(", parent: ");
998  LOG_INFO_6ADDR(&dao_parent_addr);
999  LOG_INFO_("\n");
1000 
1001  if(lifetime == RPL_ZERO_LIFETIME) {
1002  LOG_DBG("No-Path DAO received\n");
1003  uip_sr_expire_parent(dag, &prefix, &dao_parent_addr);
1004  } else {
1005  if(uip_sr_update_node(dag, &prefix, &dao_parent_addr, RPL_LIFETIME(instance, lifetime)) == NULL) {
1006  LOG_WARN("DAO failed to add link prefix: ");
1007  LOG_WARN_6ADDR(&prefix);
1008  LOG_WARN_(", parent: ");
1009  LOG_WARN_6ADDR(&dao_parent_addr);
1010  LOG_WARN_("\n");
1011  return;
1012  }
1013  }
1014 
1015  if(flags & RPL_DAO_K_FLAG) {
1016  LOG_DBG("Sending DAO ACK\n");
1017  uip_clear_buf();
1018  dao_ack_output(instance, &dao_sender_addr, sequence,
1019  RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
1020  }
1021 #endif /* RPL_WITH_NON_STORING */
1022 }
1023 /*---------------------------------------------------------------------------*/
1024 static void
1025 dao_input(void)
1026 {
1027  rpl_instance_t *instance;
1028  uint8_t instance_id;
1029 
1030  /* Destination Advertisement Object */
1031  LOG_INFO("Received a DAO from ");
1032  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
1033  LOG_INFO_("\n");
1034 
1035  instance_id = UIP_ICMP_PAYLOAD[0];
1036  instance = rpl_get_instance(instance_id);
1037  if(instance == NULL) {
1038  LOG_INFO("Ignoring a DAO for an unknown RPL instance(%u)\n",
1039  instance_id);
1040  goto discard;
1041  }
1042 
1043  if(RPL_IS_STORING(instance)) {
1044  dao_input_storing();
1045  } else if(RPL_IS_NON_STORING(instance)) {
1046  dao_input_nonstoring();
1047  }
1048 
1049 discard:
1050  uip_clear_buf();
1051 }
1052 /*---------------------------------------------------------------------------*/
1053 #if RPL_WITH_DAO_ACK
1054 static void
1055 handle_dao_retransmission(void *ptr)
1056 {
1057  rpl_parent_t *parent;
1058  uip_ipaddr_t prefix;
1059  rpl_instance_t *instance;
1060 
1061  parent = ptr;
1062  if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1063  return;
1064  }
1065  instance = parent->dag->instance;
1066 
1067  if(instance->my_dao_transmissions >= RPL_DAO_MAX_RETRANSMISSIONS) {
1068  /* No more retransmissions - give up. */
1069  if(instance->lifetime_unit == 0xffff && instance->default_lifetime == 0xff) {
1070  /*
1071  * ContikiRPL was previously using infinite lifetime for routes
1072  * and no DAO_ACK configured. This probably means that the root
1073  * and possibly other nodes might be running an old version that
1074  * does not support DAO ack. Assume that everything is ok for
1075  * now and let the normal repair mechanisms detect any problems.
1076  */
1077  return;
1078  }
1079 
1080  if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1081  /* Inform the objective function about the timeout. */
1082  instance->of->dao_ack_callback(parent, RPL_DAO_ACK_TIMEOUT);
1083  }
1084 
1085  /* Perform local repair and hope to find another parent. */
1086  rpl_local_repair(instance);
1087  return;
1088  }
1089 
1090  LOG_INFO("will retransmit DAO - seq:%d trans:%d\n", instance->my_dao_seqno,
1091  instance->my_dao_transmissions);
1092 
1093  if(get_global_addr(&prefix) == 0) {
1094  return;
1095  }
1096 
1097  ctimer_set(&instance->dao_retransmit_timer,
1098  RPL_DAO_RETRANSMISSION_TIMEOUT / 2 +
1099  (random_rand() % (RPL_DAO_RETRANSMISSION_TIMEOUT / 2)),
1100  handle_dao_retransmission, parent);
1101 
1102  instance->my_dao_transmissions++;
1103  dao_output_target_seq(parent, &prefix,
1104  instance->default_lifetime, instance->my_dao_seqno);
1105 }
1106 #endif /* RPL_WITH_DAO_ACK */
1107 /*---------------------------------------------------------------------------*/
1108 void
1109 dao_output(rpl_parent_t *parent, uint8_t lifetime)
1110 {
1111  /* Destination Advertisement Object */
1112  uip_ipaddr_t prefix;
1113 
1114  if(get_global_addr(&prefix) == 0) {
1115  LOG_ERR("No global address set for this node - suppressing DAO\n");
1116  return;
1117  }
1118 
1119  if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1120  return;
1121  }
1122 
1123  RPL_LOLLIPOP_INCREMENT(dao_sequence);
1124 #if RPL_WITH_DAO_ACK
1125  /* set up the state since this will be the first transmission of DAO */
1126  /* retransmissions will call directly to dao_output_target_seq */
1127  /* keep track of my own sending of DAO for handling ack and loss of ack */
1128  if(lifetime != RPL_ZERO_LIFETIME) {
1129  rpl_instance_t *instance;
1130  instance = parent->dag->instance;
1131 
1132  instance->my_dao_seqno = dao_sequence;
1133  instance->my_dao_transmissions = 1;
1134  ctimer_set(&instance->dao_retransmit_timer, RPL_DAO_RETRANSMISSION_TIMEOUT,
1135  handle_dao_retransmission, parent);
1136  }
1137 #else
1138  /* We know that we have tried to register so now we are assuming
1139  that we have a down-link - unless this is a zero lifetime one */
1140  parent->dag->instance->has_downward_route = lifetime != RPL_ZERO_LIFETIME;
1141 #endif /* RPL_WITH_DAO_ACK */
1142 
1143  /* Sending a DAO with own prefix as target */
1144  dao_output_target(parent, &prefix, lifetime);
1145 }
1146 /*---------------------------------------------------------------------------*/
1147 void
1148 dao_output_target(rpl_parent_t *parent, uip_ipaddr_t *prefix, uint8_t lifetime)
1149 {
1150  dao_output_target_seq(parent, prefix, lifetime, dao_sequence);
1151 }
1152 /*---------------------------------------------------------------------------*/
1153 static void
1154 dao_output_target_seq(rpl_parent_t *parent, uip_ipaddr_t *prefix,
1155  uint8_t lifetime, uint8_t seq_no)
1156 {
1157  rpl_dag_t *dag;
1158  rpl_instance_t *instance;
1159  unsigned char *buffer;
1160  uint8_t prefixlen;
1161  int pos;
1162  uip_ipaddr_t *parent_ipaddr = NULL;
1163  uip_ipaddr_t *dest_ipaddr = NULL;
1164 
1165  /* Destination Advertisement Object */
1166 
1167  /* If we are in feather mode, we should not send any DAOs */
1168  if(rpl_get_mode() == RPL_MODE_FEATHER) {
1169  return;
1170  }
1171 
1172  if(parent == NULL) {
1173  LOG_ERR("dao_output_target error parent NULL\n");
1174  return;
1175  }
1176 
1177  parent_ipaddr = rpl_parent_get_ipaddr(parent);
1178  if(parent_ipaddr == NULL) {
1179  LOG_ERR("dao_output_target error parent IP address NULL\n");
1180  return;
1181  }
1182 
1183  dag = parent->dag;
1184  if(dag == NULL) {
1185  LOG_ERR("dao_output_target error dag NULL\n");
1186  return;
1187  }
1188 
1189  instance = dag->instance;
1190 
1191  if(instance == NULL) {
1192  LOG_ERR("dao_output_target error instance NULL\n");
1193  return;
1194  }
1195  if(prefix == NULL) {
1196  LOG_ERR("dao_output_target error prefix NULL\n");
1197  return;
1198  }
1199 #ifdef RPL_DEBUG_DAO_OUTPUT
1200  RPL_DEBUG_DAO_OUTPUT(parent);
1201 #endif
1202 
1203  buffer = UIP_ICMP_PAYLOAD;
1204  pos = 0;
1205 
1206  buffer[pos++] = instance->instance_id;
1207  buffer[pos] = 0;
1208 #if RPL_DAO_SPECIFY_DAG
1209  buffer[pos] |= RPL_DAO_D_FLAG;
1210 #endif /* RPL_DAO_SPECIFY_DAG */
1211 #if RPL_WITH_DAO_ACK
1212  if(lifetime != RPL_ZERO_LIFETIME) {
1213  buffer[pos] |= RPL_DAO_K_FLAG;
1214  }
1215 #endif /* RPL_WITH_DAO_ACK */
1216  ++pos;
1217  buffer[pos++] = 0; /* reserved */
1218  buffer[pos++] = seq_no;
1219 #if RPL_DAO_SPECIFY_DAG
1220  memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
1221  pos+=sizeof(dag->dag_id);
1222 #endif /* RPL_DAO_SPECIFY_DAG */
1223 
1224  /* create target subopt */
1225  prefixlen = sizeof(*prefix) * CHAR_BIT;
1226  buffer[pos++] = RPL_OPTION_TARGET;
1227  buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
1228  buffer[pos++] = 0; /* reserved */
1229  buffer[pos++] = prefixlen;
1230  memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
1231  pos += ((prefixlen + 7) / CHAR_BIT);
1232 
1233  /* Create a transit information sub-option. */
1234  buffer[pos++] = RPL_OPTION_TRANSIT;
1235  buffer[pos++] = (instance->mop != RPL_MOP_NON_STORING) ? 4 : 20;
1236  buffer[pos++] = 0; /* flags - ignored */
1237  buffer[pos++] = 0; /* path control - ignored */
1238  buffer[pos++] = 0; /* path seq - ignored */
1239  buffer[pos++] = lifetime;
1240 
1241  if(instance->mop != RPL_MOP_NON_STORING) {
1242  /* Send DAO to parent */
1243  dest_ipaddr = parent_ipaddr;
1244  } else {
1245  /* Include parent global IP address */
1246  memcpy(buffer + pos, &parent->dag->dag_id, 8); /* Prefix */
1247  pos += 8;
1248  memcpy(buffer + pos, ((const unsigned char *)parent_ipaddr) + 8, 8); /* Interface identifier */
1249  pos += 8;
1250  /* Send DAO to root */
1251  dest_ipaddr = &parent->dag->dag_id;
1252  }
1253 
1254  LOG_INFO("Sending a %sDAO with sequence number %u, lifetime %u, prefix ",
1255  lifetime == RPL_ZERO_LIFETIME ? "No-Path " : "", seq_no, lifetime);
1256 
1257  LOG_INFO_6ADDR(prefix);
1258  LOG_INFO_(" to ");
1259  LOG_INFO_6ADDR(dest_ipaddr);
1260  LOG_INFO_(" , parent ");
1261  LOG_INFO_6ADDR(parent_ipaddr);
1262  LOG_INFO_("\n");
1263 
1264  if(dest_ipaddr != NULL) {
1265  uip_icmp6_send(dest_ipaddr, ICMP6_RPL, RPL_CODE_DAO, pos);
1266  }
1267 }
1268 /*---------------------------------------------------------------------------*/
1269 static void
1270 dao_ack_input(void)
1271 {
1272 #if RPL_WITH_DAO_ACK
1273 
1274  uint8_t *buffer;
1275  uint8_t instance_id;
1276  uint8_t sequence;
1277  uint8_t status;
1278  rpl_instance_t *instance;
1279  rpl_parent_t *parent;
1280 
1281  buffer = UIP_ICMP_PAYLOAD;
1282 
1283  instance_id = buffer[0];
1284  sequence = buffer[2];
1285  status = buffer[3];
1286 
1287  instance = rpl_get_instance(instance_id);
1288  if(instance == NULL) {
1289  uip_clear_buf();
1290  return;
1291  }
1292 
1293  if(RPL_IS_STORING(instance)) {
1294  parent = rpl_find_parent(instance->current_dag, &UIP_IP_BUF->srcipaddr);
1295  if(parent == NULL) {
1296  /* not a known instance - drop the packet and ignore */
1297  uip_clear_buf();
1298  return;
1299  }
1300  } else {
1301  parent = NULL;
1302  }
1303 
1304  if(instance->current_dag->rank == ROOT_RANK(instance)) {
1305  LOG_DBG("DODAG root received a DAO ACK, ignoring it\n");
1306  uip_clear_buf();
1307  return;
1308  }
1309 
1310  LOG_INFO("Received a DAO %s with sequence number %d (%d) and status %d from ",
1311  status < 128 ? "ACK" : "NACK",
1312  sequence, instance->my_dao_seqno, status);
1313  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
1314  LOG_INFO_("\n");
1315 
1316  if(sequence == instance->my_dao_seqno) {
1317  instance->has_downward_route = status < 128;
1318 
1319  /* always stop the retransmit timer when the ACK arrived */
1320  ctimer_stop(&instance->dao_retransmit_timer);
1321 
1322  /* Inform objective function on status of the DAO ACK */
1323  if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1324  instance->of->dao_ack_callback(parent, status);
1325  }
1326 
1327 #if RPL_REPAIR_ON_DAO_NACK
1328  if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1329  /*
1330  * Failed the DAO transmission - need to remove the default route.
1331  * Trigger a local repair since we can not get our DAO in.
1332  */
1333  rpl_local_repair(instance);
1334  }
1335 #endif
1336 
1337  } else if(RPL_IS_STORING(instance)) {
1338  /* this DAO ACK should be forwarded to another recently registered route */
1339  uip_ds6_route_t *re;
1340  uip_ipaddr_t *nexthop;
1341  if((re = find_route_entry_by_dao_ack(sequence)) != NULL) {
1342  /* pick the recorded seq no from that node and forward DAO ACK - and
1343  clear the pending flag*/
1344  RPL_ROUTE_CLEAR_DAO_PENDING(re);
1345 
1346  nexthop = uip_ds6_route_nexthop(re);
1347  if(nexthop == NULL) {
1348  LOG_WARN("No next hop to fwd DAO ACK to\n");
1349  } else {
1350  LOG_INFO("Fwd DAO ACK to:");
1351  LOG_INFO_6ADDR(nexthop);
1352  LOG_INFO_("\n");
1353  buffer[2] = re->state.dao_seqno_in;
1354  uip_icmp6_send(nexthop, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
1355  }
1356 
1357  if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1358  /* this node did not get in to the routing tables above... - remove */
1359  uip_ds6_route_rm(re);
1360  }
1361  } else {
1362  LOG_WARN("No route entry found to forward DAO ACK (seqno %u)\n", sequence);
1363  }
1364  }
1365 #endif /* RPL_WITH_DAO_ACK */
1366  uip_clear_buf();
1367 }
1368 /*---------------------------------------------------------------------------*/
1369 void
1370 dao_ack_output(rpl_instance_t *instance, uip_ipaddr_t *dest, uint8_t sequence,
1371  uint8_t status)
1372 {
1373 #if RPL_WITH_DAO_ACK
1374  unsigned char *buffer;
1375 
1376  LOG_INFO("Sending a DAO %s with sequence number %d to ", status < 128 ? "ACK" : "NACK", sequence);
1377  LOG_INFO_6ADDR(dest);
1378  LOG_INFO_(" with status %d\n", status);
1379 
1380  buffer = UIP_ICMP_PAYLOAD;
1381 
1382  buffer[0] = instance->instance_id;
1383  buffer[1] = 0;
1384  buffer[2] = sequence;
1385  buffer[3] = status;
1386 
1387  uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
1388 #endif /* RPL_WITH_DAO_ACK */
1389 }
1390 /*---------------------------------------------------------------------------*/
1391 void
1392 rpl_icmp6_register_handlers()
1393 {
1394  uip_icmp6_register_input_handler(&dis_handler);
1395  uip_icmp6_register_input_handler(&dio_handler);
1396  uip_icmp6_register_input_handler(&dao_handler);
1397  uip_icmp6_register_input_handler(&dao_ack_handler);
1398 }
1399 /*---------------------------------------------------------------------------*/
1400 
1401 /** @}*/
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:97
Header for the Contiki/uIP interface.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
Header file for ICMPv6 message and error handing (RFC 4443)
RPL DAG structure.
Definition: rpl.h:135
static uip_ds6_nbr_t * nbr
Pointer to llao option in uip_buf.
Definition: uip-nd6.c:115
RPL instance structure.
Definition: rpl.h:219
#define ROOT_RANK
Rank of a root node.
Definition: rpl-types.h:78
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:179
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:116
#define uip_is_addr_mcast_global(a)
is address a global multicast address (FFxE::/16), a is of type uip_ip6addr_t*
Definition: uip.h:2114
enum rpl_mode rpl_get_mode(void)
Get the RPL mode.
Definition: rpl.c:70
#define RPL_LIFETIME(lifetime)
Compute lifetime, accounting for the lifetime unit.
Definition: rpl-types.h:72
void uip_sr_expire_parent(void *graph, const uip_ipaddr_t *child, const uip_ipaddr_t *parent)
Expires a given child-parent link.
Definition: uip-sr.c:113
Source routing support.
Header file for IPv6-related data structures.
An entry in the routing table.
This header file contains configuration directives for uIPv6 multicast support.
#define DAG_RANK(fixpt_rank)
Return DAG RANK as per RFC 6550 (rank divided by min_hoprankinc)
Definition: rpl-types.h:81
void * dag
Pointer to an rpl_dag_t struct.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
rpl_dag_t * rpl_get_any_dag(void)
Returns pointer to any DAG (for compatibility with legagy RPL code)
Definition: rpl-dag.c:1050
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1018
uint32_t lifetime
Entry lifetime seconds.
An entry in the multicast routing table.
uip_ds6_nbr_t * rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason, void *data)
Updates IPv6 neighbor cache on incoming link-local RPL ICMPv6 messages.
Definition: rpl-icmp6.c:197
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition: uip.h:2107
Header file for the uIP TCP/IP stack.
uip_ds6_nbr_t * uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, uint8_t isrouter, uint8_t state, nbr_table_reason_t reason, void *data)
Neighbor Cache basic routines.
Definition: uip-ds6-nbr.c:74
Header file for IPv6 Neighbor discovery (RFC 4861)
uip_ds6_netif_t uip_ds6_if
The single interface.
Definition: uip-ds6.c:75
uip_sr_node_t * uip_sr_update_node(void *graph, const uip_ipaddr_t *child, const uip_ipaddr_t *parent, uint32_t lifetime)
Updates a child-parent link.
Definition: uip-sr.c:123
void rpl_process_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
Processes incoming DIO.
Definition: rpl-dag.c:1454
#define ICMP6_RPL
RPL.
Definition: uip-icmp6.h:66
void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler)
Register a handler which can handle a specific ICMPv6 message type.
Definition: uip-icmp6.c:106
Header file for the Packet buffer (packetbuf) management
#define uip_create_linklocal_rplnodes_mcast(addr)
Set IP address addr to the link-local, all-rpl-nodes multicast address.
Definition: rpl-types.h:54
void rpl_local_repair(const char *str)
Triggers a RPL local repair.
Definition: rpl-dag.c:238
void uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
Send an icmpv6 message.
Definition: uip-icmp6.c:254
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Definition: uip.h:2023
Header file for the logging system
uip_mcast6_route_t * uip_mcast6_route_add(uip_ipaddr_t *group)
Add a multicast route.
An entry in the nbr cache.
Definition: uip-ds6-nbr.h:69