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