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