Contiki-Inga 3.x
rpl-icmp6.c
Go to the documentation of this file.
1 /**
2  * \addtogroup uip6
3  * @{
4  */
5 /*
6  * Copyright (c) 2010, Swedish Institute of Computer Science.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the Institute nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * This file is part of the Contiki operating system.
34  *
35  */
36 /**
37  * \file
38  * ICMP6 I/O for RPL control messages.
39  *
40  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
41  * Contributors: Niclas Finne <nfi@sics.se>, Joel Hoglund <joel@sics.se>,
42  * Mathieu Pouillot <m.pouillot@watteco.com>
43  * George Oikonomou <oikonomou@users.sourceforge.net> (multicast)
44  */
45 
46 #include "net/ip/tcpip.h"
47 #include "net/ip/uip.h"
48 #include "net/ipv6/uip-ds6.h"
49 #include "net/ipv6/uip-nd6.h"
50 #include "net/ipv6/uip-icmp6.h"
51 #include "net/rpl/rpl-private.h"
52 #include "net/packetbuf.h"
54 
55 #include <limits.h>
56 #include <string.h>
57 
58 #define DEBUG DEBUG_NONE
59 
60 #include "net/ip/uip-debug.h"
61 
62 #if UIP_CONF_IPV6
63 /*---------------------------------------------------------------------------*/
64 #define RPL_DIO_GROUNDED 0x80
65 #define RPL_DIO_MOP_SHIFT 3
66 #define RPL_DIO_MOP_MASK 0x3c
67 #define RPL_DIO_PREFERENCE_MASK 0x07
68 
69 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
70 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
71 #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len])
72 /*---------------------------------------------------------------------------*/
73 static void dis_input(void);
74 static void dio_input(void);
75 static void dao_input(void);
76 static void dao_ack_input(void);
77 
78 /* some debug callbacks useful when debugging RPL networks */
79 #ifdef RPL_DEBUG_DIO_INPUT
80 void RPL_DEBUG_DIO_INPUT(uip_ipaddr_t *, rpl_dio_t *);
81 #endif
82 
83 #ifdef RPL_DEBUG_DAO_OUTPUT
84 void RPL_DEBUG_DAO_OUTPUT(rpl_parent_t *);
85 #endif
86 
87 static uint8_t dao_sequence = RPL_LOLLIPOP_INIT;
88 
89 extern rpl_of_t RPL_OF;
90 
91 #if RPL_CONF_MULTICAST
92 static uip_mcast6_route_t *mcast_group;
93 #endif
94 /*---------------------------------------------------------------------------*/
95 static int
96 get_global_addr(uip_ipaddr_t *addr)
97 {
98  int i;
99  int state;
100 
101  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
102  state = uip_ds6_if.addr_list[i].state;
103  if(uip_ds6_if.addr_list[i].isused &&
104  (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
105  if(!uip_is_addr_link_local(&uip_ds6_if.addr_list[i].ipaddr)) {
106  memcpy(addr, &uip_ds6_if.addr_list[i].ipaddr, sizeof(uip_ipaddr_t));
107  return 1;
108  }
109  }
110  }
111  return 0;
112 }
113 /*---------------------------------------------------------------------------*/
114 static uint32_t
115 get32(uint8_t *buffer, int pos)
116 {
117  return (uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
118  (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3];
119 }
120 /*---------------------------------------------------------------------------*/
121 static void
122 set32(uint8_t *buffer, int pos, uint32_t value)
123 {
124  buffer[pos++] = value >> 24;
125  buffer[pos++] = (value >> 16) & 0xff;
126  buffer[pos++] = (value >> 8) & 0xff;
127  buffer[pos++] = value & 0xff;
128 }
129 /*---------------------------------------------------------------------------*/
130 static uint16_t
131 get16(uint8_t *buffer, int pos)
132 {
133  return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
134 }
135 /*---------------------------------------------------------------------------*/
136 static void
137 set16(uint8_t *buffer, int pos, uint16_t value)
138 {
139  buffer[pos++] = value >> 8;
140  buffer[pos++] = value & 0xff;
141 }
142 /*---------------------------------------------------------------------------*/
143 static void
144 dis_input(void)
145 {
146  rpl_instance_t *instance;
147  rpl_instance_t *end;
148 
149  /* DAG Information Solicitation */
150  PRINTF("RPL: Received a DIS from ");
151  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
152  PRINTF("\n");
153 
154  for(instance = &instance_table[0], end = instance + RPL_MAX_INSTANCES;
155  instance < end; ++instance) {
156  if(instance->used == 1) {
157 #if RPL_LEAF_ONLY
158  if(!uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
159  PRINTF("RPL: LEAF ONLY Multicast DIS will NOT reset DIO timer\n");
160 #else /* !RPL_LEAF_ONLY */
161  if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
162  PRINTF("RPL: Multicast DIS => reset DIO timer\n");
163  rpl_reset_dio_timer(instance);
164  } else {
165 #endif /* !RPL_LEAF_ONLY */
166  PRINTF("RPL: Unicast DIS, reply to sender\n");
167  dio_output(instance, &UIP_IP_BUF->srcipaddr);
168  }
169  }
170  }
171 }
172 /*---------------------------------------------------------------------------*/
173 void
174 dis_output(uip_ipaddr_t *addr)
175 {
176  unsigned char *buffer;
177  uip_ipaddr_t tmpaddr;
178 
179  /*
180  * DAG Information Solicitation - 2 bytes reserved
181  * 0 1 2
182  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
183  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
184  * | Flags | Reserved | Option(s)...
185  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
186  */
187 
188  buffer = UIP_ICMP_PAYLOAD;
189  buffer[0] = buffer[1] = 0;
190 
191  if(addr == NULL) {
192  uip_create_linklocal_rplnodes_mcast(&tmpaddr);
193  addr = &tmpaddr;
194  }
195 
196  PRINTF("RPL: Sending a DIS to ");
197  PRINT6ADDR(addr);
198  PRINTF("\n");
199 
200  uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 2);
201 }
202 /*---------------------------------------------------------------------------*/
203 static void
204 dio_input(void)
205 {
206  unsigned char *buffer;
207  uint8_t buffer_length;
208  rpl_dio_t dio;
209  uint8_t subopt_type;
210  int i;
211  int len;
212  uip_ipaddr_t from;
213  uip_ds6_nbr_t *nbr;
214 
215  memset(&dio, 0, sizeof(dio));
216 
217  /* Set default values in case the DIO configuration option is missing. */
218  dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
219  dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
220  dio.dag_redund = RPL_DIO_REDUNDANCY;
221  dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
222  dio.dag_max_rankinc = RPL_MAX_RANKINC;
223  dio.ocp = RPL_OF.ocp;
224  dio.default_lifetime = RPL_DEFAULT_LIFETIME;
225  dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
226 
227  uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
228 
229  /* DAG Information Object */
230  PRINTF("RPL: Received a DIO from ");
231  PRINT6ADDR(&from);
232  PRINTF("\n");
233 
234  if((nbr = uip_ds6_nbr_lookup(&from)) == NULL) {
235  if((nbr = uip_ds6_nbr_add(&from, (uip_lladdr_t *)
236  packetbuf_addr(PACKETBUF_ADDR_SENDER),
237  0, NBR_REACHABLE)) != NULL) {
238  /* set reachable timer */
239  stimer_set(&nbr->reachable, UIP_ND6_REACHABLE_TIME / 1000);
240  PRINTF("RPL: Neighbor added to neighbor cache ");
241  PRINT6ADDR(&from);
242  PRINTF(", ");
243  PRINTLLADDR((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
244  PRINTF("\n");
245  } else {
246  PRINTF("RPL: Out of memory, dropping DIO from ");
247  PRINT6ADDR(&from);
248  PRINTF(", ");
249  PRINTLLADDR((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
250  PRINTF("\n");
251  return;
252  }
253  } else {
254  PRINTF("RPL: Neighbor already in neighbor cache\n");
255  }
256 
257  buffer_length = uip_len - uip_l3_icmp_hdr_len;
258 
259  /* Process the DIO base option. */
260  i = 0;
261  buffer = UIP_ICMP_PAYLOAD;
262 
263  dio.instance_id = buffer[i++];
264  dio.version = buffer[i++];
265  dio.rank = get16(buffer, i);
266  i += 2;
267 
268  PRINTF("RPL: Incoming DIO (id, ver, rank) = (%u,%u,%u)\n",
269  (unsigned)dio.instance_id,
270  (unsigned)dio.version,
271  (unsigned)dio.rank);
272 
273  dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
274  dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
275  dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
276 
277  dio.dtsn = buffer[i++];
278  /* two reserved bytes */
279  i += 2;
280 
281  memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id));
282  i += sizeof(dio.dag_id);
283 
284  PRINTF("RPL: Incoming DIO (dag_id, pref) = (");
285  PRINT6ADDR(&dio.dag_id);
286  PRINTF(", %u)\n", dio.preference);
287 
288  /* Check if there are any DIO suboptions. */
289  for(; i < buffer_length; i += len) {
290  subopt_type = buffer[i];
291  if(subopt_type == RPL_OPTION_PAD1) {
292  len = 1;
293  } else {
294  /* Suboption with a two-byte header + payload */
295  len = 2 + buffer[i + 1];
296  }
297 
298  if(len + i > buffer_length) {
299  PRINTF("RPL: Invalid DIO packet\n");
300  RPL_STAT(rpl_stats.malformed_msgs++);
301  return;
302  }
303 
304  PRINTF("RPL: DIO option %u, length: %u\n", subopt_type, len - 2);
305 
306  switch(subopt_type) {
307  case RPL_OPTION_DAG_METRIC_CONTAINER:
308  if(len < 6) {
309  PRINTF("RPL: Invalid DAG MC, len = %d\n", len);
310  RPL_STAT(rpl_stats.malformed_msgs++);
311  return;
312  }
313  dio.mc.type = buffer[i + 2];
314  dio.mc.flags = buffer[i + 3] << 1;
315  dio.mc.flags |= buffer[i + 4] >> 7;
316  dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
317  dio.mc.prec = buffer[i + 4] & 0xf;
318  dio.mc.length = buffer[i + 5];
319 
320  if(dio.mc.type == RPL_DAG_MC_NONE) {
321  /* No metric container: do nothing */
322  } else if(dio.mc.type == RPL_DAG_MC_ETX) {
323  dio.mc.obj.etx = get16(buffer, i + 6);
324 
325  PRINTF("RPL: DAG MC: type %u, flags %u, aggr %u, prec %u, length %u, ETX %u\n",
326  (unsigned)dio.mc.type,
327  (unsigned)dio.mc.flags,
328  (unsigned)dio.mc.aggr,
329  (unsigned)dio.mc.prec,
330  (unsigned)dio.mc.length,
331  (unsigned)dio.mc.obj.etx);
332  } else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
333  dio.mc.obj.energy.flags = buffer[i + 6];
334  dio.mc.obj.energy.energy_est = buffer[i + 7];
335  } else {
336  PRINTF("RPL: Unhandled DAG MC type: %u\n", (unsigned)dio.mc.type);
337  return;
338  }
339  break;
340  case RPL_OPTION_ROUTE_INFO:
341  if(len < 9) {
342  PRINTF("RPL: Invalid destination prefix option, len = %d\n", len);
343  RPL_STAT(rpl_stats.malformed_msgs++);
344  return;
345  }
346 
347  /* The flags field includes the preference value. */
348  dio.destination_prefix.length = buffer[i + 2];
349  dio.destination_prefix.flags = buffer[i + 3];
350  dio.destination_prefix.lifetime = get32(buffer, i + 4);
351 
352  if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
353  dio.destination_prefix.length <= 128) {
354  PRINTF("RPL: Copying destination prefix\n");
355  memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
356  (dio.destination_prefix.length + 7) / 8);
357  } else {
358  PRINTF("RPL: Invalid route info option, len = %d\n", len);
359  RPL_STAT(rpl_stats.malformed_msgs++);
360  return;
361  }
362 
363  break;
364  case RPL_OPTION_DAG_CONF:
365  if(len != 16) {
366  PRINTF("RPL: Invalid DAG configuration option, len = %d\n", len);
367  RPL_STAT(rpl_stats.malformed_msgs++);
368  return;
369  }
370 
371  /* Path control field not yet implemented - at i + 2 */
372  dio.dag_intdoubl = buffer[i + 3];
373  dio.dag_intmin = buffer[i + 4];
374  dio.dag_redund = buffer[i + 5];
375  dio.dag_max_rankinc = get16(buffer, i + 6);
376  dio.dag_min_hoprankinc = get16(buffer, i + 8);
377  dio.ocp = get16(buffer, i + 10);
378  /* buffer + 12 is reserved */
379  dio.default_lifetime = buffer[i + 13];
380  dio.lifetime_unit = get16(buffer, i + 14);
381  PRINTF("RPL: DAG conf:dbl=%d, min=%d red=%d maxinc=%d mininc=%d ocp=%d d_l=%u l_u=%u\n",
382  dio.dag_intdoubl, dio.dag_intmin, dio.dag_redund,
383  dio.dag_max_rankinc, dio.dag_min_hoprankinc, dio.ocp,
384  dio.default_lifetime, dio.lifetime_unit);
385  break;
386  case RPL_OPTION_PREFIX_INFO:
387  if(len != 32) {
388  PRINTF("RPL: Invalid DAG prefix info, len != 32\n");
389  RPL_STAT(rpl_stats.malformed_msgs++);
390  return;
391  }
392  dio.prefix_info.length = buffer[i + 2];
393  dio.prefix_info.flags = buffer[i + 3];
394  /* valid lifetime is ingnored for now - at i + 4 */
395  /* preferred lifetime stored in lifetime */
396  dio.prefix_info.lifetime = get32(buffer, i + 8);
397  /* 32-bit reserved at i + 12 */
398  PRINTF("RPL: Copying prefix information\n");
399  memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
400  break;
401  default:
402  PRINTF("RPL: Unsupported suboption type in DIO: %u\n",
403  (unsigned)subopt_type);
404  }
405  }
406 
407 #ifdef RPL_DEBUG_DIO_INPUT
408  RPL_DEBUG_DIO_INPUT(&from, &dio);
409 #endif
410 
411  rpl_process_dio(&from, &dio);
412 }
413 /*---------------------------------------------------------------------------*/
414 void
415 dio_output(rpl_instance_t *instance, uip_ipaddr_t *uc_addr)
416 {
417  unsigned char *buffer;
418  int pos;
419  rpl_dag_t *dag = instance->current_dag;
420 #if !RPL_LEAF_ONLY
421  uip_ipaddr_t addr;
422 #endif /* !RPL_LEAF_ONLY */
423 
424 #if RPL_LEAF_ONLY
425  /* In leaf mode, we only send DIO messages as unicasts in response to
426  unicast DIS messages. */
427  if(uc_addr == NULL) {
428  PRINTF("RPL: LEAF ONLY have multicast addr: skip dio_output\n");
429  return;
430  }
431 #endif /* RPL_LEAF_ONLY */
432 
433  /* DAG Information Object */
434  pos = 0;
435 
436  buffer = UIP_ICMP_PAYLOAD;
437  buffer[pos++] = instance->instance_id;
438  buffer[pos++] = dag->version;
439 
440 #if RPL_LEAF_ONLY
441  PRINTF("RPL: LEAF ONLY DIO rank set to INFINITE_RANK\n");
442  set16(buffer, pos, INFINITE_RANK);
443 #else /* RPL_LEAF_ONLY */
444  set16(buffer, pos, dag->rank);
445 #endif /* RPL_LEAF_ONLY */
446  pos += 2;
447 
448  buffer[pos] = 0;
449  if(dag->grounded) {
450  buffer[pos] |= RPL_DIO_GROUNDED;
451  }
452 
453  buffer[pos] |= instance->mop << RPL_DIO_MOP_SHIFT;
454  buffer[pos] |= dag->preference & RPL_DIO_PREFERENCE_MASK;
455  pos++;
456 
457  buffer[pos++] = instance->dtsn_out;
458 
459  /* always request new DAO to refresh route */
460  RPL_LOLLIPOP_INCREMENT(instance->dtsn_out);
461 
462  /* reserved 2 bytes */
463  buffer[pos++] = 0; /* flags */
464  buffer[pos++] = 0; /* reserved */
465 
466  memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
467  pos += 16;
468 
469 #if !RPL_LEAF_ONLY
470  if(instance->mc.type != RPL_DAG_MC_NONE) {
471  instance->of->update_metric_container(instance);
472 
473  buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
474  buffer[pos++] = 6;
475  buffer[pos++] = instance->mc.type;
476  buffer[pos++] = instance->mc.flags >> 1;
477  buffer[pos] = (instance->mc.flags & 1) << 7;
478  buffer[pos++] |= (instance->mc.aggr << 4) | instance->mc.prec;
479  if(instance->mc.type == RPL_DAG_MC_ETX) {
480  buffer[pos++] = 2;
481  set16(buffer, pos, instance->mc.obj.etx);
482  pos += 2;
483  } else if(instance->mc.type == RPL_DAG_MC_ENERGY) {
484  buffer[pos++] = 2;
485  buffer[pos++] = instance->mc.obj.energy.flags;
486  buffer[pos++] = instance->mc.obj.energy.energy_est;
487  } else {
488  PRINTF("RPL: Unable to send DIO because of unhandled DAG MC type %u\n",
489  (unsigned)instance->mc.type);
490  return;
491  }
492  }
493 #endif /* !RPL_LEAF_ONLY */
494 
495  /* Always add a DAG configuration option. */
496  buffer[pos++] = RPL_OPTION_DAG_CONF;
497  buffer[pos++] = 14;
498  buffer[pos++] = 0; /* No Auth, PCS = 0 */
499  buffer[pos++] = instance->dio_intdoubl;
500  buffer[pos++] = instance->dio_intmin;
501  buffer[pos++] = instance->dio_redundancy;
502  set16(buffer, pos, instance->max_rankinc);
503  pos += 2;
504  set16(buffer, pos, instance->min_hoprankinc);
505  pos += 2;
506  /* OCP is in the DAG_CONF option */
507  set16(buffer, pos, instance->of->ocp);
508  pos += 2;
509  buffer[pos++] = 0; /* reserved */
510  buffer[pos++] = instance->default_lifetime;
511  set16(buffer, pos, instance->lifetime_unit);
512  pos += 2;
513 
514  /* Check if we have a prefix to send also. */
515  if(dag->prefix_info.length > 0) {
516  buffer[pos++] = RPL_OPTION_PREFIX_INFO;
517  buffer[pos++] = 30; /* always 30 bytes + 2 long */
518  buffer[pos++] = dag->prefix_info.length;
519  buffer[pos++] = dag->prefix_info.flags;
520  set32(buffer, pos, dag->prefix_info.lifetime);
521  pos += 4;
522  set32(buffer, pos, dag->prefix_info.lifetime);
523  pos += 4;
524  memset(&buffer[pos], 0, 4);
525  pos += 4;
526  memcpy(&buffer[pos], &dag->prefix_info.prefix, 16);
527  pos += 16;
528  PRINTF("RPL: Sending prefix info in DIO for ");
529  PRINT6ADDR(&dag->prefix_info.prefix);
530  PRINTF("\n");
531  } else {
532  PRINTF("RPL: No prefix to announce (len %d)\n",
533  dag->prefix_info.length);
534  }
535 
536 #if RPL_LEAF_ONLY
537 #if (DEBUG) & DEBUG_PRINT
538  if(uc_addr == NULL) {
539  PRINTF("RPL: LEAF ONLY sending unicast-DIO from multicast-DIO\n");
540  }
541 #endif /* DEBUG_PRINT */
542  PRINTF("RPL: Sending unicast-DIO with rank %u to ",
543  (unsigned)dag->rank);
544  PRINT6ADDR(uc_addr);
545  PRINTF("\n");
546  uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
547 #else /* RPL_LEAF_ONLY */
548  /* Unicast requests get unicast replies! */
549  if(uc_addr == NULL) {
550  PRINTF("RPL: Sending a multicast-DIO with rank %u\n",
551  (unsigned)instance->current_dag->rank);
552  uip_create_linklocal_rplnodes_mcast(&addr);
553  uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DIO, pos);
554  } else {
555  PRINTF("RPL: Sending unicast-DIO with rank %u to ",
556  (unsigned)instance->current_dag->rank);
557  PRINT6ADDR(uc_addr);
558  PRINTF("\n");
559  uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
560  }
561 #endif /* RPL_LEAF_ONLY */
562 }
563 /*---------------------------------------------------------------------------*/
564 static void
565 dao_input(void)
566 {
567  uip_ipaddr_t dao_sender_addr;
568  rpl_dag_t *dag;
569  rpl_instance_t *instance;
570  unsigned char *buffer;
571  uint16_t sequence;
572  uint8_t instance_id;
573  uint8_t lifetime;
574  uint8_t prefixlen;
575  uint8_t flags;
576  uint8_t subopt_type;
577  /*
578  uint8_t pathcontrol;
579  uint8_t pathsequence;
580  */
581  uip_ipaddr_t prefix;
582  uip_ds6_route_t *rep;
583  uint8_t buffer_length;
584  int pos;
585  int len;
586  int i;
587  int learned_from;
588  rpl_parent_t *parent;
589  uip_ds6_nbr_t *nbr;
590 
591  prefixlen = 0;
592  parent = NULL;
593 
594  uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
595 
596  /* Destination Advertisement Object */
597  PRINTF("RPL: Received a DAO from ");
598  PRINT6ADDR(&dao_sender_addr);
599  PRINTF("\n");
600 
601  buffer = UIP_ICMP_PAYLOAD;
602  buffer_length = uip_len - uip_l3_icmp_hdr_len;
603 
604  pos = 0;
605  instance_id = buffer[pos++];
606 
607  instance = rpl_get_instance(instance_id);
608  if(instance == NULL) {
609  PRINTF("RPL: Ignoring a DAO for an unknown RPL instance(%u)\n",
610  instance_id);
611  return;
612  }
613 
614  lifetime = instance->default_lifetime;
615 
616  flags = buffer[pos++];
617  /* reserved */
618  pos++;
619  sequence = buffer[pos++];
620 
621  dag = instance->current_dag;
622  /* Is the DAG ID present? */
623  if(flags & RPL_DAO_D_FLAG) {
624  if(memcmp(&dag->dag_id, &buffer[pos], sizeof(dag->dag_id))) {
625  PRINTF("RPL: Ignoring a DAO for a DAG different from ours\n");
626  return;
627  }
628  pos += 16;
629  }
630 
631  learned_from = uip_is_addr_mcast(&dao_sender_addr) ?
632  RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
633 
634  PRINTF("RPL: DAO from %s\n",
635  learned_from == RPL_ROUTE_FROM_UNICAST_DAO? "unicast": "multicast");
636  if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
637  /* Check whether this is a DAO forwarding loop. */
638  parent = rpl_find_parent(dag, &dao_sender_addr);
639  /* check if this is a new DAO registration with an "illegal" rank */
640  /* if we already route to this node it is likely */
641  if(parent != NULL &&
642  DAG_RANK(parent->rank, instance) < DAG_RANK(dag->rank, instance)) {
643  PRINTF("RPL: Loop detected when receiving a unicast DAO from a node with a lower rank! (%u < %u)\n",
644  DAG_RANK(parent->rank, instance), DAG_RANK(dag->rank, instance));
645  parent->rank = INFINITE_RANK;
646  parent->updated = 1;
647  return;
648  }
649 
650  /* If we get the DAO from our parent, we also have a loop. */
651  if(parent != NULL && parent == dag->preferred_parent) {
652  PRINTF("RPL: Loop detected when receiving a unicast DAO from our parent\n");
653  parent->rank = INFINITE_RANK;
654  parent->updated = 1;
655  return;
656  }
657  }
658 
659  /* Check if there are any RPL options present. */
660  for(i = pos; i < buffer_length; i += len) {
661  subopt_type = buffer[i];
662  if(subopt_type == RPL_OPTION_PAD1) {
663  len = 1;
664  } else {
665  /* The option consists of a two-byte header and a payload. */
666  len = 2 + buffer[i + 1];
667  }
668 
669  switch(subopt_type) {
670  case RPL_OPTION_TARGET:
671  /* Handle the target option. */
672  prefixlen = buffer[i + 3];
673  memset(&prefix, 0, sizeof(prefix));
674  memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
675  break;
676  case RPL_OPTION_TRANSIT:
677  /* The path sequence and control are ignored. */
678  /* pathcontrol = buffer[i + 3];
679  pathsequence = buffer[i + 4];*/
680  lifetime = buffer[i + 5];
681  /* The parent address is also ignored. */
682  break;
683  }
684  }
685 
686  PRINTF("RPL: DAO lifetime: %u, prefix length: %u prefix: ",
687  (unsigned)lifetime, (unsigned)prefixlen);
688  PRINT6ADDR(&prefix);
689  PRINTF("\n");
690 
691 #if RPL_CONF_MULTICAST
692  if(uip_is_addr_mcast_global(&prefix)) {
693  mcast_group = uip_mcast6_route_add(&prefix);
694  if(mcast_group) {
695  mcast_group->dag = dag;
696  mcast_group->lifetime = RPL_LIFETIME(instance, lifetime);
697  }
698  goto fwd_dao;
699  }
700 #endif
701 
702  rep = uip_ds6_route_lookup(&prefix);
703 
704  if(lifetime == RPL_ZERO_LIFETIME) {
705  PRINTF("RPL: No-Path DAO received\n");
706  /* No-Path DAO received; invoke the route purging routine. */
707  if(rep != NULL &&
708  rep->state.nopath_received == 0 &&
709  rep->length == prefixlen &&
710  uip_ds6_route_nexthop(rep) != NULL &&
711  uip_ipaddr_cmp(uip_ds6_route_nexthop(rep), &dao_sender_addr)) {
712  PRINTF("RPL: Setting expiration timer for prefix ");
713  PRINT6ADDR(&prefix);
714  PRINTF("\n");
715  rep->state.nopath_received = 1;
716  rep->state.lifetime = DAO_EXPIRATION_TIMEOUT;
717 
718  /* We forward the incoming no-path DAO to our parent, if we have
719  one. */
720  if(dag->preferred_parent != NULL &&
721  rpl_get_parent_ipaddr(dag->preferred_parent) != NULL) {
722  PRINTF("RPL: Forwarding no-path DAO to parent ");
723  PRINT6ADDR(rpl_get_parent_ipaddr(dag->preferred_parent));
724  PRINTF("\n");
725  uip_icmp6_send(rpl_get_parent_ipaddr(dag->preferred_parent),
726  ICMP6_RPL, RPL_CODE_DAO, buffer_length);
727  }
728  if(flags & RPL_DAO_K_FLAG) {
729  dao_ack_output(instance, &dao_sender_addr, sequence);
730  }
731  }
732  return;
733  }
734 
735  PRINTF("RPL: adding DAO route\n");
736 
737  if((nbr = uip_ds6_nbr_lookup(&dao_sender_addr)) == NULL) {
738  if((nbr = uip_ds6_nbr_add(&dao_sender_addr,
739  (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER),
740  0, NBR_REACHABLE)) != NULL) {
741  /* set reachable timer */
742  stimer_set(&nbr->reachable, UIP_ND6_REACHABLE_TIME / 1000);
743  PRINTF("RPL: Neighbor added to neighbor cache ");
744  PRINT6ADDR(&dao_sender_addr);
745  PRINTF(", ");
746  PRINTLLADDR((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
747  PRINTF("\n");
748  } else {
749  PRINTF("RPL: Out of Memory, dropping DAO from ");
750  PRINT6ADDR(&dao_sender_addr);
751  PRINTF(", ");
752  PRINTLLADDR((uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
753  PRINTF("\n");
754  return;
755  }
756  } else {
757  PRINTF("RPL: Neighbor already in neighbor cache\n");
758  }
759 
760  rpl_lock_parent(parent);
761 
762  rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
763  if(rep == NULL) {
764  RPL_STAT(rpl_stats.mem_overflows++);
765  PRINTF("RPL: Could not add a route after receiving a DAO\n");
766  return;
767  }
768 
769  rep->state.lifetime = RPL_LIFETIME(instance, lifetime);
770  rep->state.learned_from = learned_from;
771 
772 #if RPL_CONF_MULTICAST
773 fwd_dao:
774 #endif
775 
776  if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
777  if(dag->preferred_parent != NULL &&
778  rpl_get_parent_ipaddr(dag->preferred_parent) != NULL) {
779  PRINTF("RPL: Forwarding DAO to parent ");
780  PRINT6ADDR(rpl_get_parent_ipaddr(dag->preferred_parent));
781  PRINTF("\n");
782  uip_icmp6_send(rpl_get_parent_ipaddr(dag->preferred_parent),
783  ICMP6_RPL, RPL_CODE_DAO, buffer_length);
784  }
785  if(flags & RPL_DAO_K_FLAG) {
786  dao_ack_output(instance, &dao_sender_addr, sequence);
787  }
788  }
789 }
790 /*---------------------------------------------------------------------------*/
791 void
792 dao_output(rpl_parent_t *parent, uint8_t lifetime)
793 {
794  /* Destination Advertisement Object */
795  uip_ipaddr_t prefix;
796 
797  if(get_global_addr(&prefix) == 0) {
798  PRINTF("RPL: No global address set for this node - suppressing DAO\n");
799  return;
800  }
801 
802  /* Sending a DAO with own prefix as target */
803  dao_output_target(parent, &prefix, lifetime);
804 }
805 /*---------------------------------------------------------------------------*/
806 void
807 dao_output_target(rpl_parent_t *parent, uip_ipaddr_t *prefix, uint8_t lifetime)
808 {
809  rpl_dag_t *dag;
810  rpl_instance_t *instance;
811  unsigned char *buffer;
812  uint8_t prefixlen;
813  int pos;
814 
815  /* Destination Advertisement Object */
816 
817  /* If we are in feather mode, we should not send any DAOs */
818  if(rpl_get_mode() == RPL_MODE_FEATHER) {
819  return;
820  }
821 
822  if(parent == NULL) {
823  PRINTF("RPL dao_output_target error parent NULL\n");
824  return;
825  }
826 
827  dag = parent->dag;
828  if(dag == NULL) {
829  PRINTF("RPL dao_output_target error dag NULL\n");
830  return;
831  }
832 
833  instance = dag->instance;
834 
835  if(instance == NULL) {
836  PRINTF("RPL dao_output_target error instance NULL\n");
837  return;
838  }
839  if(prefix == NULL) {
840  PRINTF("RPL dao_output_target error prefix NULL\n");
841  return;
842  }
843 #ifdef RPL_DEBUG_DAO_OUTPUT
844  RPL_DEBUG_DAO_OUTPUT(parent);
845 #endif
846 
847  buffer = UIP_ICMP_PAYLOAD;
848 
849  RPL_LOLLIPOP_INCREMENT(dao_sequence);
850  pos = 0;
851 
852  buffer[pos++] = instance->instance_id;
853  buffer[pos] = 0;
854 #if RPL_DAO_SPECIFY_DAG
855  buffer[pos] |= RPL_DAO_D_FLAG;
856 #endif /* RPL_DAO_SPECIFY_DAG */
857 #if RPL_CONF_DAO_ACK
858  buffer[pos] |= RPL_DAO_K_FLAG;
859 #endif /* RPL_CONF_DAO_ACK */
860  ++pos;
861  buffer[pos++] = 0; /* reserved */
862  buffer[pos++] = dao_sequence;
863 #if RPL_DAO_SPECIFY_DAG
864  memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
865  pos+=sizeof(dag->dag_id);
866 #endif /* RPL_DAO_SPECIFY_DAG */
867 
868  /* create target subopt */
869  prefixlen = sizeof(*prefix) * CHAR_BIT;
870  buffer[pos++] = RPL_OPTION_TARGET;
871  buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
872  buffer[pos++] = 0; /* reserved */
873  buffer[pos++] = prefixlen;
874  memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
875  pos += ((prefixlen + 7) / CHAR_BIT);
876 
877  /* Create a transit information sub-option. */
878  buffer[pos++] = RPL_OPTION_TRANSIT;
879  buffer[pos++] = 4;
880  buffer[pos++] = 0; /* flags - ignored */
881  buffer[pos++] = 0; /* path control - ignored */
882  buffer[pos++] = 0; /* path seq - ignored */
883  buffer[pos++] = lifetime;
884 
885  PRINTF("RPL: Sending DAO with prefix ");
886  PRINT6ADDR(prefix);
887  PRINTF(" to ");
888  PRINT6ADDR(rpl_get_parent_ipaddr(parent));
889  PRINTF("\n");
890 
891  if(rpl_get_parent_ipaddr(parent) != NULL) {
892  uip_icmp6_send(rpl_get_parent_ipaddr(parent), ICMP6_RPL, RPL_CODE_DAO, pos);
893  }
894 }
895 /*---------------------------------------------------------------------------*/
896 static void
897 dao_ack_input(void)
898 {
899 #if DEBUG
900  unsigned char *buffer;
901  uint8_t buffer_length;
902  uint8_t instance_id;
903  uint8_t sequence;
904  uint8_t status;
905 
906  buffer = UIP_ICMP_PAYLOAD;
907  buffer_length = uip_len - uip_l3_icmp_hdr_len;
908 
909  instance_id = buffer[0];
910  sequence = buffer[2];
911  status = buffer[3];
912 
913  PRINTF("RPL: Received a DAO ACK with sequence number %d and status %d from ",
914  sequence, status);
915  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
916  PRINTF("\n");
917 #endif /* DEBUG */
918 }
919 /*---------------------------------------------------------------------------*/
920 void
921 dao_ack_output(rpl_instance_t *instance, uip_ipaddr_t *dest, uint8_t sequence)
922 {
923  unsigned char *buffer;
924 
925  PRINTF("RPL: Sending a DAO ACK with sequence number %d to ", sequence);
926  PRINT6ADDR(dest);
927  PRINTF("\n");
928 
929  buffer = UIP_ICMP_PAYLOAD;
930 
931  buffer[0] = instance->instance_id;
932  buffer[1] = 0;
933  buffer[2] = sequence;
934  buffer[3] = 0;
935 
936  uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
937 }
938 /*---------------------------------------------------------------------------*/
939 void
940 uip_rpl_input(void)
941 {
942  PRINTF("Received an RPL control message\n");
943  switch(UIP_ICMP_BUF->icode) {
944  case RPL_CODE_DIO:
945  dio_input();
946  break;
947  case RPL_CODE_DIS:
948  dis_input();
949  break;
950  case RPL_CODE_DAO:
951  dao_input();
952  break;
953  case RPL_CODE_DAO_ACK:
954  dao_ack_input();
955  break;
956  default:
957  PRINTF("RPL: received an unknown ICMP6 code (%u)\n", UIP_ICMP_BUF->icode);
958  break;
959  }
960 
961  uip_len = 0;
962 }
963 #endif /* UIP_CONF_IPV6 */