Contiki-NG
rpl-icmp6.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \addtogroup rpl-lite
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  * Simon Duquennoy <simon.duquennoy@inria.fr>
42  * Contributors: Niclas Finne <nfi@sics.se>, Joel Hoglund <joel@sics.se>,
43  * Mathieu Pouillot <m.pouillot@watteco.com>,
44  * George Oikonomou <oikonomou@users.sourceforge.net> (multicast)
45  */
46 
47 #include "net/routing/rpl-lite/rpl.h"
48 #include "net/ipv6/uip-icmp6.h"
49 #include "net/packetbuf.h"
50 #include "lib/random.h"
51 
52 #include <limits.h>
53 
54 /* Log configuration */
55 #include "sys/log.h"
56 #define LOG_MODULE "RPL"
57 #define LOG_LEVEL LOG_LEVEL_RPL
58 
59 /*---------------------------------------------------------------------------*/
60 #define RPL_DIO_GROUNDED 0x80
61 #define RPL_DIO_MOP_SHIFT 3
62 #define RPL_DIO_MOP_MASK 0x38
63 #define RPL_DIO_PREFERENCE_MASK 0x07
64 
65 /*---------------------------------------------------------------------------*/
66 static void dis_input(void);
67 static void dio_input(void);
68 static void dao_input(void);
69 
70 /*---------------------------------------------------------------------------*/
71 /* Initialize RPL ICMPv6 message handlers */
72 UIP_ICMP6_HANDLER(dis_handler, ICMP6_RPL, RPL_CODE_DIS, dis_input);
73 UIP_ICMP6_HANDLER(dio_handler, ICMP6_RPL, RPL_CODE_DIO, dio_input);
74 UIP_ICMP6_HANDLER(dao_handler, ICMP6_RPL, RPL_CODE_DAO, dao_input);
75 
76 #if RPL_WITH_DAO_ACK
77 static void dao_ack_input(void);
78 UIP_ICMP6_HANDLER(dao_ack_handler, ICMP6_RPL, RPL_CODE_DAO_ACK, dao_ack_input);
79 #endif /* RPL_WITH_DAO_ACK */
80 
81 /*---------------------------------------------------------------------------*/
82 static uint32_t
83 get32(uint8_t *buffer, int pos)
84 {
85  return ((uint32_t)buffer[pos] << 24 | (uint32_t)buffer[pos + 1] << 16 |
86  (uint32_t)buffer[pos + 2] << 8 | buffer[pos + 3]);
87 }
88 /*---------------------------------------------------------------------------*/
89 static void
90 set32(uint8_t *buffer, int pos, uint32_t value)
91 {
92  buffer[pos++] = value >> 24;
93  buffer[pos++] = (value >> 16) & 0xff;
94  buffer[pos++] = (value >> 8) & 0xff;
95  buffer[pos++] = value & 0xff;
96 }
97 /*---------------------------------------------------------------------------*/
98 static uint16_t
99 get16(uint8_t *buffer, int pos)
100 {
101  return (uint16_t)buffer[pos] << 8 | buffer[pos + 1];
102 }
103 /*---------------------------------------------------------------------------*/
104 static void
105 set16(uint8_t *buffer, int pos, uint16_t value)
106 {
107  buffer[pos++] = value >> 8;
108  buffer[pos++] = value & 0xff;
109 }
110 /*---------------------------------------------------------------------------*/
112 rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason, void *data)
113 {
115 
116  if((nbr = uip_ds6_nbr_lookup(from)) == NULL) {
117  if((nbr = uip_ds6_nbr_add(from, (uip_lladdr_t *)
118  packetbuf_addr(PACKETBUF_ADDR_SENDER),
119  0, NBR_REACHABLE, reason, data)) == NULL) {
120  LOG_ERR("could not add neighbor to cache ");
121  LOG_ERR_6ADDR(from);
122  LOG_ERR_(", ");
123  LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
124  LOG_ERR_("\n");
125  }
126  }
127 
128  return nbr;
129 }
130 /*---------------------------------------------------------------------------*/
131 static void
132 dis_input(void)
133 {
134  if(!curr_instance.used) {
135  LOG_WARN("dis_input: not in an instance yet, discard\n");
136  goto discard;
137  }
138 
139  LOG_INFO("received a DIS from ");
140  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
141  LOG_INFO_("\n");
142 
143  rpl_process_dis(&UIP_IP_BUF->srcipaddr, uip_is_addr_mcast(&UIP_IP_BUF->destipaddr));
144 
145  discard:
146  uipbuf_clear();
147 }
148 /*---------------------------------------------------------------------------*/
149 void
151 {
152  unsigned char *buffer;
153 
154  /* Make sure we're up-to-date before sending data out */
156 
157  buffer = UIP_ICMP_PAYLOAD;
158  buffer[0] = buffer[1] = 0;
159 
160  if(addr == NULL) {
161  addr = &rpl_multicast_addr;
162  }
163 
164  LOG_INFO("sending a DIS to ");
165  LOG_INFO_6ADDR(addr);
166  LOG_INFO_("\n");
167 
168  uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIS, 2);
169 }
170 /*---------------------------------------------------------------------------*/
171 static void
172 dio_input(void)
173 {
174  unsigned char *buffer;
175  uint8_t buffer_length;
176  rpl_dio_t dio;
177  uint8_t subopt_type;
178  int i;
179  int len;
180  uip_ipaddr_t from;
181 
182  memset(&dio, 0, sizeof(dio));
183 
184  /* Set default values in case the DIO configuration option is missing. */
185  dio.dag_intdoubl = RPL_DIO_INTERVAL_DOUBLINGS;
186  dio.dag_intmin = RPL_DIO_INTERVAL_MIN;
187  dio.dag_redund = RPL_DIO_REDUNDANCY;
188  dio.dag_min_hoprankinc = RPL_MIN_HOPRANKINC;
189  dio.dag_max_rankinc = RPL_MAX_RANKINC;
190  dio.ocp = RPL_OF_OCP;
191  dio.default_lifetime = RPL_DEFAULT_LIFETIME;
192  dio.lifetime_unit = RPL_DEFAULT_LIFETIME_UNIT;
193 
194  uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
195 
196  buffer_length = uip_len - uip_l3_icmp_hdr_len;
197 
198  /* Process the DIO base option. */
199  i = 0;
200  buffer = UIP_ICMP_PAYLOAD;
201 
202  dio.instance_id = buffer[i++];
203  dio.version = buffer[i++];
204  dio.rank = get16(buffer, i);
205  i += 2;
206 
207  dio.grounded = buffer[i] & RPL_DIO_GROUNDED;
208  dio.mop = (buffer[i]& RPL_DIO_MOP_MASK) >> RPL_DIO_MOP_SHIFT;
209  dio.preference = buffer[i++] & RPL_DIO_PREFERENCE_MASK;
210 
211  dio.dtsn = buffer[i++];
212  /* two reserved bytes */
213  i += 2;
214 
215  memcpy(&dio.dag_id, buffer + i, sizeof(dio.dag_id));
216  i += sizeof(dio.dag_id);
217 
218  /* Check if there are any DIO suboptions. */
219  for(; i < buffer_length; i += len) {
220  subopt_type = buffer[i];
221  if(subopt_type == RPL_OPTION_PAD1) {
222  len = 1;
223  } else {
224  /* Suboption with a two-byte header + payload */
225  len = 2 + buffer[i + 1];
226  }
227 
228  if(len + i > buffer_length) {
229  LOG_ERR("dio_input: malformed packet, discard\n");
230  goto discard;
231  }
232 
233  switch(subopt_type) {
234  case RPL_OPTION_DAG_METRIC_CONTAINER:
235  if(len < 6) {
236  LOG_WARN("dio_input: invalid DAG MC, len %u, discard\n", len);
237  goto discard;
238  }
239  dio.mc.type = buffer[i + 2];
240  dio.mc.flags = buffer[i + 3] << 1;
241  dio.mc.flags |= buffer[i + 4] >> 7;
242  dio.mc.aggr = (buffer[i + 4] >> 4) & 0x3;
243  dio.mc.prec = buffer[i + 4] & 0xf;
244  dio.mc.length = buffer[i + 5];
245 
246  if(dio.mc.type == RPL_DAG_MC_NONE) {
247  /* No metric container: do nothing */
248  } else if(dio.mc.type == RPL_DAG_MC_ETX) {
249  dio.mc.obj.etx = get16(buffer, i + 6);
250  } else if(dio.mc.type == RPL_DAG_MC_ENERGY) {
251  dio.mc.obj.energy.flags = buffer[i + 6];
252  dio.mc.obj.energy.energy_est = buffer[i + 7];
253  } else {
254  LOG_WARN("dio_input: unsupported DAG MC type %u, discard\n", (unsigned)dio.mc.type);
255  goto discard;
256  }
257  break;
258  case RPL_OPTION_ROUTE_INFO:
259  if(len < 9) {
260  LOG_WARN("dio_input: invalid destination prefix option, len %u, discard\n", len);
261  goto discard;
262  }
263 
264  /* The flags field includes the preference value. */
265  dio.destination_prefix.length = buffer[i + 2];
266  dio.destination_prefix.flags = buffer[i + 3];
267  dio.destination_prefix.lifetime = get32(buffer, i + 4);
268 
269  if(((dio.destination_prefix.length + 7) / 8) + 8 <= len &&
270  dio.destination_prefix.length <= 128) {
271  memcpy(&dio.destination_prefix.prefix, &buffer[i + 8],
272  (dio.destination_prefix.length + 7) / 8);
273  } else {
274  LOG_WARN("dio_input: invalid route info option, len %u, discard\n", len);
275  goto discard;
276  }
277 
278  break;
279  case RPL_OPTION_DAG_CONF:
280  if(len != 16) {
281  LOG_WARN("dio_input: invalid DAG configuration option, len %u, discard\n", len);
282  goto discard;
283  }
284 
285  /* Path control field not yet implemented - at i + 2 */
286  dio.dag_intdoubl = buffer[i + 3];
287  dio.dag_intmin = buffer[i + 4];
288  dio.dag_redund = buffer[i + 5];
289  dio.dag_max_rankinc = get16(buffer, i + 6);
290  dio.dag_min_hoprankinc = get16(buffer, i + 8);
291  dio.ocp = get16(buffer, i + 10);
292  /* buffer + 12 is reserved */
293  dio.default_lifetime = buffer[i + 13];
294  dio.lifetime_unit = get16(buffer, i + 14);
295  break;
296  case RPL_OPTION_PREFIX_INFO:
297  if(len != 32) {
298  LOG_WARN("dio_input: invalid DAG prefix info, len %u, discard\n", len);
299  goto discard;
300  }
301  dio.prefix_info.length = buffer[i + 2];
302  dio.prefix_info.flags = buffer[i + 3];
303  /* valid lifetime is ingnored for now - at i + 4 */
304  /* preferred lifetime stored in lifetime */
305  dio.prefix_info.lifetime = get32(buffer, i + 8);
306  /* 32-bit reserved at i + 12 */
307  memcpy(&dio.prefix_info.prefix, &buffer[i + 16], 16);
308  break;
309  default:
310  LOG_WARN("dio_input: unsupported suboption type in DIO: %u, discard\n", (unsigned)subopt_type);
311  goto discard;
312  }
313  }
314 
315  LOG_INFO("received a %s-DIO from ",
316  uip_is_addr_mcast(&UIP_IP_BUF->destipaddr) ? "multicast" : "unicast");
317  LOG_INFO_6ADDR(&from);
318  LOG_INFO_(", instance_id %u, DAG ID ", (unsigned)dio.instance_id);
319  LOG_INFO_6ADDR(&dio.dag_id);
320  LOG_INFO_(", version %u, dtsn %u, rank %u\n",
321  (unsigned)dio.version,
322  (unsigned)dio.dtsn,
323  (unsigned)dio.rank);
324 
325  rpl_process_dio(&from, &dio);
326 
327 discard:
328  uipbuf_clear();
329 }
330 /*---------------------------------------------------------------------------*/
331 void
332 rpl_icmp6_dio_output(uip_ipaddr_t *uc_addr)
333 {
334  unsigned char *buffer;
335  int pos;
336  uip_ipaddr_t *addr = uc_addr;
337 
338  /* Make sure we're up-to-date before sending data out */
340 
341  if(rpl_get_leaf_only()) {
342  /* In leaf mode, we only send DIO messages as unicasts in response to
343  unicast DIS messages. */
344  if(uc_addr == NULL) {
345  /* Do not send multicast DIO in leaf mode */
346  return;
347  }
348  }
349 
350  /* DAG Information Object */
351  pos = 0;
352 
353  buffer = UIP_ICMP_PAYLOAD;
354  buffer[pos++] = curr_instance.instance_id;
355  buffer[pos++] = curr_instance.dag.version;
356 
357  if(rpl_get_leaf_only()) {
358  set16(buffer, pos, RPL_INFINITE_RANK);
359  } else {
360  set16(buffer, pos, curr_instance.dag.rank);
361  }
362  pos += 2;
363 
364  buffer[pos] = 0;
365  if(curr_instance.dag.grounded) {
366  buffer[pos] |= RPL_DIO_GROUNDED;
367  }
368 
369  buffer[pos] |= curr_instance.mop << RPL_DIO_MOP_SHIFT;
370  buffer[pos] |= curr_instance.dag.preference & RPL_DIO_PREFERENCE_MASK;
371  pos++;
372 
373  buffer[pos++] = curr_instance.dtsn_out;
374 
375  /* reserved 2 bytes */
376  buffer[pos++] = 0; /* flags */
377  buffer[pos++] = 0; /* reserved */
378 
379  memcpy(buffer + pos, &curr_instance.dag.dag_id, sizeof(curr_instance.dag.dag_id));
380  pos += 16;
381 
382  if(!rpl_get_leaf_only()) {
383  if(curr_instance.mc.type != RPL_DAG_MC_NONE) {
384  buffer[pos++] = RPL_OPTION_DAG_METRIC_CONTAINER;
385  buffer[pos++] = 6;
386  buffer[pos++] = curr_instance.mc.type;
387  buffer[pos++] = curr_instance.mc.flags >> 1;
388  buffer[pos] = (curr_instance.mc.flags & 1) << 7;
389  buffer[pos++] |= (curr_instance.mc.aggr << 4) | curr_instance.mc.prec;
390  if(curr_instance.mc.type == RPL_DAG_MC_ETX) {
391  buffer[pos++] = 2;
392  set16(buffer, pos, curr_instance.mc.obj.etx);
393  pos += 2;
394  } else if(curr_instance.mc.type == RPL_DAG_MC_ENERGY) {
395  buffer[pos++] = 2;
396  buffer[pos++] = curr_instance.mc.obj.energy.flags;
397  buffer[pos++] = curr_instance.mc.obj.energy.energy_est;
398  } else {
399  LOG_ERR("unable to send DIO because of unsupported DAG MC type %u\n",
400  (unsigned)curr_instance.mc.type);
401  return;
402  }
403  }
404  }
405 
406  /* Always add a DAG configuration option. */
407  buffer[pos++] = RPL_OPTION_DAG_CONF;
408  buffer[pos++] = 14;
409  buffer[pos++] = 0; /* No Auth, PCS = 0 */
410  buffer[pos++] = curr_instance.dio_intdoubl;
411  buffer[pos++] = curr_instance.dio_intmin;
412  buffer[pos++] = curr_instance.dio_redundancy;
413  set16(buffer, pos, curr_instance.max_rankinc);
414  pos += 2;
415  set16(buffer, pos, curr_instance.min_hoprankinc);
416  pos += 2;
417  /* OCP is in the DAG_CONF option */
418  set16(buffer, pos, curr_instance.of->ocp);
419  pos += 2;
420  buffer[pos++] = 0; /* reserved */
421  buffer[pos++] = curr_instance.default_lifetime;
422  set16(buffer, pos, curr_instance.lifetime_unit);
423  pos += 2;
424 
425  /* Check if we have a prefix to send also. */
426  if(curr_instance.dag.prefix_info.length > 0) {
427  buffer[pos++] = RPL_OPTION_PREFIX_INFO;
428  buffer[pos++] = 30; /* always 30 bytes + 2 long */
429  buffer[pos++] = curr_instance.dag.prefix_info.length;
430  buffer[pos++] = curr_instance.dag.prefix_info.flags;
431  set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
432  pos += 4;
433  set32(buffer, pos, curr_instance.dag.prefix_info.lifetime);
434  pos += 4;
435  memset(&buffer[pos], 0, 4);
436  pos += 4;
437  memcpy(&buffer[pos], &curr_instance.dag.prefix_info.prefix, 16);
438  pos += 16;
439  }
440 
441  if(!rpl_get_leaf_only()) {
442  addr = addr != NULL ? addr : &rpl_multicast_addr;
443  }
444 
445  LOG_INFO("sending a %s-DIO with rank %u to ",
446  uc_addr != NULL ? "unicast" : "multicast",
447  (unsigned)curr_instance.dag.rank);
448  LOG_INFO_6ADDR(addr);
449  LOG_INFO_("\n");
450 
451  uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_DIO, pos);
452 }
453 /*---------------------------------------------------------------------------*/
454 static void
455 dao_input(void)
456 {
457  struct rpl_dao dao;
458  uint8_t subopt_type;
459  unsigned char *buffer;
460  uint8_t buffer_length;
461  int pos;
462  int len;
463  int i;
464  uip_ipaddr_t from;
465 
466  memset(&dao, 0, sizeof(dao));
467 
468  dao.instance_id = UIP_ICMP_PAYLOAD[0];
469  if(!curr_instance.used || curr_instance.instance_id != dao.instance_id) {
470  LOG_ERR("dao_input: unknown RPL instance %u, discard\n", dao.instance_id);
471  goto discard;
472  }
473 
474  uip_ipaddr_copy(&from, &UIP_IP_BUF->srcipaddr);
475  memset(&dao.parent_addr, 0, 16);
476 
477  buffer = UIP_ICMP_PAYLOAD;
478  buffer_length = uip_len - uip_l3_icmp_hdr_len;
479 
480  pos = 0;
481  pos++; /* instance ID */
482  dao.lifetime = curr_instance.default_lifetime;
483  dao.flags = buffer[pos++];
484  pos++; /* reserved */
485  dao.sequence = buffer[pos++];
486 
487  /* Is the DAG ID present? */
488  if(dao.flags & RPL_DAO_D_FLAG) {
489  if(memcmp(&curr_instance.dag.dag_id, &buffer[pos], sizeof(curr_instance.dag.dag_id))) {
490  LOG_ERR("dao_input: different DAG ID ");
491  LOG_ERR_6ADDR((uip_ipaddr_t *)&buffer[pos]);
492  LOG_ERR_(", discard\n");
493  goto discard;
494  }
495  pos += 16;
496  }
497 
498  /* Check if there are any RPL options present. */
499  for(i = pos; i < buffer_length; i += len) {
500  subopt_type = buffer[i];
501  if(subopt_type == RPL_OPTION_PAD1) {
502  len = 1;
503  } else {
504  /* The option consists of a two-byte header and a payload. */
505  len = 2 + buffer[i + 1];
506  }
507 
508  switch(subopt_type) {
509  case RPL_OPTION_TARGET:
510  /* Handle the target option. */
511  dao.prefixlen = buffer[i + 3];
512  memset(&dao.prefix, 0, sizeof(dao.prefix));
513  memcpy(&dao.prefix, buffer + i + 4, (dao.prefixlen + 7) / CHAR_BIT);
514  break;
515  case RPL_OPTION_TRANSIT:
516  /* The path sequence and control are ignored. */
517  /* pathcontrol = buffer[i + 3];
518  pathsequence = buffer[i + 4];*/
519  dao.lifetime = buffer[i + 5];
520  if(len >= 20) {
521  memcpy(&dao.parent_addr, buffer + i + 6, 16);
522  }
523  break;
524  }
525  }
526 
527  /* Destination Advertisement Object */
528  LOG_INFO("received a %sDAO from ", dao.lifetime == 0 ? "No-path " : "");
529  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
530  LOG_INFO_(", seqno %u, lifetime %u, prefix ", dao.sequence, dao.lifetime);
531  LOG_INFO_6ADDR(&dao.prefix);
532  LOG_INFO_(", prefix length %u, parent ", dao.prefixlen);
533  LOG_INFO_6ADDR(&dao.parent_addr);
534  LOG_INFO_(" \n");
535 
536  rpl_process_dao(&from, &dao);
537 
538  discard:
539  uipbuf_clear();
540 }
541 /*---------------------------------------------------------------------------*/
542 void
543 rpl_icmp6_dao_output(uint8_t lifetime)
544 {
545  unsigned char *buffer;
546  uint8_t prefixlen;
547  int pos;
548  const uip_ipaddr_t *prefix = rpl_get_global_address();
549  uip_ipaddr_t *parent_ipaddr = rpl_neighbor_get_ipaddr(curr_instance.dag.preferred_parent);
550 
551  /* Make sure we're up-to-date before sending data out */
553 
554  if(!curr_instance.used) {
555  LOG_WARN("rpl_icmp6_dao_output: not in an instance, skip sending DAO\n");
556  return;
557  }
558 
559  if(curr_instance.dag.preferred_parent == NULL) {
560  LOG_WARN("rpl_icmp6_dao_output: no preferred parent, skip sending DAO\n");
561  return;
562  }
563 
564  if(prefix == NULL || parent_ipaddr == NULL || curr_instance.mop == RPL_MOP_NO_DOWNWARD_ROUTES) {
565  LOG_WARN("rpl_icmp6_dao_output: node not ready to send a DAO (prefix %p, parent addr %p, mop %u)\n",
566  prefix, parent_ipaddr, curr_instance.mop);
567  return;
568  }
569 
570  buffer = UIP_ICMP_PAYLOAD;
571  pos = 0;
572 
573  buffer[pos++] = curr_instance.instance_id;
574  buffer[pos] = 0;
575 #if RPL_WITH_DAO_ACK
576  if(lifetime != 0) {
577  buffer[pos] |= RPL_DAO_K_FLAG;
578  }
579 #endif /* RPL_WITH_DAO_ACK */
580  ++pos;
581  buffer[pos++] = 0; /* reserved */
582  buffer[pos++] = curr_instance.dag.dao_last_seqno;
583 
584  /* create target subopt */
585  prefixlen = sizeof(*prefix) * CHAR_BIT;
586  buffer[pos++] = RPL_OPTION_TARGET;
587  buffer[pos++] = 2 + ((prefixlen + 7) / CHAR_BIT);
588  buffer[pos++] = 0; /* reserved */
589  buffer[pos++] = prefixlen;
590  memcpy(buffer + pos, prefix, (prefixlen + 7) / CHAR_BIT);
591  pos += ((prefixlen + 7) / CHAR_BIT);
592 
593  /* Create a transit information sub-option. */
594  buffer[pos++] = RPL_OPTION_TRANSIT;
595  buffer[pos++] = 20;
596  buffer[pos++] = 0; /* flags - ignored */
597  buffer[pos++] = 0; /* path control - ignored */
598  buffer[pos++] = 0; /* path seq - ignored */
599  buffer[pos++] = lifetime;
600 
601  /* Include parent global IP address */
602  memcpy(buffer + pos, &curr_instance.dag.dag_id, 8); /* Prefix */
603  pos += 8;
604  memcpy(buffer + pos, ((const unsigned char *)parent_ipaddr) + 8, 8); /* Interface identifier */
605  pos += 8;
606 
607  LOG_INFO("sending a %sDAO seqno %u, tx count %u, lifetime %u, prefix ",
608  lifetime == 0 ? "No-path " : "",
609  curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_transmissions, lifetime);
610  LOG_INFO_6ADDR(prefix);
611  LOG_INFO_(" to ");
612  LOG_INFO_6ADDR(&curr_instance.dag.dag_id);
613  LOG_INFO_(", parent ");
614  LOG_INFO_6ADDR(parent_ipaddr);
615  LOG_INFO_("\n");
616 
617  /* Send DAO to root (IPv6 address is DAG ID) */
618  uip_icmp6_send(&curr_instance.dag.dag_id, ICMP6_RPL, RPL_CODE_DAO, pos);
619 }
620 #if RPL_WITH_DAO_ACK
621 /*---------------------------------------------------------------------------*/
622 static void
623 dao_ack_input(void)
624 {
625  uint8_t *buffer;
626  uint8_t instance_id;
627  uint8_t sequence;
628  uint8_t status;
629 
630  buffer = UIP_ICMP_PAYLOAD;
631 
632  instance_id = buffer[0];
633  sequence = buffer[2];
634  status = buffer[3];
635 
636  if(!curr_instance.used || curr_instance.instance_id != instance_id) {
637  LOG_ERR("dao_ack_input: unknown instance, discard\n");
638  goto discard;
639  }
640 
641  LOG_INFO("received a DAO-%s with seqno %d (%d %d) and status %d from ",
642  status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ? "ACK" : "NACK", sequence,
643  curr_instance.dag.dao_last_seqno, curr_instance.dag.dao_last_seqno, status);
644  LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
645  LOG_INFO_("\n");
646 
647  rpl_process_dao_ack(sequence, status);
648 
649  discard:
650  uipbuf_clear();
651 }
652 /*---------------------------------------------------------------------------*/
653 void
654 rpl_icmp6_dao_ack_output(uip_ipaddr_t *dest, uint8_t sequence, uint8_t status)
655 {
656  unsigned char *buffer;
657 
658  /* Make sure we're up-to-date before sending data out */
660 
661  buffer = UIP_ICMP_PAYLOAD;
662  buffer[0] = curr_instance.instance_id;
663  buffer[1] = 0;
664  buffer[2] = sequence;
665  buffer[3] = status;
666 
667  LOG_INFO("sending a DAO-%s seqno %d to ",
668  status < RPL_DAO_ACK_UNABLE_TO_ACCEPT ? "ACK" : "NACK", sequence);
669  LOG_INFO_6ADDR(dest);
670  LOG_INFO_(" with status %d\n", status);
671 
672  uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
673 }
674 #endif /* RPL_WITH_DAO_ACK */
675 /*---------------------------------------------------------------------------*/
676 void
678 {
679  uip_icmp6_register_input_handler(&dis_handler);
680  uip_icmp6_register_input_handler(&dio_handler);
681  uip_icmp6_register_input_handler(&dao_handler);
682 #if RPL_WITH_DAO_ACK
683  uip_icmp6_register_input_handler(&dao_ack_handler);
684 #endif /* RPL_WITH_DAO_ACK */
685 }
686 /*---------------------------------------------------------------------------*/
687 
688 /** @}*/
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition: uip.h:71
Header file for ICMPv6 message and error handing (RFC 4443)
uip_ipaddr_t * rpl_neighbor_get_ipaddr(rpl_nbr_t *nbr)
Returns a neighbor&#39;s (link-local) IPv6 address.
Definition: rpl-neighbor.c:252
static uip_ds6_nbr_t * nbr
Pointer to llao option in uip_buf.
Definition: uip-nd6.c:106
void rpl_process_dao(uip_ipaddr_t *from, rpl_dao_t *dao)
Processes incoming DAO.
Definition: rpl-dag.c:633
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:159
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
uint8_t rpl_get_leaf_only(void)
Get the value of the rpl_leaf_only flag.
Definition: rpl.c:241
void rpl_icmp6_dao_output(uint8_t lifetime)
Creates an ICMPv6 DAO packet and sends it to the root, advertising the current preferred parent...
Definition: rpl-icmp6.c:543
void rpl_process_dao_ack(uint8_t sequence, uint8_t status)
Processes incoming DAO-ACK.
uip_ds6_nbr_t * uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr)
Get the neighbor cache associated with a specified IPv6 address.
Definition: uip-ds6-nbr.c:467
void rpl_process_dis(uip_ipaddr_t *from, int is_multicast)
Processes incoming DIS.
Definition: rpl-dag.c:619
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1015
uip_ds6_nbr_t * rpl_icmp6_update_nbr_table(uip_ipaddr_t *from, nbr_table_reason_t reason, void *data)
Updates IPv6 neighbor cache on incoming link-local RPL ICMPv6 messages.
Definition: rpl-icmp6.c:194
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition: uip.h:1961
const uip_ipaddr_t * rpl_get_global_address(void)
Get one of the node&#39;s global addresses.
Definition: rpl.c:71
void rpl_icmp6_dao_ack_output(uip_ipaddr_t *dest, uint8_t sequence, uint8_t status)
Creates an ICMPv6 DAO-ACK packet and sends it to the originator of the ACK.
uip_ds6_nbr_t * uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, uint8_t isrouter, uint8_t state, nbr_table_reason_t reason, void *data)
Add a neighbor cache for a specified IPv6 address, which is associated with a specified link-layer ad...
Definition: uip-ds6-nbr.c:122
void rpl_dag_update_state(void)
Updates RPL internal state: selects preferred parent, updates rank & metreic container, triggers control traffic accordingly and updates uIP6 internal state.
Definition: rpl-dag.c:266
void rpl_process_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
Processes incoming DIO.
Definition: rpl-dag.c:1466
#define ICMP6_RPL
RPL.
Definition: uip-icmp6.h:66
void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler)
Register a handler which can handle a specific ICMPv6 message type.
Definition: uip-icmp6.c:102
Header file for the Packet buffer (packetbuf) management
void uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
Send an icmpv6 message.
Definition: uip-icmp6.c:230
void rpl_icmp6_dio_output(uip_ipaddr_t *uc_addr)
Creates an ICMPv6 DIO packet and sends it.
Definition: rpl-icmp6.c:332
Header file for the logging system
void rpl_icmp6_init()
Initializes rpl-icmp6 module, registers ICMPv6 handlers for all RPL ICMPv6 messages: DIO...
Definition: rpl-icmp6.c:677
The default nbr_table entry (when UIP_DS6_NBR_MULTI_IPV6_ADDRS is disabled), that implements nbr cach...
Definition: uip-ds6-nbr.h:105
void rpl_icmp6_dis_output(uip_ipaddr_t *addr)
Creates an ICMPv6 DIS packet and sends it.
Definition: rpl-icmp6.c:150