Contiki 2.5
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  */
44 
45 #include "net/tcpip.h"
46 #include "net/uip.h"
47 #include "net/uip-ds6.h"
48 #include "net/uip-nd6.h"
49 #include "net/uip-icmp6.h"
50 #include "net/rpl/rpl-private.h"
51 #include "net/packetbuf.h"
52 
53 #include <limits.h>
54 #include <string.h>
55 
56 #define DEBUG DEBUG_NONE
57 
58 #include "net/uip-debug.h"
59 
60 /*---------------------------------------------------------------------------*/
61 #define RPL_DIO_GROUNDED 0x80
62 #define RPL_DIO_MOP_SHIFT 3
63 #define RPL_DIO_MOP_MASK 0x3c
64 #define RPL_DIO_PREFERENCE_MASK 0x07
65 
66 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
67 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
68 #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len])
69 /*---------------------------------------------------------------------------*/
70 static void dis_input(void);
71 static void dio_input(void);
72 static void dao_input(void);
73 static void dao_ack_input(void);
74 
75 static uint8_t dao_sequence;
76 /*---------------------------------------------------------------------------*/
77 static int
78 get_global_addr(uip_ipaddr_t *addr)
79 {
80  int i;
81  int state;
82 
83  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
84  state = uip_ds6_if.addr_list[i].state;
85  if(uip_ds6_if.addr_list[i].isused &&
86  (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
87  if(!uip_is_addr_link_local(&uip_ds6_if.addr_list[i].ipaddr)) {
88  memcpy(addr, &uip_ds6_if.addr_list[i].ipaddr, sizeof(uip_ipaddr_t));
89  return 1;
90  }
91  }
92  }
93  return 0;
94 }
95 /*---------------------------------------------------------------------------*/
96 static uint32_t
97 get32(uint8_t *buffer, int pos)
98 {
99  return (uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
100  (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3];
101 }
102 /*---------------------------------------------------------------------------*/
103 static void
104 set32(uint8_t *buffer, int pos, uint32_t value)
105 {
106  buffer[pos++] = value >> 24;
107  buffer[pos++] = (value >> 16) & 0xff;
108  buffer[pos++] = (value >> 8) & 0xff;
109  buffer[pos++] = value & 0xff;
110 }
111 /*---------------------------------------------------------------------------*/
112 static uint16_t
113 get16(uint8_t *buffer, int pos)
114 {
115  return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
116 }
117 /*---------------------------------------------------------------------------*/
118 static void
119 set16(uint8_t *buffer, int pos, uint16_t value)
120 {
121  buffer[pos++] = value >> 8;
122  buffer[pos++] = value & 0xff;
123 }
124 /*---------------------------------------------------------------------------*/
125 static void
126 dis_input(void)
127 {
128  rpl_dag_t *dag;
129 
130  /* DAG Information Solicitation */
131  PRINTF("RPL: Received a DIS from ");
132  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
133  PRINTF("\n");
134 
135  dag = rpl_get_dag(RPL_ANY_INSTANCE);
136  if(dag != NULL) {
137  if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
138  PRINTF("RPL: Multicast DIS => reset DIO timer\n");
139  rpl_reset_dio_timer(dag, 0);
140  } else {
141  PRINTF("RPL: Unicast DIS, reply to sender\n");
142  dio_output(dag, &UIP_IP_BUF->srcipaddr);
143  }
144  }
145 }
146 /*---------------------------------------------------------------------------*/
147 void
148 dis_output(uip_ipaddr_t *addr)
149 {
150  unsigned char *buffer;
151  uip_ipaddr_t tmpaddr;
152 
153  /* DAG Information Solicitation - 2 bytes reserved */
154  /* 0 1 2 */
155  /* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 */
156  /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */
157  /* | Flags | Reserved | Option(s)... */
158  /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */
159 
160  buffer = UIP_ICMP_PAYLOAD;
161  buffer[0] = buffer[1] = 0;
162 
163  if(addr == NULL) {
164  PRINTF("RPL: Sending a DIS\n");
165  uip_create_linklocal_rplnodes_mcast(&tmpaddr);
166  addr = &tmpaddr;
167  } else {
168  PRINTF("RPL: Sending a unicast DIS\n");
169  }
170  uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 2);
171 }
172 /*---------------------------------------------------------------------------*/
173 static void
174 dio_input(void)
175 {
176  unsigned char *buffer;
177  uint8_t buffer_length;
178  rpl_dio_t dio;
179  uint8_t subopt_type;
180  int i;
181  int len;
182  uip_ipaddr_t from;
183  uip_ds6_nbr_t *nbr;
184 
185  memset(&dio, 0, sizeof(dio));
186 
187  dio.dag_intdoubl = DEFAULT_DIO_INTERVAL_DOUBLINGS;
188  dio.dag_intmin = DEFAULT_DIO_INTERVAL_MIN;
189  dio.dag_redund = DEFAULT_DIO_REDUNDANCY;
190 
191  uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
192 
193  /* DAG Information Object */
194  PRINTF("RPL: Received a DIO from ");
195  PRINT6ADDR(&from);
196  PRINTF("\n");
197 
198  if((nbr = uip_ds6_nbr_lookup(&from)) == NULL) {
199  if((nbr = uip_ds6_nbr_add(&from, (uip_lladdr_t *)
200  packetbuf_addr(PACKETBUF_ADDR_SENDER),
201  0, NBR_REACHABLE)) != NULL) {
202  /* set reachable timer */
203  stimer_set(&nbr->reachable, UIP_ND6_REACHABLE_TIME / 1000);
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  } else {
211  PRINTF("RPL: Neighbor already in neighbor cache\n");
212  }
213 
214  buffer_length = uip_len - uip_l2_l3_icmp_hdr_len;
215 
216 #if RPL_CONF_ADJUST_LLH_LEN
217  buffer_length+=UIP_LLH_LEN; //Add jackdaw, minimal-net ethernet header
218 #endif
219 
220  /* Process the DIO base option. */
221  i = 0;
222  buffer = UIP_ICMP_PAYLOAD;
223 
224  dio.instance_id = buffer[i++];
225  dio.version = buffer[i++];
226  dio.rank = get16(buffer, i);
227  i += 2;
228 
229  PRINTF("RPL: Incoming DIO rank %u\n", (unsigned)dio.rank);
230 
231  dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
232  dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
233  dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
234 
235  dio.dtsn = buffer[i++];
236  /* two reserved bytes */
237  i += 2;
238 
239  memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id));
240  i += sizeof(dio.dag_id);
241 
242  /* Check if there are any DIO suboptions. */
243  for(; i < buffer_length; i += len) {
244  subopt_type = buffer[i];
245  if(subopt_type == RPL_OPTION_PAD1) {
246  len = 1;
247  } else {
248  /* Suboption with a two-byte header + payload */
249  len = 2 + buffer[i + 1];
250  }
251 
252  if(len + i > buffer_length) {
253  PRINTF("RPL: Invalid DIO packet\n");
254  RPL_STAT(rpl_stats.malformed_msgs++);
255  return;
256  }
257 
258  PRINTF("RPL: DIO option %u, length: %u\n", subopt_type, len - 2);
259 
260  switch(subopt_type) {
261  case RPL_OPTION_DAG_METRIC_CONTAINER:
262  if(len < 6) {
263  PRINTF("RPL: Invalid DAG MC, len = %d\n", len);
264  RPL_STAT(rpl_stats.malformed_msgs++);
265  return;
266  }
267 
268  dio.mc.type = buffer[i + 2];
269  dio.mc.flags = buffer[i + 3] << 1;
270  dio.mc.flags |= buffer[i + 4] >> 7;
271  dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
272  dio.mc.prec = buffer[i + 4] & 0xf;
273  dio.mc.length = buffer[i + 5];
274 
275  if(dio.mc.type == RPL_DAG_MC_ETX) {
276  dio.mc.obj.etx = get16(buffer, i + 6);
277 
278  PRINTF("RPL: DAG MC: type %u, flags %u, aggr %u, prec %u, length %u, ETX %u\n",
279  (unsigned)dio.mc.type,
280  (unsigned)dio.mc.flags,
281  (unsigned)dio.mc.aggr,
282  (unsigned)dio.mc.prec,
283  (unsigned)dio.mc.length,
284  (unsigned)dio.mc.obj.etx);
285  } else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
286  dio.mc.obj.energy.flags = buffer[i + 6];
287  dio.mc.obj.energy.energy_est = buffer[i + 7];
288  } else {
289  PRINTF("RPL: Unhandled DAG MC type: %u\n", (unsigned)dio.mc.type);
290  return;
291  }
292  break;
293  case RPL_OPTION_ROUTE_INFO:
294  if(len < 9) {
295  PRINTF("RPL: Invalid destination prefix option, len = %d\n", len);
296  RPL_STAT(rpl_stats.malformed_msgs++);
297  return;
298  }
299 
300  /* flags is both preference and flags for now */
301  dio.destination_prefix.length = buffer[i + 2];
302  dio.destination_prefix.flags = buffer[i + 3];
303  dio.destination_prefix.lifetime = get32(buffer, i + 4);
304 
305  if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
306  dio.destination_prefix.length <= 128) {
307  PRINTF("RPL: Copying destination prefix\n");
308  memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
309  (dio.destination_prefix.length + 7) / 8);
310  } else {
311  PRINTF("RPL: Invalid route infoprefix option, len = %d\n", len);
312  RPL_STAT(rpl_stats.malformed_msgs++);
313  return;
314  }
315 
316  break;
317  case RPL_OPTION_DAG_CONF:
318  if(len != 16) {
319  PRINTF("RPL: Invalid DAG configuration option, len = %d\n", len);
320  RPL_STAT(rpl_stats.malformed_msgs++);
321  return;
322  }
323 
324  /* Path control field not yet implemented - at i + 2 */
325  dio.dag_intdoubl = buffer[i + 3];
326  dio.dag_intmin = buffer[i + 4];
327  dio.dag_redund = buffer[i + 5];
328  dio.dag_max_rankinc = get16(buffer, i + 6);
329  dio.dag_min_hoprankinc = get16(buffer, i + 8);
330  dio.ocp = get16(buffer, i + 10);
331  /* buffer + 12 is reserved */
332  dio.default_lifetime = buffer[i + 13];
333  dio.lifetime_unit = get16(buffer, i + 14);
334  PRINTF("RPL: DIO Conf:dbl=%d, min=%d red=%d maxinc=%d mininc=%d ocp=%d d_l=%u l_u=%u\n",
335  dio.dag_intdoubl, dio.dag_intmin, dio.dag_redund,
336  dio.dag_max_rankinc, dio.dag_min_hoprankinc, dio.ocp,
337  dio.default_lifetime, dio.lifetime_unit);
338  break;
339  case RPL_OPTION_PREFIX_INFO:
340  if(len != 32) {
341  PRINTF("RPL: DAG Prefix info not ok, len != 32\n");
342  RPL_STAT(rpl_stats.malformed_msgs++);
343  return;
344  }
345  dio.prefix_info.length = buffer[i + 2];
346  dio.prefix_info.flags = buffer[i + 3];
347  /* valid lifetime is ingnored for now - at i + 4 */
348  /* preferred lifetime stored in lifetime */
349  dio.prefix_info.lifetime = get32(buffer, i + 8);
350  /* 32-bit reserved at i + 12 */
351  PRINTF("RPL: Copying prefix information\n");
352  memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
353  break;
354  default:
355  PRINTF("RPL: Unsupported suboption type in DIO: %u\n",
356  (unsigned)subopt_type);
357  }
358  }
359 
360  rpl_process_dio(&from, &dio);
361 }
362 /*---------------------------------------------------------------------------*/
363 void
364 dio_output(rpl_dag_t *dag, uip_ipaddr_t *uc_addr)
365 {
366  unsigned char *buffer;
367  int pos;
368  uip_ipaddr_t addr;
369 
370  /* DAG Information Object */
371  pos = 0;
372 
373  buffer = UIP_ICMP_PAYLOAD;
374  buffer[pos++] = dag->instance_id;
375  buffer[pos++] = dag->version;
376  set16(buffer, pos, dag->rank);
377  pos += 2;
378 
379  buffer[pos] = 0;
380  if(dag->grounded) {
381  buffer[pos] |= RPL_DIO_GROUNDED;
382  }
383 
384  buffer[pos++] = dag->mop << RPL_DIO_MOP_SHIFT;
385  buffer[pos++] = ++dag->dtsn_out;
386 
387  /* reserved 2 bytes */
388  buffer[pos++] = 0; /* flags */
389  buffer[pos++] = 0; /* reserved */
390 
391  memcpy(buffer + pos, &dag->dag_id, sizeof(dag->dag_id));
392  pos += 16;
393 
394  if(dag->mc.type != RPL_DAG_MC_NONE) {
395  dag->of->update_metric_container(dag);
396 
397  buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
398  buffer[pos++] = 6;
399  buffer[pos++] = dag->mc.type;
400  buffer[pos++] = dag->mc.flags >> 1;
401  buffer[pos] = (dag->mc.flags & 1) << 7;
402  buffer[pos++] |= (dag->mc.aggr << 4) | dag->mc.prec;
403 
404  if(dag->mc.type == RPL_DAG_MC_ETX) {
405  buffer[pos++] = 2;
406  set16(buffer, pos, dag->mc.obj.etx);
407  pos += 2;
408  } else if(dag->mc.type == RPL_DAG_MC_ENERGY) {
409  buffer[pos++] = 2;
410  buffer[pos++] = dag->mc.obj.energy.flags;
411  buffer[pos++] = dag->mc.obj.energy.energy_est;
412  } else {
413  PRINTF("RPL: Unable to send DIO because of unhandled DAG MC type %u\n",
414  (unsigned)dag->mc.type);
415  return;
416  }
417  }
418 
419  /* Always add a DAG configuration option. */
420  buffer[pos++] = RPL_OPTION_DAG_CONF;
421  buffer[pos++] = 14;
422  buffer[pos++] = 0; /* No Auth, PCS = 0 */
423  buffer[pos++] = dag->dio_intdoubl;
424  buffer[pos++] = dag->dio_intmin;
425  buffer[pos++] = dag->dio_redundancy;
426  set16(buffer, pos, dag->max_rankinc);
427  pos += 2;
428  set16(buffer, pos, dag->min_hoprankinc);
429  pos += 2;
430  /* OCP is in the DAG_CONF option */
431  set16(buffer, pos, dag->of->ocp);
432  pos += 2;
433  buffer[pos++] = 0; /* reserved */
434  buffer[pos++] = dag->default_lifetime;
435  set16(buffer, pos, dag->lifetime_unit);
436  pos += 2;
437 
438  /* Check if we have a prefix to send also. */
439  if(dag->prefix_info.length > 0) {
440  buffer[pos++] = RPL_OPTION_PREFIX_INFO;
441  buffer[pos++] = 30; /* always 30 bytes + 2 long */
442  buffer[pos++] = dag->prefix_info.length;
443  buffer[pos++] = dag->prefix_info.flags;
444  set32(buffer, pos, dag->prefix_info.lifetime);
445  pos += 4;
446  set32(buffer, pos, dag->prefix_info.lifetime);
447  pos += 4;
448  memset(&buffer[pos], 0, 4);
449  pos += 4;
450  memcpy(&buffer[pos], &dag->prefix_info.prefix, 16);
451  pos += 16;
452  PRINTF("RPL: Sending prefix info in DIO for ");
453  PRINT6ADDR(&dag->prefix_info.prefix);
454  PRINTF("\n");
455  } else {
456  PRINTF("RPL: No prefix to announce (len %d)\n",
457  dag->prefix_info.length);
458  }
459 
460  /* Unicast requests get unicast replies! */
461  if(uc_addr == NULL) {
462  PRINTF("RPL: Sending a multicast-DIO with rank %u\n",
463  (unsigned)dag->rank);
464  uip_create_linklocal_rplnodes_mcast(&addr);
465  uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DIO, pos);
466  } else {
467  PRINTF("RPL: Sending unicast-DIO with rank %u to ",
468  (unsigned)dag->rank);
469  PRINT6ADDR(uc_addr);
470  PRINTF("\n");
471  uip_icmp6_send(uc_addr, ICMP6_RPL, RPL_CODE_DIO, pos);
472  }
473 }
474 /*---------------------------------------------------------------------------*/
475 static void
476 dao_input(void)
477 {
478  uip_ipaddr_t dao_sender_addr;
479  rpl_dag_t *dag;
480  unsigned char *buffer;
481  uint16_t sequence;
482  uint8_t instance_id;
483  rpl_lifetime_t lifetime;
484  uint8_t prefixlen;
485  uint8_t flags;
486  uint8_t subopt_type;
487  uint8_t pathcontrol;
488  uint8_t pathsequence;
489  uip_ipaddr_t prefix;
490  uip_ds6_route_t *rep;
491  uint8_t buffer_length;
492  int pos;
493  int len;
494  int i;
495  int learned_from;
496  rpl_parent_t *p;
497 
498  prefixlen = 0;
499 
500  uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
501 
502  /* Destination Advertisement Object */
503  PRINTF("RPL: Received a DAO from ");
504  PRINT6ADDR(&dao_sender_addr);
505  PRINTF("\n");
506 
507  buffer = UIP_ICMP_PAYLOAD;
508  buffer_length = uip_len - uip_l2_l3_icmp_hdr_len;
509 #if RPL_CONF_ADJUST_LLH_LEN
510  buffer_length += UIP_LLH_LEN; /* Add jackdaw, minimal-net ethernet header */
511 #endif
512 
513  pos = 0;
514  instance_id = buffer[pos++];
515 
516  dag = rpl_get_dag(instance_id);
517  if(dag == NULL) {
518  PRINTF("RPL: Ignoring a DAO for a different RPL instance (%u)\n",
519  instance_id);
520  return;
521  }
522 
523  lifetime = dag->default_lifetime;
524 
525  flags = buffer[pos++];
526  /* reserved */
527  pos++;
528  sequence = buffer[pos++];
529 
530  /* Is the DAGID present? */
531  if(flags & RPL_DAO_D_FLAG) {
532  /* Currently the DAG ID is ignored since we only use global
533  RPL Instance IDs. */
534  pos += 16;
535  }
536 
537  /* Check if there are any RPL options present. */
538  i = pos;
539  for(; i < buffer_length; i += len) {
540  subopt_type = buffer[i];
541  if(subopt_type == RPL_OPTION_PAD1) {
542  len = 1;
543  } else {
544  /* The option consists of a two-byte header and a payload. */
545  len = 2 + buffer[i + 1];
546  }
547 
548  switch(subopt_type) {
549  case RPL_OPTION_TARGET:
550  /* handle the target option */
551  prefixlen = buffer[i + 3];
552  memset(&prefix, 0, sizeof(prefix));
553  memcpy(&prefix, buffer + i + 4, (prefixlen + 7) / CHAR_BIT);
554  break;
555  case RPL_OPTION_TRANSIT:
556  /* The path sequence and control are ignored. */
557  pathcontrol = buffer[i + 3];
558  pathsequence = buffer[i + 4];
559  lifetime = buffer[i + 5];
560  /* The parent address is also ignored. */
561  break;
562  }
563  }
564 
565  PRINTF("RPL: DAO lifetime: %u, prefix length: %u prefix: ",
566  (unsigned)lifetime, (unsigned)prefixlen);
567  PRINT6ADDR(&prefix);
568  PRINTF("\n");
569 
570  rep = uip_ds6_route_lookup(&prefix);
571 
572  if(lifetime == ZERO_LIFETIME) {
573  /* No-Path DAO received; invoke the route purging routine. */
574  if(rep != NULL && rep->state.saved_lifetime == 0) {
575  PRINTF("RPL: Setting expiration timer for prefix ");
576  PRINT6ADDR(&prefix);
577  PRINTF("\n");
578  rep->state.saved_lifetime = rep->state.lifetime;
579  rep->state.lifetime = DAO_EXPIRATION_TIMEOUT;
580  }
581  return;
582  }
583 
584  learned_from = uip_is_addr_mcast(&dao_sender_addr) ?
585  RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
586 
587  if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
588  /* Check whether this is a DAO forwarding loop. */
589  p = rpl_find_parent(dag, &dao_sender_addr);
590  if(p != NULL && DAG_RANK(p->rank, dag) < DAG_RANK(dag->rank, dag)) {
591  PRINTF("RPL: Loop detected when receiving a unicast DAO from a node with a lower rank! (%u < %u)\n",
592  DAG_RANK(p->rank, dag), DAG_RANK(dag->rank, dag));
593  p->rank = INFINITE_RANK;
594  p->updated = 1;
595  return;
596  }
597  }
598 
599  if(rep == NULL) {
600  rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
601  if(rep == NULL) {
602  RPL_STAT(rpl_stats.mem_overflows++);
603  PRINTF("RPL: Could not add a route after receiving a DAO\n");
604  return;
605  }
606  }
607 
608  rep->state.lifetime = RPL_LIFETIME(dag, lifetime);
609  rep->state.learned_from = learned_from;
610 
611  if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
612  if(dag->preferred_parent) {
613  PRINTF("RPL: Forwarding DAO to parent ");
614  PRINT6ADDR(&dag->preferred_parent->addr);
615  PRINTF("\n");
616  uip_icmp6_send(&dag->preferred_parent->addr,
617  ICMP6_RPL, RPL_CODE_DAO, buffer_length);
618  } else if(flags & RPL_DAO_K_FLAG) {
619  dao_ack_output(dag, &dao_sender_addr, sequence);
620  }
621  }
622 }
623 /*---------------------------------------------------------------------------*/
624 void
625 dao_output(rpl_parent_t *n, rpl_lifetime_t lifetime)
626 {
627  rpl_dag_t *dag;
628  unsigned char *buffer;
629  uint8_t prefixlen;
630  uip_ipaddr_t addr;
631  uip_ipaddr_t prefix;
632  int pos;
633 
634  /* Destination Advertisement Object */
635  if(get_global_addr(&prefix) == 0) {
636  PRINTF("RPL: No global address set for this node - suppressing DAO\n");
637  return;
638  }
639 
640  if(n == NULL) {
641  dag = rpl_get_dag(RPL_ANY_INSTANCE);
642  if(dag == NULL) {
643  PRINTF("RPL: Did not join a DAG before sending DAO\n");
644  return;
645  }
646  } else {
647  dag = n->dag;
648  }
649 
650  buffer = UIP_ICMP_PAYLOAD;
651 
652  ++dao_sequence;
653  pos = 0;
654 
655  buffer[pos++] = dag->instance_id;
656 #if RPL_CONF_DAO_ACK
657  buffer[pos++] = RPL_DAO_K_FLAG; /* DAO ACK request, no DODAGID */
658 #else
659  buffer[pos++] = 0; /* No DAO ACK request, no DODAGID */
660 #endif
661  buffer[pos++] = 0; /* reserved */
662  buffer[pos++] = dao_sequence & 0xff;
663 
664  /* create target subopt */
665  prefixlen = sizeof(prefix) * CHAR_BIT;
666  buffer[pos++] = RPL_OPTION_TARGET;
667  buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
668  buffer[pos++] = 0; /* reserved */
669  buffer[pos++] = prefixlen;
670  memcpy(buffer + pos, &prefix, (prefixlen + 7) / CHAR_BIT);
671  pos += ((prefixlen + 7) / CHAR_BIT);
672 
673  /* Create a transit information sub-option. */
674  buffer[pos++] = RPL_OPTION_TRANSIT;
675  buffer[pos++] = 4;
676  buffer[pos++] = 0; /* flags - ignored */
677  buffer[pos++] = 0; /* path control - ignored */
678  buffer[pos++] = 0; /* path seq - ignored */
679  buffer[pos++] = lifetime;
680 
681  if(n == NULL) {
682  uip_create_linklocal_rplnodes_mcast(&addr);
683  } else {
684  uip_ipaddr_copy(&addr, &n->addr);
685  }
686 
687  PRINTF("RPL: Sending DAO with prefix ");
688  PRINT6ADDR(&prefix);
689  PRINTF(" to ");
690  if(n != NULL) {
691  PRINT6ADDR(&n->addr);
692  } else {
693  PRINTF("multicast address");
694  }
695  PRINTF("\n");
696 
697  uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DAO, pos);
698 }
699 /*---------------------------------------------------------------------------*/
700 static void
701 dao_ack_input(void)
702 {
703  unsigned char *buffer;
704  uint8_t buffer_length;
705  uint8_t instance_id;
706  uint8_t sequence;
707  uint8_t status;
708 
709  buffer = UIP_ICMP_PAYLOAD;
710  buffer_length = uip_len - uip_l2_l3_icmp_hdr_len;
711 #if RPL_CONF_ADJUST_LLH_LEN
712  buffer_length+=UIP_LLH_LEN; //Add jackdaw, minimal-net ethernet header
713 #endif
714 
715  instance_id = buffer[0];
716  sequence = buffer[2];
717  status = buffer[3];
718 
719  PRINTF("RPL: Received a DAO ACK with sequence number %d and status %d from ",
720  sequence, status);
721  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
722  PRINTF("\n");
723 }
724 /*---------------------------------------------------------------------------*/
725 void
726 dao_ack_output(rpl_dag_t *dag, uip_ipaddr_t *dest, uint8_t sequence)
727 {
728  unsigned char *buffer;
729 
730  PRINTF("RPL: Sending a DAO ACK with sequence number %d to ", sequence);
731  PRINT6ADDR(dest);
732  PRINTF("\n");
733 
734  buffer = UIP_ICMP_PAYLOAD;
735 
736  buffer[0] = dag->instance_id;
737  buffer[1] = 0;
738  buffer[2] = sequence;
739  buffer[3] = 0;
740 
741  uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
742 }
743 /*---------------------------------------------------------------------------*/
744 void
745 uip_rpl_input(void)
746 {
747  PRINTF("Received an RPL control message\n");
748  switch(UIP_ICMP_BUF->icode) {
749  case RPL_CODE_DIO:
750  dio_input();
751  break;
752  case RPL_CODE_DIS:
753  dis_input();
754  break;
755  case RPL_CODE_DAO:
756  dao_input();
757  break;
758  case RPL_CODE_DAO_ACK:
759  dao_ack_input();
760  break;
761  default:
762  PRINTF("RPL: received an unknown ICMP6 code (%u)\n", UIP_ICMP_BUF->icode);
763  break;
764  }
765 
766  uip_len = 0;
767 }