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  dio.prefix_info.flags = buffer[i + 3];
451  /* valid lifetime is ingnored for now - at i + 4 */
452  /* preferred lifetime stored in lifetime */
453  dio.prefix_info.lifetime = get32(buffer, i + 8);
454  /* 32-bit reserved at i + 12 */
455  LOG_INFO("Copying prefix information\n");
456  memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
457  break;
458  default:
459  LOG_WARN("Unsupported suboption type in DIO: %u\n",
460  (unsigned)subopt_type);
461  }
462  }
463 
464 #ifdef RPL_DEBUG_DIO_INPUT
465  RPL_DEBUG_DIO_INPUT(&from, &dio);
466 #endif
467 
468  rpl_process_dio(&from, &dio);
469 
470 discard:
471  uipbuf_clear();
472 }
473 /*---------------------------------------------------------------------------*/
474 void
475 dio_output(rpl_instance_t *instance, uip_ipaddr_t *uc_addr)
476 {
477  unsigned char *buffer;
478  int pos;
479  int is_root;
480  rpl_dag_t *dag = instance->current_dag;
481 #if !RPL_LEAF_ONLY
482  uip_ipaddr_t addr;
483 #endif /* !RPL_LEAF_ONLY */
484 
485 #if RPL_LEAF_ONLY
486  /* In leaf mode, we only send DIO messages as unicasts in response to
487  unicast DIS messages. */
488  if(uc_addr == NULL) {
489  LOG_DBG("LEAF ONLY have multicast addr: skip dio_output\n");
490  return;
491  }
492 #endif /* RPL_LEAF_ONLY */
493 
494  /* DAG Information Object */
495  pos = 0;
496 
497  buffer = UIP_ICMP_PAYLOAD;
498  buffer[pos++] = instance->instance_id;
499  buffer[pos++] = dag->version;
500  is_root = (dag->rank == ROOT_RANK(instance));
501 
502 #if RPL_LEAF_ONLY
503  LOG_DBG("LEAF ONLY DIO rank set to RPL_INFINITE_RANK\n");
504  set16(buffer, pos, RPL_INFINITE_RANK);
505 #else /* RPL_LEAF_ONLY */
506  set16(buffer, pos, dag->rank);
507 #endif /* RPL_LEAF_ONLY */
508  pos += 2;
509 
510  buffer[pos] = 0;
511  if(dag->grounded) {
512  buffer[pos] |= RPL_DIO_GROUNDED;
513  }
514 
515  buffer[pos] |= instance->mop << RPL_DIO_MOP_SHIFT;
516  buffer[pos] |= dag->preference & RPL_DIO_PREFERENCE_MASK;
517  pos++;
518 
519  buffer[pos++] = instance->dtsn_out;
520 
521  if(RPL_DIO_REFRESH_DAO_ROUTES && is_root && uc_addr == NULL) {
522  /* Request new DAO to refresh route. We do not do this for unicast DIO
523  * in order to avoid DAO messages after a DIS-DIO update,
524  * or upon unicast DIO probing. */
525  RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
526  }
527 
528  /* reserved 2 bytes */
529  buffer[pos++] = 0; /* flags */
530  buffer[pos++] = 0; /* reserved */
531 
532  memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
533  pos += 16;
534 
535 #if !RPL_LEAF_ONLY
536  if(instance->mc.type != RPL_DAG_MC_NONE) {
537  instance->of->update_metric_container(instance);
538 
539  buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
540  buffer[pos++] = 6;
541  buffer[pos++] = instance->mc.type;
542  buffer[pos++] = instance->mc.flags >> 1;
543  buffer[pos] = (instance->mc.flags & 1) << 7;
544  buffer[pos++] |= (instance->mc.aggr << 4) | instance->mc.prec;
545  if(instance->mc.type == RPL_DAG_MC_ETX) {
546  buffer[pos++] = 2;
547  set16(buffer, pos, instance->mc.obj.etx);
548  pos += 2;
549  } else if(instance->mc.type == RPL_DAG_MC_ENERGY) {
550  buffer[pos++] = 2;
551  buffer[pos++] = instance->mc.obj.energy.flags;
552  buffer[pos++] = instance->mc.obj.energy.energy_est;
553  } else {
554  LOG_ERR("Unable to send DIO because of unhandled DAG MC type %u\n",
555  (unsigned)instance->mc.type);
556  return;
557  }
558  }
559 #endif /* !RPL_LEAF_ONLY */
560 
561  /* Always add a DAG configuration option. */
562  buffer[pos++] = RPL_OPTION_DAG_CONF;
563  buffer[pos++] = 14;
564  buffer[pos++] = 0; /* No Auth, PCS = 0 */
565  buffer[pos++] = instance->dio_intdoubl;
566  buffer[pos++] = instance->dio_intmin;
567  buffer[pos++] = instance->dio_redundancy;
568  set16(buffer, pos, instance->max_rankinc);
569  pos += 2;
570  set16(buffer, pos, instance->min_hoprankinc);
571  pos += 2;
572  /* OCP is in the DAG_CONF option */
573  set16(buffer, pos, instance->of->ocp);
574  pos += 2;
575  buffer[pos++] = 0; /* reserved */
576  buffer[pos++] = instance->default_lifetime;
577  set16(buffer, pos, instance->lifetime_unit);
578  pos += 2;
579 
580  /* Check if we have a prefix to send also. */
581  if(dag->prefix_info.length > 0) {
582  buffer[pos++] = RPL_OPTION_PREFIX_INFO;
583  buffer[pos++] = 30; /* always 30 bytes + 2 long */
584  buffer[pos++] = dag->prefix_info.length;
585  buffer[pos++] = dag->prefix_info.flags;
586  set32(buffer, pos, dag->prefix_info.lifetime);
587  pos += 4;
588  set32(buffer, pos, dag->prefix_info.lifetime);
589  pos += 4;
590  memset(&buffer[pos], 0, 4);
591  pos += 4;
592  memcpy(&buffer[pos], &dag->prefix_info.prefix, 16);
593  pos += 16;
594  LOG_DBG("Sending prefix info in DIO for ");
595  LOG_DBG_6ADDR(&dag->prefix_info.prefix);
596  LOG_DBG_("\n");
597  } else {
598  LOG_DBG("No prefix to announce (len %d)\n",
599  dag->prefix_info.length);
600  }
601 
602 #if RPL_LEAF_ONLY
603  if(LOG_DBG_ENABLED) {
604  if(uc_addr == NULL) {
605  LOG_DBG("LEAF ONLY sending unicast-DIO from multicast-DIO\n");
606  }
607  }
608 
609  LOG_INFO("Sending unicast-DIO with rank %u to ",
610  (unsigned)dag->rank);
611  LOG_INFO_6ADDR(uc_addr);
612  LOG_INFO_("\n");
613  uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
614 #else /* RPL_LEAF_ONLY */
615  /* Unicast requests get unicast replies! */
616  if(uc_addr == NULL) {
617  LOG_INFO("Sending a multicast-DIO with rank %u\n",
618  (unsigned)instance->current_dag->rank);
620  uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DIO, pos);
621  } else {
622  LOG_INFO("Sending unicast-DIO with rank %u to ",
623  (unsigned)instance->current_dag->rank);
624  LOG_INFO_6ADDR(uc_addr);
625  LOG_INFO_("\n");
626  uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
627  }
628 #endif /* RPL_LEAF_ONLY */
629 }
630 /*---------------------------------------------------------------------------*/
631 static void
632 dao_input_storing(void)
633 {
634 #if RPL_WITH_STORING
635  uip_ipaddr_t dao_sender_addr;
636  rpl_dag_t *dag;
637  rpl_instance_t *instance;
638  unsigned char *buffer;
639  uint16_t sequence;
640  uint8_t instance_id;
641  uint8_t lifetime;
642  uint8_t prefixlen;
643  uint8_t flags;
644  uint8_t subopt_type;
645  /*
646  uint8_t pathcontrol;
647  uint8_t pathsequence;
648  */
649  uip_ipaddr_t prefix;
650  uip_ds6_route_t *rep;
651  uint8_t buffer_length;
652  int pos;
653  int len;
654  int i;
655  int learned_from;
656  rpl_parent_t *parent;
658  int is_root;
659 
660  prefixlen = 0;
661  parent = NULL;
662  memset(&prefix, 0, sizeof(prefix));
663 
664  uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
665 
666  buffer = UIP_ICMP_PAYLOAD;
667  buffer_length = uip_len - uip_l3_icmp_hdr_len;
668 
669  pos = 0;
670  instance_id = buffer[pos++];
671 
672  instance = rpl_get_instance(instance_id);
673 
674  lifetime = instance->default_lifetime;
675 
676  flags = buffer[pos++];
677  /* reserved */
678  pos++;
679  sequence = buffer[pos++];
680 
681  dag = instance->current_dag;
682  is_root = (dag->rank == ROOT_RANK(instance));
683 
684  /* Is the DAG ID present? */
685  if(flags & RPL_DAO_D_FLAG) {
686  if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
687  LOG_INFO("Ignoring a DAO for a DAG different from ours\n");
688  return;
689  }
690  pos += 16;
691  }
692 
693  learned_from = uip_is_addr_mcast(&dao_sender_addr) ?
694  RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
695 
696  /* Destination Advertisement Object */
697  LOG_DBG("Received a (%s) DAO with sequence number %u from ",
698  learned_from == RPL_ROUTE_FROM_UNICAST_DAO? "unicast": "multicast", sequence);
699  LOG_DBG_6ADDR(&dao_sender_addr);
700  LOG_DBG_("\n");
701 
702  if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
703  /* Check whether this is a DAO forwarding loop. */
704  parent = rpl_find_parent(dag, &dao_sender_addr);
705  /* check if this is a new DAO registration with an "illegal" rank */
706  /* if we already route to this node it is likely */
707  if(parent != NULL &&
708  DAG_RANK(parent->rank, instance) < DAG_RANK(dag->rank, instance)) {
709  LOG_WARN("Loop detected when receiving a unicast DAO from a node with a lower rank! (%u < %u)\n",
710  DAG_RANK(parent->rank, instance), DAG_RANK(dag->rank, instance));
711  parent->rank = RPL_INFINITE_RANK;
712  parent->flags |= RPL_PARENT_FLAG_UPDATED;
713  return;
714  }
715 
716  /* If we get the DAO from our parent, we also have a loop. */
717  if(parent != NULL && parent == dag->preferred_parent) {
718  LOG_WARN("Loop detected when receiving a unicast DAO from our parent\n");
719  parent->rank = RPL_INFINITE_RANK;
720  parent->flags |= RPL_PARENT_FLAG_UPDATED;
721  return;
722  }
723  }
724 
725  /* Check if there are any RPL options present. */
726  for(i = pos; i < buffer_length; i += len) {
727  subopt_type = buffer[i];
728  if(subopt_type == RPL_OPTION_PAD1) {
729  len = 1;
730  } else {
731  /* The option consists of a two-byte header and a payload. */
732  len = 2 + buffer[i + 1];
733  }
734 
735  switch(subopt_type) {
736  case RPL_OPTION_TARGET:
737  /* Handle the target option. */
738  prefixlen = buffer[i + 3];
739  memset(&prefix, 0, sizeof(prefix));
740  memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
741  break;
742  case RPL_OPTION_TRANSIT:
743  /* The path sequence and control are ignored. */
744  /* pathcontrol = buffer[i + 3];
745  pathsequence = buffer[i + 4];*/
746  lifetime = buffer[i + 5];
747  /* The parent address is also ignored. */
748  break;
749  }
750  }
751 
752  LOG_INFO("DAO lifetime: %u, prefix length: %u prefix: ",
753  (unsigned)lifetime, (unsigned)prefixlen);
754  LOG_INFO_6ADDR(&prefix);
755  LOG_INFO_("\n");
756 
757 #if RPL_WITH_MULTICAST
758  if(uip_is_addr_mcast_global(&prefix)) {
759  /*
760  * "rep" is used for a unicast route which we don't need now; so set NULL so
761  * that operations on "rep" will be skipped.
762  */
763  rep = NULL;
764  mcast_group = uip_mcast6_route_add(&prefix);
765  if(mcast_group) {
766  mcast_group->dag = dag;
767  mcast_group->lifetime = RPL_LIFETIME(instance, lifetime);
768  }
769  goto fwd_dao;
770  }
771 #endif
772 
773  rep = uip_ds6_route_lookup(&prefix);
774 
775  if(lifetime == RPL_ZERO_LIFETIME) {
776  LOG_INFO("No-Path DAO received\n");
777  /* No-Path DAO received; invoke the route purging routine. */
778  if(rep != NULL &&
779  !RPL_ROUTE_IS_NOPATH_RECEIVED(rep) &&
780  rep->length == prefixlen &&
781  uip_ds6_route_nexthop(rep) != NULL &&
782  uip_ipaddr_cmp(uip_ds6_route_nexthop(rep), &dao_sender_addr)) {
783  LOG_DBG("Setting expiration timer for prefix ");
784  LOG_DBG_6ADDR(&prefix);
785  LOG_DBG_("\n");
786  RPL_ROUTE_SET_NOPATH_RECEIVED(rep);
787  rep->state.lifetime = RPL_NOPATH_REMOVAL_DELAY;
788 
789  /* We forward the incoming No-Path DAO to our parent, if we have
790  one. */
791  if(dag->preferred_parent != NULL &&
792  rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
793  uint8_t out_seq;
794  out_seq = prepare_for_dao_fwd(sequence, rep);
795 
796  LOG_DBG("Forwarding No-path DAO to parent - out_seq:%d",
797  out_seq);
798  LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
799  LOG_DBG_("\n");
800 
801  buffer = UIP_ICMP_PAYLOAD;
802  buffer[3] = out_seq; /* add an outgoing seq no before fwd */
803  uip_icmp6_send(rpl_parent_get_ipaddr(dag->preferred_parent),
804  ICMP6_RPL, RPL_CODE_DAO, buffer_length);
805  }
806  }
807  /* independent if we remove or not - ACK the request */
808  if(flags & RPL_DAO_K_FLAG) {
809  /* indicate that we accepted the no-path DAO */
810  uipbuf_clear();
811  dao_ack_output(instance, &dao_sender_addr, sequence,
812  RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
813  }
814  return;
815  }
816 
817  LOG_INFO("Adding DAO route\n");
818 
819  /* Update and add neighbor - if no room - fail. */
820  if((nbr = rpl_icmp6_update_nbr_table(&dao_sender_addr, NBR_TABLE_REASON_RPL_DAO, instance)) == NULL) {
821  LOG_ERR("Out of Memory, dropping DAO from ");
822  LOG_ERR_6ADDR(&dao_sender_addr);
823  LOG_ERR_(", ");
824  LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
825  LOG_ERR_("\n");
826  if(flags & RPL_DAO_K_FLAG) {
827  /* signal the failure to add the node */
828  dao_ack_output(instance, &dao_sender_addr, sequence,
829  is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
830  RPL_DAO_ACK_UNABLE_TO_ACCEPT);
831  }
832  return;
833  }
834 
835  rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
836  if(rep == NULL) {
837  RPL_STAT(rpl_stats.mem_overflows++);
838  LOG_ERR("Could not add a route after receiving a DAO\n");
839  if(flags & RPL_DAO_K_FLAG) {
840  /* signal the failure to add the node */
841  dao_ack_output(instance, &dao_sender_addr, sequence,
842  is_root ? RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT :
843  RPL_DAO_ACK_UNABLE_TO_ACCEPT);
844  }
845  return;
846  }
847 
848  /* set lifetime and clear NOPATH bit */
849  rep->state.lifetime = RPL_LIFETIME(instance, lifetime);
850  RPL_ROUTE_CLEAR_NOPATH_RECEIVED(rep);
851 
852 #if RPL_WITH_MULTICAST
853 fwd_dao:
854 #endif
855 
856  if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
857  int should_ack = 0;
858 
859  if(flags & RPL_DAO_K_FLAG) {
860  if(rep != NULL) {
861  /*
862  * check if this route is already installed and we can ack now!
863  * not pending - and same seq-no means that we can ack.
864  * (e.g. the route is installed already so it will not take any
865  * more room that it already takes - so should be ok!)
866  */
867  if((!RPL_ROUTE_IS_DAO_PENDING(rep) &&
868  rep->state.dao_seqno_in == sequence) ||
869  dag->rank == ROOT_RANK(instance)) {
870  should_ack = 1;
871  }
872  }
873  }
874 
875  if(dag->preferred_parent != NULL &&
876  rpl_parent_get_ipaddr(dag->preferred_parent) != NULL) {
877  uint8_t out_seq = 0;
878  if(rep != NULL) {
879  /* if this is pending and we get the same seq no it is a retrans */
880  if(RPL_ROUTE_IS_DAO_PENDING(rep) &&
881  rep->state.dao_seqno_in == sequence) {
882  /* keep the same seq-no as before for parent also */
883  out_seq = rep->state.dao_seqno_out;
884  } else {
885  out_seq = prepare_for_dao_fwd(sequence, rep);
886  }
887  }
888 
889  LOG_DBG("Forwarding DAO to parent ");
890  LOG_DBG_6ADDR(rpl_parent_get_ipaddr(dag->preferred_parent));
891  LOG_DBG_(" in seq: %d out seq: %d\n", sequence, out_seq);
892 
893  buffer = UIP_ICMP_PAYLOAD;
894  buffer[3] = out_seq; /* add an outgoing seq no before fwd */
895  uip_icmp6_send(rpl_parent_get_ipaddr(dag->preferred_parent),
896  ICMP6_RPL, RPL_CODE_DAO, buffer_length);
897  }
898  if(should_ack) {
899  LOG_DBG("Sending DAO ACK\n");
900  uipbuf_clear();
901  dao_ack_output(instance, &dao_sender_addr, sequence,
902  RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
903  }
904  }
905 #endif /* RPL_WITH_STORING */
906 }
907 /*---------------------------------------------------------------------------*/
908 static void
909 dao_input_nonstoring(void)
910 {
911 #if RPL_WITH_NON_STORING
912  uip_ipaddr_t dao_sender_addr;
913  uip_ipaddr_t dao_parent_addr;
914  rpl_dag_t *dag;
915  rpl_instance_t *instance;
916  unsigned char *buffer;
917  uint16_t sequence;
918  uint8_t instance_id;
919  uint8_t lifetime;
920  uint8_t prefixlen;
921  uint8_t flags;
922  uint8_t subopt_type;
923  uip_ipaddr_t prefix;
924  uint8_t buffer_length;
925  int pos;
926  int len;
927  int i;
928 
929  /* Destination Advertisement Object */
930  LOG_INFO("Received a DAO from ");
931  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
932  LOG_INFO_("\n");
933 
934  prefixlen = 0;
935 
936  uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
937  memset(&dao_parent_addr, 0, 16);
938 
939  buffer = UIP_ICMP_PAYLOAD;
940  buffer_length = uip_len - uip_l3_icmp_hdr_len;
941 
942  pos = 0;
943  instance_id = buffer[pos++];
944  instance = rpl_get_instance(instance_id);
945  lifetime = instance->default_lifetime;
946 
947  flags = buffer[pos++];
948  /* reserved */
949  pos++;
950  sequence = buffer[pos++];
951 
952  dag = instance->current_dag;
953  /* Is the DAG ID present? */
954  if(flags & RPL_DAO_D_FLAG) {
955  if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
956  LOG_INFO("Ignoring a DAO for a DAG different from ours\n");
957  return;
958  }
959  pos += 16;
960  }
961 
962  /* Check if there are any RPL options present. */
963  for(i = pos; i < buffer_length; i += len) {
964  subopt_type = buffer[i];
965  if(subopt_type == RPL_OPTION_PAD1) {
966  len = 1;
967  } else {
968  /* The option consists of a two-byte header and a payload. */
969  len = 2 + buffer[i + 1];
970  }
971 
972  switch(subopt_type) {
973  case RPL_OPTION_TARGET:
974  /* Handle the target option. */
975  prefixlen = buffer[i + 3];
976  memset(&prefix, 0, sizeof(prefix));
977  memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
978  break;
979  case RPL_OPTION_TRANSIT:
980  /* The path sequence and control are ignored. */
981  /* pathcontrol = buffer[i + 3];
982  pathsequence = buffer[i + 4];*/
983  lifetime = buffer[i + 5];
984  if(len >= 20) {
985  memcpy(&dao_parent_addr, buffer + i + 6, 16);
986  }
987  break;
988  }
989  }
990 
991  LOG_INFO("DAO lifetime: %u, prefix length: %u prefix: ",
992  (unsigned)lifetime, (unsigned)prefixlen);
993  LOG_INFO_6ADDR(&prefix);
994  LOG_INFO_(", parent: ");
995  LOG_INFO_6ADDR(&dao_parent_addr);
996  LOG_INFO_("\n");
997 
998  if(lifetime == RPL_ZERO_LIFETIME) {
999  LOG_DBG("No-Path DAO received\n");
1000  uip_sr_expire_parent(dag, &prefix, &dao_parent_addr);
1001  } else {
1002  if(uip_sr_update_node(dag, &prefix, &dao_parent_addr, RPL_LIFETIME(instance, lifetime)) == NULL) {
1003  LOG_WARN("DAO failed to add link prefix: ");
1004  LOG_WARN_6ADDR(&prefix);
1005  LOG_WARN_(", parent: ");
1006  LOG_WARN_6ADDR(&dao_parent_addr);
1007  LOG_WARN_("\n");
1008  return;
1009  }
1010  }
1011 
1012  if(flags & RPL_DAO_K_FLAG) {
1013  LOG_DBG("Sending DAO ACK\n");
1014  uipbuf_clear();
1015  dao_ack_output(instance, &dao_sender_addr, sequence,
1016  RPL_DAO_ACK_UNCONDITIONAL_ACCEPT);
1017  }
1018 #endif /* RPL_WITH_NON_STORING */
1019 }
1020 /*---------------------------------------------------------------------------*/
1021 static void
1022 dao_input(void)
1023 {
1024  rpl_instance_t *instance;
1025  uint8_t instance_id;
1026 
1027  /* Destination Advertisement Object */
1028  LOG_INFO("Received a DAO from ");
1029  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
1030  LOG_INFO_("\n");
1031 
1032  instance_id = UIP_ICMP_PAYLOAD[0];
1033  instance = rpl_get_instance(instance_id);
1034  if(instance == NULL) {
1035  LOG_INFO("Ignoring a DAO for an unknown RPL instance(%u)\n",
1036  instance_id);
1037  goto discard;
1038  }
1039 
1040  if(RPL_IS_STORING(instance)) {
1041  dao_input_storing();
1042  } else if(RPL_IS_NON_STORING(instance)) {
1043  dao_input_nonstoring();
1044  }
1045 
1046 discard:
1047  uipbuf_clear();
1048 }
1049 /*---------------------------------------------------------------------------*/
1050 #if RPL_WITH_DAO_ACK
1051 static void
1052 handle_dao_retransmission(void *ptr)
1053 {
1054  rpl_parent_t *parent;
1055  uip_ipaddr_t prefix;
1056  rpl_instance_t *instance;
1057 
1058  parent = ptr;
1059  if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1060  return;
1061  }
1062  instance = parent->dag->instance;
1063 
1064  if(instance->my_dao_transmissions >= RPL_DAO_MAX_RETRANSMISSIONS) {
1065  /* No more retransmissions - give up. */
1066  if(instance->lifetime_unit == 0xffff && instance->default_lifetime == 0xff) {
1067  /*
1068  * ContikiRPL was previously using infinite lifetime for routes
1069  * and no DAO_ACK configured. This probably means that the root
1070  * and possibly other nodes might be running an old version that
1071  * does not support DAO ack. Assume that everything is ok for
1072  * now and let the normal repair mechanisms detect any problems.
1073  */
1074  return;
1075  }
1076 
1077  if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1078  /* Inform the objective function about the timeout. */
1079  instance->of->dao_ack_callback(parent, RPL_DAO_ACK_TIMEOUT);
1080  }
1081 
1082  /* Perform local repair and hope to find another parent. */
1083  rpl_local_repair(instance);
1084  return;
1085  }
1086 
1087  LOG_INFO("will retransmit DAO - seq:%d trans:%d\n", instance->my_dao_seqno,
1088  instance->my_dao_transmissions);
1089 
1090  if(get_global_addr(&prefix) == 0) {
1091  return;
1092  }
1093 
1094  ctimer_set(&instance->dao_retransmit_timer,
1095  RPL_DAO_RETRANSMISSION_TIMEOUT / 2 +
1096  (random_rand() % (RPL_DAO_RETRANSMISSION_TIMEOUT / 2)),
1097  handle_dao_retransmission, parent);
1098 
1099  instance->my_dao_transmissions++;
1100  dao_output_target_seq(parent, &prefix,
1101  instance->default_lifetime, instance->my_dao_seqno);
1102 }
1103 #endif /* RPL_WITH_DAO_ACK */
1104 /*---------------------------------------------------------------------------*/
1105 void
1106 dao_output(rpl_parent_t *parent, uint8_t lifetime)
1107 {
1108  /* Destination Advertisement Object */
1109  uip_ipaddr_t prefix;
1110 
1111  if(get_global_addr(&prefix) == 0) {
1112  LOG_ERR("No global address set for this node - suppressing DAO\n");
1113  return;
1114  }
1115 
1116  if(parent == NULL || parent->dag == NULL || parent->dag->instance == NULL) {
1117  return;
1118  }
1119 
1120  RPL_LOLLIPOP_INCREMENT(dao_sequence);
1121 #if RPL_WITH_DAO_ACK
1122  /* set up the state since this will be the first transmission of DAO */
1123  /* retransmissions will call directly to dao_output_target_seq */
1124  /* keep track of my own sending of DAO for handling ack and loss of ack */
1125  if(lifetime != RPL_ZERO_LIFETIME) {
1126  rpl_instance_t *instance;
1127  instance = parent->dag->instance;
1128 
1129  instance->my_dao_seqno = dao_sequence;
1130  instance->my_dao_transmissions = 1;
1131  ctimer_set(&instance->dao_retransmit_timer, RPL_DAO_RETRANSMISSION_TIMEOUT,
1132  handle_dao_retransmission, parent);
1133  }
1134 #else
1135  /* We know that we have tried to register so now we are assuming
1136  that we have a down-link - unless this is a zero lifetime one */
1137  parent->dag->instance->has_downward_route = lifetime != RPL_ZERO_LIFETIME;
1138 #endif /* RPL_WITH_DAO_ACK */
1139 
1140  /* Sending a DAO with own prefix as target */
1141  dao_output_target(parent, &prefix, lifetime);
1142 }
1143 /*---------------------------------------------------------------------------*/
1144 void
1145 dao_output_target(rpl_parent_t *parent, uip_ipaddr_t *prefix, uint8_t lifetime)
1146 {
1147  dao_output_target_seq(parent, prefix, lifetime, dao_sequence);
1148 }
1149 /*---------------------------------------------------------------------------*/
1150 static void
1151 dao_output_target_seq(rpl_parent_t *parent, uip_ipaddr_t *prefix,
1152  uint8_t lifetime, uint8_t seq_no)
1153 {
1154  rpl_dag_t *dag;
1155  rpl_instance_t *instance;
1156  unsigned char *buffer;
1157  uint8_t prefixlen;
1158  int pos;
1159  uip_ipaddr_t *parent_ipaddr = NULL;
1160  uip_ipaddr_t *dest_ipaddr = NULL;
1161 
1162  /* Destination Advertisement Object */
1163 
1164  /* If we are in feather mode, we should not send any DAOs */
1165  if(rpl_get_mode() == RPL_MODE_FEATHER) {
1166  return;
1167  }
1168 
1169  if(parent == NULL) {
1170  LOG_ERR("dao_output_target error parent NULL\n");
1171  return;
1172  }
1173 
1174  parent_ipaddr = rpl_parent_get_ipaddr(parent);
1175  if(parent_ipaddr == NULL) {
1176  LOG_ERR("dao_output_target error parent IP address NULL\n");
1177  return;
1178  }
1179 
1180  dag = parent->dag;
1181  if(dag == NULL) {
1182  LOG_ERR("dao_output_target error dag NULL\n");
1183  return;
1184  }
1185 
1186  instance = dag->instance;
1187 
1188  if(instance == NULL) {
1189  LOG_ERR("dao_output_target error instance NULL\n");
1190  return;
1191  }
1192  if(prefix == NULL) {
1193  LOG_ERR("dao_output_target error prefix NULL\n");
1194  return;
1195  }
1196 #ifdef RPL_DEBUG_DAO_OUTPUT
1197  RPL_DEBUG_DAO_OUTPUT(parent);
1198 #endif
1199 
1200  buffer = UIP_ICMP_PAYLOAD;
1201  pos = 0;
1202 
1203  buffer[pos++] = instance->instance_id;
1204  buffer[pos] = 0;
1205 #if RPL_DAO_SPECIFY_DAG
1206  buffer[pos] |= RPL_DAO_D_FLAG;
1207 #endif /* RPL_DAO_SPECIFY_DAG */
1208 #if RPL_WITH_DAO_ACK
1209  if(lifetime != RPL_ZERO_LIFETIME) {
1210  buffer[pos] |= RPL_DAO_K_FLAG;
1211  }
1212 #endif /* RPL_WITH_DAO_ACK */
1213  ++pos;
1214  buffer[pos++] = 0; /* reserved */
1215  buffer[pos++] = seq_no;
1216 #if RPL_DAO_SPECIFY_DAG
1217  memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
1218  pos+=sizeof(dag->dag_id);
1219 #endif /* RPL_DAO_SPECIFY_DAG */
1220 
1221  /* create target subopt */
1222  prefixlen = sizeof(*prefix) * CHAR_BIT;
1223  buffer[pos++] = RPL_OPTION_TARGET;
1224  buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
1225  buffer[pos++] = 0; /* reserved */
1226  buffer[pos++] = prefixlen;
1227  memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
1228  pos += ((prefixlen + 7) / CHAR_BIT);
1229 
1230  /* Create a transit information sub-option. */
1231  buffer[pos++] = RPL_OPTION_TRANSIT;
1232  buffer[pos++] = (instance->mop != RPL_MOP_NON_STORING) ? 4 : 20;
1233  buffer[pos++] = 0; /* flags - ignored */
1234  buffer[pos++] = 0; /* path control - ignored */
1235  buffer[pos++] = 0; /* path seq - ignored */
1236  buffer[pos++] = lifetime;
1237 
1238  if(instance->mop != RPL_MOP_NON_STORING) {
1239  /* Send DAO to parent */
1240  dest_ipaddr = parent_ipaddr;
1241  } else {
1242  /* Include parent global IP address */
1243  memcpy(buffer + pos, &parent->dag->dag_id, 8); /* Prefix */
1244  pos += 8;
1245  memcpy(buffer + pos, ((const unsigned char *)parent_ipaddr) + 8, 8); /* Interface identifier */
1246  pos += 8;
1247  /* Send DAO to root */
1248  dest_ipaddr = &parent->dag->dag_id;
1249  }
1250 
1251  LOG_INFO("Sending a %sDAO with sequence number %u, lifetime %u, prefix ",
1252  lifetime == RPL_ZERO_LIFETIME ? "No-Path " : "", seq_no, lifetime);
1253 
1254  LOG_INFO_6ADDR(prefix);
1255  LOG_INFO_(" to ");
1256  LOG_INFO_6ADDR(dest_ipaddr);
1257  LOG_INFO_(" , parent ");
1258  LOG_INFO_6ADDR(parent_ipaddr);
1259  LOG_INFO_("\n");
1260 
1261  if(dest_ipaddr != NULL) {
1262  uip_icmp6_send(dest_ipaddr, ICMP6_RPL, RPL_CODE_DAO, pos);
1263  }
1264 }
1265 /*---------------------------------------------------------------------------*/
1266 static void
1267 dao_ack_input(void)
1268 {
1269 #if RPL_WITH_DAO_ACK
1270 
1271  uint8_t *buffer;
1272  uint8_t instance_id;
1273  uint8_t sequence;
1274  uint8_t status;
1275  rpl_instance_t *instance;
1276  rpl_parent_t *parent;
1277 
1278  buffer = UIP_ICMP_PAYLOAD;
1279 
1280  instance_id = buffer[0];
1281  sequence = buffer[2];
1282  status = buffer[3];
1283 
1284  instance = rpl_get_instance(instance_id);
1285  if(instance == NULL) {
1286  uipbuf_clear();
1287  return;
1288  }
1289 
1290  if(RPL_IS_STORING(instance)) {
1291  parent = rpl_find_parent(instance->current_dag, &UIP_IP_BUF->srcipaddr);
1292  if(parent == NULL) {
1293  /* not a known instance - drop the packet and ignore */
1294  uipbuf_clear();
1295  return;
1296  }
1297  } else {
1298  parent = NULL;
1299  }
1300 
1301  if(instance->current_dag->rank == ROOT_RANK(instance)) {
1302  LOG_DBG("DODAG root received a DAO ACK, ignoring it\n");
1303  uipbuf_clear();
1304  return;
1305  }
1306 
1307  LOG_INFO("Received a DAO %s with sequence number %d (%d) and status %d from ",
1308  status < 128 ? "ACK" : "NACK",
1309  sequence, instance->my_dao_seqno, status);
1310  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
1311  LOG_INFO_("\n");
1312 
1313  if(sequence == instance->my_dao_seqno) {
1314  instance->has_downward_route = status < 128;
1315 
1316  /* always stop the retransmit timer when the ACK arrived */
1317  ctimer_stop(&instance->dao_retransmit_timer);
1318 
1319  /* Inform objective function on status of the DAO ACK */
1320  if(RPL_IS_STORING(instance) && instance->of->dao_ack_callback) {
1321  instance->of->dao_ack_callback(parent, status);
1322  }
1323 
1324 #if RPL_REPAIR_ON_DAO_NACK
1325  if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1326  /*
1327  * Failed the DAO transmission - need to remove the default route.
1328  * Trigger a local repair since we can not get our DAO in.
1329  */
1330  rpl_local_repair(instance);
1331  }
1332 #endif
1333 
1334  } else if(RPL_IS_STORING(instance)) {
1335  /* this DAO ACK should be forwarded to another recently registered route */
1336  uip_ds6_route_t *re;
1337  uip_ipaddr_t *nexthop;
1338  if((re = find_route_entry_by_dao_ack(sequence)) != NULL) {
1339  /* pick the recorded seq no from that node and forward DAO ACK - and
1340  clear the pending flag*/
1341  RPL_ROUTE_CLEAR_DAO_PENDING(re);
1342 
1343  nexthop = uip_ds6_route_nexthop(re);
1344  if(nexthop == NULL) {
1345  LOG_WARN("No next hop to fwd DAO ACK to\n");
1346  } else {
1347  LOG_INFO("Fwd DAO ACK to:");
1348  LOG_INFO_6ADDR(nexthop);
1349  LOG_INFO_("\n");
1350  buffer[2] = re->state.dao_seqno_in;
1351  uip_icmp6_send(nexthop, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
1352  }
1353 
1354  if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
1355  /* this node did not get in to the routing tables above... - remove */
1356  uip_ds6_route_rm(re);
1357  }
1358  } else {
1359  LOG_WARN("No route entry found to forward DAO ACK (seqno %u)\n", sequence);
1360  }
1361  }
1362 #endif /* RPL_WITH_DAO_ACK */
1363  uipbuf_clear();
1364 }
1365 /*---------------------------------------------------------------------------*/
1366 void
1367 dao_ack_output(rpl_instance_t *instance, uip_ipaddr_t *dest, uint8_t sequence,
1368  uint8_t status)
1369 {
1370 #if RPL_WITH_DAO_ACK
1371  unsigned char *buffer;
1372 
1373  LOG_INFO("Sending a DAO %s with sequence number %d to ", status < 128 ? "ACK" : "NACK", sequence);
1374  LOG_INFO_6ADDR(dest);
1375  LOG_INFO_(" with status %d\n", status);
1376 
1377  buffer = UIP_ICMP_PAYLOAD;
1378 
1379  buffer[0] = instance->instance_id;
1380  buffer[1] = 0;
1381  buffer[2] = sequence;
1382  buffer[3] = status;
1383 
1384  uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
1385 #endif /* RPL_WITH_DAO_ACK */
1386 }
1387 /*---------------------------------------------------------------------------*/
1388 void
1389 rpl_icmp6_register_handlers()
1390 {
1391  uip_icmp6_register_input_handler(&dis_handler);
1392  uip_icmp6_register_input_handler(&dio_handler);
1393  uip_icmp6_register_input_handler(&dao_handler);
1394  uip_icmp6_register_input_handler(&dao_ack_handler);
1395 }
1396 /*---------------------------------------------------------------------------*/
1397 
1398 /** @}*/
#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:1055
#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:1459
#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: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: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