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