Contiki 2.5
uip-nd6.c
Go to the documentation of this file.
1 /**
2  * \addtogroup uip6
3  * @{
4  */
5 
6 /**
7  * \file
8  * Neighbor discovery (RFC 4861)
9  * \author Mathilde Durvy <mdurvy@cisco.com>
10  * \author Julien Abeille <jabeille@cisco.com>
11  */
12 
13 /*
14  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the project nor the names of its contributors
26  * may be used to endorse or promote products derived from this software
27  * without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41 /*
42  * Copyright (c) 2006, Swedish Institute of Computer Science.
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  * notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in the
52  * documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the Institute nor the names of its contributors
54  * may be used to endorse or promote products derived from this software
55  * without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  */
70 
71 #include <string.h>
72 #include "net/uip-icmp6.h"
73 #include "net/uip-nd6.h"
74 #include "net/uip-ds6.h"
75 #include "lib/random.h"
76 
77 /*------------------------------------------------------------------*/
78 #define DEBUG 0
79 #if DEBUG
80 #include <stdio.h>
81 #define PRINTF(...) printf(__VA_ARGS__)
82 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
83 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5])
84 #else
85 #define PRINTF(...)
86 #define PRINT6ADDR(addr)
87 #define PRINTLLADDR(addr)
88 #endif
89 
90 #if UIP_LOGGING
91 #include <stdio.h>
92 void uip_log(char *msg);
93 
94 #define UIP_LOG(m) uip_log(m)
95 #else
96 #define UIP_LOG(m)
97 #endif /* UIP_LOGGING == 1 */
98 
99 /*------------------------------------------------------------------*/
100 /** @{ */
101 /** \name Pointers to the header structures.
102  * All pointers except UIP_IP_BUF depend on uip_ext_len, which at
103  * packet reception, is the total length of the extension headers.
104  *
105  * The pointer to ND6 options header also depends on nd6_opt_offset,
106  * which we set in each function.
107  *
108  * Care should be taken when manipulating these buffers about the
109  * value of these length variables
110  */
111 
112 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) /**< Pointer to IP header */
113 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) /**< Pointer to ICMP header*/
114 /**@{ Pointers to messages just after icmp header */
115 #define UIP_ND6_RS_BUF ((uip_nd6_rs *)&uip_buf[uip_l2_l3_icmp_hdr_len])
116 #define UIP_ND6_RA_BUF ((uip_nd6_ra *)&uip_buf[uip_l2_l3_icmp_hdr_len])
117 #define UIP_ND6_NS_BUF ((uip_nd6_ns *)&uip_buf[uip_l2_l3_icmp_hdr_len])
118 #define UIP_ND6_NA_BUF ((uip_nd6_na *)&uip_buf[uip_l2_l3_icmp_hdr_len])
119 /** @} */
120 /** Pointer to ND option */
121 #define UIP_ND6_OPT_HDR_BUF ((uip_nd6_opt_hdr *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset])
122 #define UIP_ND6_OPT_PREFIX_BUF ((uip_nd6_opt_prefix_info *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset])
123 #define UIP_ND6_OPT_MTU_BUF ((uip_nd6_opt_mtu *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset])
124 /** @} */
125 
126 static uint8_t nd6_opt_offset; /** Offset from the end of the icmpv6 header to the option in uip_buf*/
127 static uint8_t *nd6_opt_llao; /** Pointer to llao option in uip_buf */
128 
129 #if !UIP_CONF_ROUTER // TBD see if we move it to ra_input
130 static uip_nd6_opt_prefix_info *nd6_opt_prefix_info; /** Pointer to prefix information option in uip_buf */
131 static uip_ipaddr_t ipaddr;
132 static uip_ds6_prefix_t *prefix; /** Pointer to a prefix list entry */
133 #endif
134 static uip_ds6_nbr_t *nbr; /** Pointer to a nbr cache entry*/
135 static uip_ds6_defrt_t *defrt; /** Pointer to a router list entry */
136 static uip_ds6_addr_t *addr; /** Pointer to an interface address */
137 
138 
139 /*------------------------------------------------------------------*/
140 /* create a llao */
141 static void
142 create_llao(uint8_t *llao, uint8_t type) {
143  llao[UIP_ND6_OPT_TYPE_OFFSET] = type;
144  llao[UIP_ND6_OPT_LEN_OFFSET] = UIP_ND6_OPT_LLAO_LEN >> 3;
145  memcpy(&llao[UIP_ND6_OPT_DATA_OFFSET], &uip_lladdr, UIP_LLADDR_LEN);
146  /* padding on some */
147  memset(&llao[UIP_ND6_OPT_DATA_OFFSET + UIP_LLADDR_LEN], 0,
148  UIP_ND6_OPT_LLAO_LEN - 2 - UIP_LLADDR_LEN);
149 }
150 
151 /*------------------------------------------------------------------*/
152 
153 
154 void
156 {
157  PRINTF("Received NS from");
158  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
159  PRINTF("to");
160  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
161  PRINTF("with target address");
162  PRINT6ADDR((uip_ipaddr_t *) (&UIP_ND6_NS_BUF->tgtipaddr));
163  PRINTF("\n");
164  UIP_STAT(++uip_stat.nd6.recv);
165 
166  u8_t flags;
167 
168 #if UIP_CONF_IPV6_CHECKS
169  if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
170  (uip_is_addr_mcast(&UIP_ND6_NS_BUF->tgtipaddr)) ||
171  (UIP_ICMP_BUF->icode != 0)) {
172  PRINTF("NS received is bad\n");
173  goto discard;
174  }
175 #endif /* UIP_CONF_IPV6_CHECKS */
176 
177  /* Options processing */
178  nd6_opt_llao = NULL;
179  nd6_opt_offset = UIP_ND6_NS_LEN;
180  while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
181 #if UIP_CONF_IPV6_CHECKS
182  if(UIP_ND6_OPT_HDR_BUF->len == 0) {
183  PRINTF("NS received is bad\n");
184  goto discard;
185  }
186 #endif /* UIP_CONF_IPV6_CHECKS */
187  switch (UIP_ND6_OPT_HDR_BUF->type) {
188  case UIP_ND6_OPT_SLLAO:
189  nd6_opt_llao = &uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset];
190 #if UIP_CONF_IPV6_CHECKS
191  /* There must be NO option in a DAD NS */
192  if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
193  PRINTF("NS received is bad\n");
194  goto discard;
195  } else {
196 #endif /*UIP_CONF_IPV6_CHECKS */
197  nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr);
198  if(nbr == NULL) {
199  uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr,
200  (uip_lladdr_t *)&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
201  0, NBR_STALE);
202  } else {
203  if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
204  &nbr->lladdr, UIP_LLADDR_LEN) != 0) {
205  memcpy(&nbr->lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
206  UIP_LLADDR_LEN);
207  nbr->state = NBR_STALE;
208  } else {
209  if(nbr->state == NBR_INCOMPLETE) {
210  nbr->state = NBR_STALE;
211  }
212  }
213  }
214 #if UIP_CONF_IPV6_CHECKS
215  }
216 #endif /*UIP_CONF_IPV6_CHECKS */
217  break;
218  default:
219  PRINTF("ND option not supported in NS");
220  break;
221  }
222  nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
223  }
224 
225  addr = uip_ds6_addr_lookup(&UIP_ND6_NS_BUF->tgtipaddr);
226  if(addr != NULL) {
227  if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
228  /* DAD CASE */
229 #if UIP_CONF_IPV6_CHECKS
230  if(!uip_is_addr_solicited_node(&UIP_IP_BUF->destipaddr)) {
231  PRINTF("NS received is bad\n");
232  goto discard;
233  }
234 #endif /* UIP_CONF_IPV6_CHECKS */
235  if(addr->state != ADDR_TENTATIVE) {
236  uip_create_linklocal_allnodes_mcast(&UIP_IP_BUF->destipaddr);
237  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
238  flags = UIP_ND6_NA_FLAG_OVERRIDE;
239  goto create_na;
240  } else {
241  /** \todo if I sent a NS before him, I win */
242  uip_ds6_dad_failed(addr);
243  goto discard;
244  }
245  }
246 #if UIP_CONF_IPV6_CHECKS
247  if(uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr)) {
248  /**
249  * \NOTE do we do something here? we both are using the same address.
250  * If we are doing dad, we could cancel it, though we should receive a
251  * NA in response of DAD NS we sent, hence DAD will fail anyway. If we
252  * were not doing DAD, it means there is a duplicate in the network!
253  */
254  PRINTF("NS received is bad\n");
255  goto discard;
256  }
257 #endif /*UIP_CONF_IPV6_CHECKS */
258 
259  /* Address resolution case */
260  if(uip_is_addr_solicited_node(&UIP_IP_BUF->destipaddr)) {
261  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
262  uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_ND6_NS_BUF->tgtipaddr);
263  flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
264  goto create_na;
265  }
266 
267  /* NUD CASE */
268  if(uip_ds6_addr_lookup(&UIP_IP_BUF->destipaddr) == addr) {
269  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
270  uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_ND6_NS_BUF->tgtipaddr);
271  flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
272  goto create_na;
273  } else {
274 #if UIP_CONF_IPV6_CHECKS
275  PRINTF("NS received is bad\n");
276  goto discard;
277 #endif /* UIP_CONF_IPV6_CHECKS */
278  }
279  } else {
280  goto discard;
281  }
282 
283 
284 create_na:
285  uip_ext_len = 0;
286  UIP_IP_BUF->vtc = 0x60;
287  UIP_IP_BUF->tcflow = 0;
288  UIP_IP_BUF->flow = 0;
289  UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */
290  UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN;
291  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
293 
294  UIP_ICMP_BUF->type = ICMP6_NA;
295  UIP_ICMP_BUF->icode = 0;
296 
297  UIP_ND6_NA_BUF->flagsreserved = flags;
298  memcpy(&UIP_ND6_NA_BUF->tgtipaddr, &addr->ipaddr, sizeof(uip_ipaddr_t));
299 
300  create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NA_LEN],
301  UIP_ND6_OPT_TLLAO);
302 
303  UIP_ICMP_BUF->icmpchksum = 0;
304  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
305 
306  uip_len =
307  UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN;
308 
309  UIP_STAT(++uip_stat.nd6.sent);
310  PRINTF("Sending NA to");
311  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
312  PRINTF("from");
313  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
314  PRINTF("with target address");
315  PRINT6ADDR(&UIP_ND6_NA_BUF->tgtipaddr);
316  PRINTF("\n");
317  return;
318 
319 discard:
320  uip_len = 0;
321  return;
322 }
323 
324 
325 
326 /*------------------------------------------------------------------*/
327 void
329 {
330  uip_ext_len = 0;
331  UIP_IP_BUF->vtc = 0x60;
332  UIP_IP_BUF->tcflow = 0;
333  UIP_IP_BUF->flow = 0;
334  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
336 
337  if(dest == NULL) {
338  uip_create_solicited_node(tgt, &UIP_IP_BUF->destipaddr);
339  } else {
340  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, dest);
341  }
342  UIP_ICMP_BUF->type = ICMP6_NS;
343  UIP_ICMP_BUF->icode = 0;
344  UIP_ND6_NS_BUF->reserved = 0;
345  uip_ipaddr_copy((uip_ipaddr_t *) &UIP_ND6_NS_BUF->tgtipaddr, tgt);
346  UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */
347  /*
348  * check if we add a SLLAO option: for DAD, MUST NOT, for NUD, MAY
349  * (here yes), for Address resolution , MUST
350  */
351  if(!(uip_ds6_is_my_addr(tgt))) {
352  if(src != NULL) {
353  uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, src);
354  } else {
355  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
356  }
357  UIP_IP_BUF->len[1] =
358  UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN;
359 
360  create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NS_LEN],
361  UIP_ND6_OPT_SLLAO);
362 
363  uip_len =
364  UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN;
365  } else {
366  uip_create_unspecified(&UIP_IP_BUF->srcipaddr);
367  UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NS_LEN;
368  uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NS_LEN;
369  }
370 
371  UIP_ICMP_BUF->icmpchksum = 0;
372  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
373 
374  UIP_STAT(++uip_stat.nd6.sent);
375  PRINTF("Sending NS to");
376  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
377  PRINTF("from");
378  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
379  PRINTF("with target address");
380  PRINT6ADDR(tgt);
381  PRINTF("\n");
382  return;
383 }
384 
385 
386 
387 /*------------------------------------------------------------------*/
388 void
390 {
391  PRINTF("Received NA from");
392  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
393  PRINTF("to");
394  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
395  PRINTF("with target address");
396  PRINT6ADDR((uip_ipaddr_t *) (&UIP_ND6_NA_BUF->tgtipaddr));
397  PRINTF("\n");
398  UIP_STAT(++uip_stat.nd6.recv);
399 
400  /*
401  * booleans. the three last one are not 0 or 1 but 0 or 0x80, 0x40, 0x20
402  * but it works. Be careful though, do not use tests such as is_router == 1
403  */
404  u8_t is_llchange = 0;
405  u8_t is_router = ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_ROUTER));
406  u8_t is_solicited =
407  ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_SOLICITED));
408  u8_t is_override =
409  ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_OVERRIDE));
410 
411 #if UIP_CONF_IPV6_CHECKS
412  if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
413  (UIP_ICMP_BUF->icode != 0) ||
414  (uip_is_addr_mcast(&UIP_ND6_NA_BUF->tgtipaddr)) ||
415  (is_solicited && uip_is_addr_mcast(&UIP_IP_BUF->destipaddr))) {
416  PRINTF("NA received is bad\n");
417  goto discard;
418  }
419 #endif /*UIP_CONF_IPV6_CHECKS */
420 
421  /* Options processing: we handle TLLAO, and must ignore others */
422  nd6_opt_offset = UIP_ND6_NA_LEN;
423  nd6_opt_llao = NULL;
424  while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
425 #if UIP_CONF_IPV6_CHECKS
426  if(UIP_ND6_OPT_HDR_BUF->len == 0) {
427  PRINTF("NA received is bad\n");
428  goto discard;
429  }
430 #endif /*UIP_CONF_IPV6_CHECKS */
431  switch (UIP_ND6_OPT_HDR_BUF->type) {
432  case UIP_ND6_OPT_TLLAO:
433  nd6_opt_llao = (uint8_t *)UIP_ND6_OPT_HDR_BUF;
434  break;
435  default:
436  PRINTF("ND option not supported in NA\n");
437  break;
438  }
439  nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
440  }
441  addr = uip_ds6_addr_lookup(&UIP_ND6_NA_BUF->tgtipaddr);
442  /* Message processing, including TLLAO if any */
443  if(addr != NULL) {
444  if(addr->state == ADDR_TENTATIVE) {
445  uip_ds6_dad_failed(addr);
446  }
447  PRINTF("NA received is bad\n");
448  goto discard;
449  } else {
450  nbr = uip_ds6_nbr_lookup(&UIP_ND6_NA_BUF->tgtipaddr);
451  if(nbr == NULL) {
452  goto discard;
453  }
454  if(nd6_opt_llao != 0) {
455  is_llchange =
456  memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], (void *)(&nbr->lladdr),
457  UIP_LLADDR_LEN);
458  }
459  if(nbr->state == NBR_INCOMPLETE) {
460  if(nd6_opt_llao == NULL) {
461  goto discard;
462  }
463  memcpy(&nbr->lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
464  UIP_LLADDR_LEN);
465  if(is_solicited) {
466  nbr->state = NBR_REACHABLE;
467  nbr->nscount = 0;
468 
469  /* reachable time is stored in ms */
470  stimer_set(&(nbr->reachable), uip_ds6_if.reachable_time / 1000);
471 
472  } else {
473  nbr->state = NBR_STALE;
474  }
475  nbr->isrouter = is_router;
476  } else {
477  if(!is_override && is_llchange) {
478  if(nbr->state == NBR_REACHABLE) {
479  nbr->state = NBR_STALE;
480  }
481  goto discard;
482  } else {
483  if(is_override || (!is_override && nd6_opt_llao != 0 && !is_llchange)
484  || nd6_opt_llao == 0) {
485  if(nd6_opt_llao != 0) {
486  memcpy(&nbr->lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
487  UIP_LLADDR_LEN);
488  }
489  if(is_solicited) {
490  nbr->state = NBR_REACHABLE;
491  /* reachable time is stored in ms */
492  stimer_set(&(nbr->reachable), uip_ds6_if.reachable_time / 1000);
493  } else {
494  if(nd6_opt_llao != 0 && is_llchange) {
495  nbr->state = NBR_STALE;
496  }
497  }
498  }
499  }
500  if(nbr->isrouter && !is_router) {
501  defrt = uip_ds6_defrt_lookup(&UIP_IP_BUF->srcipaddr);
502  if(defrt != NULL) {
503  uip_ds6_defrt_rm(defrt);
504  }
505  }
506  nbr->isrouter = is_router;
507  }
508  }
509 #if UIP_CONF_IPV6_QUEUE_PKT
510  /* The nbr is now reachable, check if we had buffered a pkt for it */
511  /*if(nbr->queue_buf_len != 0) {
512  uip_len = nbr->queue_buf_len;
513  memcpy(UIP_IP_BUF, nbr->queue_buf, uip_len);
514  nbr->queue_buf_len = 0;
515  return;
516  }*/
517  if(uip_packetqueue_buflen(&nbr->packethandle) != 0) {
518  uip_len = uip_packetqueue_buflen(&nbr->packethandle);
519  memcpy(UIP_IP_BUF, uip_packetqueue_buf(&nbr->packethandle), uip_len);
520  uip_packetqueue_free(&nbr->packethandle);
521  return;
522  }
523 
524 #endif /*UIP_CONF_IPV6_QUEUE_PKT */
525 
526 discard:
527  uip_len = 0;
528  return;
529 }
530 
531 
532 #if UIP_CONF_ROUTER
533 #if UIP_ND6_SEND_RA
534 /*---------------------------------------------------------------------------*/
535 void
536 uip_nd6_rs_input(void)
537 {
538 
539  PRINTF("Received RS from");
540  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
541  PRINTF("to");
542  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
543  PRINTF("\n");
544  UIP_STAT(++uip_stat.nd6.recv);
545 
546 
547 #if UIP_CONF_IPV6_CHECKS
548  /*
549  * Check hop limit / icmp code
550  * target address must not be multicast
551  * if the NA is solicited, dest must not be multicast
552  */
553  if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) || (UIP_ICMP_BUF->icode != 0)) {
554  PRINTF("RS received is bad\n");
555  goto discard;
556  }
557 #endif /*UIP_CONF_IPV6_CHECKS */
558 
559  /* Only valid option is Source Link-Layer Address option any thing
560  else is discarded */
561  nd6_opt_offset = UIP_ND6_RS_LEN;
562  nd6_opt_llao = NULL;
563 
564  while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
565 #if UIP_CONF_IPV6_CHECKS
566  if(UIP_ND6_OPT_HDR_BUF->len == 0) {
567  PRINTF("RS received is bad\n");
568  goto discard;
569  }
570 #endif /*UIP_CONF_IPV6_CHECKS */
571  switch (UIP_ND6_OPT_HDR_BUF->type) {
572  case UIP_ND6_OPT_SLLAO:
573  nd6_opt_llao = UIP_ND6_OPT_HDR_BUF;
574  break;
575  default:
576  PRINTF("ND option not supported in RS\n");
577  break;
578  }
579  nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
580  }
581  /* Options processing: only SLLAO */
582  if(nd6_opt_llao != NULL) {
583 #if UIP_CONF_IPV6_CHECKS
584  if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
585  PRINTF("RS received is bad\n");
586  goto discard;
587  } else {
588 #endif /*UIP_CONF_IPV6_CHECKS */
589  if((nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr)) == NULL) {
590  /* we need to add the neighbor */
591  uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr,
592  &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], 0, NBR_STALE);
593  } else {
594  /* If LL address changed, set neighbor state to stale */
595  if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
596  &nbr->lladdr, UIP_LLADDR_LEN) != 0) {
597  memcpy(&nbr->lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
598  UIP_LLADDR_LEN);
599  nbr->state = NBR_STALE;
600  }
601  nbr->isrouter = 0;
602  }
603 #if UIP_CONF_IPV6_CHECKS
604  }
605 #endif /*UIP_CONF_IPV6_CHECKS */
606  }
607 
608  /* Schedule a sollicited RA */
609  uip_ds6_send_ra_sollicited();
610 
611 discard:
612  uip_len = 0;
613  return;
614 }
615 
616 /*---------------------------------------------------------------------------*/
617 void
618 uip_nd6_ra_output(uip_ipaddr_t * dest)
619 {
620 
621  UIP_IP_BUF->vtc = 0x60;
622  UIP_IP_BUF->tcflow = 0;
623  UIP_IP_BUF->flow = 0;
624  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
626 
627  if(dest == NULL) {
628  uip_create_linklocal_allnodes_mcast(&UIP_IP_BUF->destipaddr);
629  } else {
630  /* For sollicited RA */
631  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, dest);
632  }
633  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
634 
635  UIP_ICMP_BUF->type = ICMP6_RA;
636  UIP_ICMP_BUF->icode = 0;
637 
638  UIP_ND6_RA_BUF->cur_ttl = uip_ds6_if.cur_hop_limit;
639 
640  UIP_ND6_RA_BUF->flags_reserved =
641  (UIP_ND6_M_FLAG << 7) | (UIP_ND6_O_FLAG << 6);
642 
643  UIP_ND6_RA_BUF->router_lifetime = uip_htons(UIP_ND6_ROUTER_LIFETIME);
644  //UIP_ND6_RA_BUF->reachable_time = uip_htonl(uip_ds6_if.reachable_time);
645  //UIP_ND6_RA_BUF->retrans_timer = uip_htonl(uip_ds6_if.retrans_timer);
646  UIP_ND6_RA_BUF->reachable_time = 0;
647  UIP_ND6_RA_BUF->retrans_timer = 0;
648 
649  uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_RA_LEN;
650  nd6_opt_offset = UIP_ND6_RA_LEN;
651 
652 
653  /* Prefix list */
654  for(prefix = uip_ds6_prefix_list;
655  prefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB; prefix++) {
656  if((prefix->isused) && (prefix->advertise)) {
657  UIP_ND6_OPT_PREFIX_BUF->type = UIP_ND6_OPT_PREFIX_INFO;
658  UIP_ND6_OPT_PREFIX_BUF->len = UIP_ND6_OPT_PREFIX_INFO_LEN / 8;
659  UIP_ND6_OPT_PREFIX_BUF->preflen = prefix->length;
660  UIP_ND6_OPT_PREFIX_BUF->flagsreserved1 = prefix->l_a_reserved;
661  UIP_ND6_OPT_PREFIX_BUF->validlt = uip_htonl(prefix->vlifetime);
662  UIP_ND6_OPT_PREFIX_BUF->preferredlt = uip_htonl(prefix->plifetime);
663  UIP_ND6_OPT_PREFIX_BUF->reserved2 = 0;
664  uip_ipaddr_copy(&(UIP_ND6_OPT_PREFIX_BUF->prefix), &(prefix->ipaddr));
665  nd6_opt_offset += UIP_ND6_OPT_PREFIX_INFO_LEN;
666  uip_len += UIP_ND6_OPT_PREFIX_INFO_LEN;
667  }
668  }
669 
670  /* Source link-layer option */
671  create_llao((uint8_t *)UIP_ND6_OPT_HDR_BUF, UIP_ND6_OPT_SLLAO);
672 
674  nd6_opt_offset += UIP_ND6_OPT_LLAO_LEN;
675 
676  /* MTU */
677  UIP_ND6_OPT_MTU_BUF->type = UIP_ND6_OPT_MTU;
678  UIP_ND6_OPT_MTU_BUF->len = UIP_ND6_OPT_MTU_LEN >> 3;
679  UIP_ND6_OPT_MTU_BUF->reserved = 0;
680  //UIP_ND6_OPT_MTU_BUF->mtu = uip_htonl(uip_ds6_if.link_mtu);
681  UIP_ND6_OPT_MTU_BUF->mtu = uip_htonl(1500);
682 
683  uip_len += UIP_ND6_OPT_MTU_LEN;
684  nd6_opt_offset += UIP_ND6_OPT_MTU_LEN;
685  UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
686  UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
687 
688  /*ICMP checksum */
689  UIP_ICMP_BUF->icmpchksum = 0;
690  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
691 
692  UIP_STAT(++uip_stat.nd6.sent);
693  PRINTF("Sending RA to");
694  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
695  PRINTF("from");
696  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
697  PRINTF("\n");
698  return;
699 }
700 #endif /* UIP_ND6_SEND_RA */
701 #endif /* UIP_CONF_ROUTER */
702 
703 #if !UIP_CONF_ROUTER
704 /*---------------------------------------------------------------------------*/
705 void
707 {
708  UIP_IP_BUF->vtc = 0x60;
709  UIP_IP_BUF->tcflow = 0;
710  UIP_IP_BUF->flow = 0;
711  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
713  uip_create_linklocal_allrouters_mcast(&UIP_IP_BUF->destipaddr);
714  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
715  UIP_ICMP_BUF->type = ICMP6_RS;
716  UIP_ICMP_BUF->icode = 0;
717  UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */
718 
719  if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
720  UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_RS_LEN;
721  uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN;
722  } else {
723  uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN;
724  UIP_IP_BUF->len[1] =
725  UIP_ICMPH_LEN + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN;
726 
727  create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_RS_LEN],
728  UIP_ND6_OPT_SLLAO);
729  }
730 
731  UIP_ICMP_BUF->icmpchksum = 0;
732  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
733 
734  UIP_STAT(++uip_stat.nd6.sent);
735  PRINTF("Sendin RS to");
736  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
737  PRINTF("from");
738  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
739  PRINTF("\n");
740  return;
741 }
742 
743 
744 /*---------------------------------------------------------------------------*/
745 void
747 {
748  PRINTF("Received RA from");
749  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
750  PRINTF("to");
751  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
752  PRINTF("\n");
753  UIP_STAT(++uip_stat.nd6.recv);
754 
755 #if UIP_CONF_IPV6_CHECKS
756  if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
757  (!uip_is_addr_link_local(&UIP_IP_BUF->srcipaddr)) ||
758  (UIP_ICMP_BUF->icode != 0)) {
759  PRINTF("RA received is bad");
760  goto discard;
761  }
762 #endif /*UIP_CONF_IPV6_CHECKS */
763 
764  if(UIP_ND6_RA_BUF->cur_ttl != 0) {
765  uip_ds6_if.cur_hop_limit = UIP_ND6_RA_BUF->cur_ttl;
766  PRINTF("uip_ds6_if.cur_hop_limit %u\n", uip_ds6_if.cur_hop_limit);
767  }
768 
769  if(UIP_ND6_RA_BUF->reachable_time != 0) {
770  if(uip_ds6_if.base_reachable_time !=
771  uip_ntohl(UIP_ND6_RA_BUF->reachable_time)) {
772  uip_ds6_if.base_reachable_time = uip_ntohl(UIP_ND6_RA_BUF->reachable_time);
773  uip_ds6_if.reachable_time = uip_ds6_compute_reachable_time();
774  }
775  }
776  if(UIP_ND6_RA_BUF->retrans_timer != 0) {
777  uip_ds6_if.retrans_timer = uip_ntohl(UIP_ND6_RA_BUF->retrans_timer);
778  }
779 
780  /* Options processing */
781  nd6_opt_offset = UIP_ND6_RA_LEN;
782  while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
783  if(UIP_ND6_OPT_HDR_BUF->len == 0) {
784  PRINTF("RA received is bad");
785  goto discard;
786  }
787  switch (UIP_ND6_OPT_HDR_BUF->type) {
788  case UIP_ND6_OPT_SLLAO:
789  PRINTF("Processing SLLAO option in RA\n");
790  nd6_opt_llao = (uint8_t *) UIP_ND6_OPT_HDR_BUF;
791  nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr);
792  if(nbr == NULL) {
793  nbr = uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr,
794  (uip_lladdr_t *)&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
795  1, NBR_STALE);
796  } else {
797  if(nbr->state == NBR_INCOMPLETE) {
798  nbr->state = NBR_STALE;
799  }
800  if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
801  &nbr->lladdr, UIP_LLADDR_LEN) != 0) {
802  memcpy(&nbr->lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
803  UIP_LLADDR_LEN);
804  nbr->state = NBR_STALE;
805  }
806  nbr->isrouter = 1;
807  }
808  break;
809  case UIP_ND6_OPT_MTU:
810  PRINTF("Processing MTU option in RA\n");
811  uip_ds6_if.link_mtu =
812  uip_ntohl(((uip_nd6_opt_mtu *) UIP_ND6_OPT_HDR_BUF)->mtu);
813  break;
814  case UIP_ND6_OPT_PREFIX_INFO:
815  PRINTF("Processing PREFIX option in RA\n");
816  nd6_opt_prefix_info = (uip_nd6_opt_prefix_info *) UIP_ND6_OPT_HDR_BUF;
817  if((uip_ntohl(nd6_opt_prefix_info->validlt) >=
818  uip_ntohl(nd6_opt_prefix_info->preferredlt))
819  && (!uip_is_addr_link_local(&nd6_opt_prefix_info->prefix))) {
820  /* on-link flag related processing */
821  if(nd6_opt_prefix_info->flagsreserved1 & UIP_ND6_RA_FLAG_ONLINK) {
822  prefix =
823  uip_ds6_prefix_lookup(&nd6_opt_prefix_info->prefix,
824  nd6_opt_prefix_info->preflen);
825  if(prefix == NULL) {
826  if(nd6_opt_prefix_info->validlt != 0) {
827  if(nd6_opt_prefix_info->validlt != UIP_ND6_INFINITE_LIFETIME) {
828  prefix = uip_ds6_prefix_add(&nd6_opt_prefix_info->prefix,
829  nd6_opt_prefix_info->preflen,
830  uip_ntohl(nd6_opt_prefix_info->
831  validlt));
832  } else {
833  prefix = uip_ds6_prefix_add(&nd6_opt_prefix_info->prefix,
834  nd6_opt_prefix_info->preflen, 0);
835  }
836  }
837  } else {
838  switch (nd6_opt_prefix_info->validlt) {
839  case 0:
840  uip_ds6_prefix_rm(prefix);
841  break;
843  prefix->isinfinite = 1;
844  break;
845  default:
846  PRINTF("Updating timer of prefix");
847  PRINT6ADDR(&prefix->ipaddr);
848  PRINTF("new value %lu\n", uip_ntohl(nd6_opt_prefix_info->validlt));
849  stimer_set(&prefix->vlifetime,
850  uip_ntohl(nd6_opt_prefix_info->validlt));
851  prefix->isinfinite = 0;
852  break;
853  }
854  }
855  }
856  /* End of on-link flag related processing */
857  /* autonomous flag related processing */
858  if((nd6_opt_prefix_info->flagsreserved1 & UIP_ND6_RA_FLAG_AUTONOMOUS)
859  && (nd6_opt_prefix_info->validlt != 0)
860  && (nd6_opt_prefix_info->preflen == UIP_DEFAULT_PREFIX_LEN)) {
861 
862  uip_ipaddr_copy(&ipaddr, &nd6_opt_prefix_info->prefix);
863  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
864  addr = uip_ds6_addr_lookup(&ipaddr);
865  if((addr != NULL) && (addr->type == ADDR_AUTOCONF)) {
866  if(nd6_opt_prefix_info->validlt != UIP_ND6_INFINITE_LIFETIME) {
867  /* The processing below is defined in RFC4862 section 5.5.3 e */
868  if((uip_ntohl(nd6_opt_prefix_info->validlt) > 2 * 60 * 60) ||
869  (uip_ntohl(nd6_opt_prefix_info->validlt) >
870  stimer_remaining(&addr->vlifetime))) {
871  PRINTF("Updating timer of address");
872  PRINT6ADDR(&addr->ipaddr);
873  PRINTF("new value %lu\n",
874  uip_ntohl(nd6_opt_prefix_info->validlt));
875  stimer_set(&addr->vlifetime,
876  uip_ntohl(nd6_opt_prefix_info->validlt));
877  } else {
878  stimer_set(&addr->vlifetime, 2 * 60 * 60);
879  PRINTF("Updating timer of address ");
880  PRINT6ADDR(&addr->ipaddr);
881  PRINTF("new value %lu\n", (unsigned long)(2 * 60 * 60));
882  }
883  addr->isinfinite = 0;
884  } else {
885  addr->isinfinite = 1;
886  }
887  } else {
888  if(uip_ntohl(nd6_opt_prefix_info->validlt) ==
890  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
891  } else {
892  uip_ds6_addr_add(&ipaddr, uip_ntohl(nd6_opt_prefix_info->validlt),
893  ADDR_AUTOCONF);
894  }
895  }
896  }
897  /* End of autonomous flag related processing */
898  }
899  break;
900  default:
901  PRINTF("ND option not supported in RA");
902  break;
903  }
904  nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
905  }
906 
907  defrt = uip_ds6_defrt_lookup(&UIP_IP_BUF->srcipaddr);
908  if(UIP_ND6_RA_BUF->router_lifetime != 0) {
909  if(nbr != NULL) {
910  nbr->isrouter = 1;
911  }
912  if(defrt == NULL) {
913  uip_ds6_defrt_add(&UIP_IP_BUF->srcipaddr,
914  (unsigned
915  long)(uip_ntohs(UIP_ND6_RA_BUF->router_lifetime)));
916  } else {
917  stimer_set(&(defrt->lifetime),
918  (unsigned long)(uip_ntohs(UIP_ND6_RA_BUF->router_lifetime)));
919  }
920  } else {
921  if(defrt != NULL) {
922  uip_ds6_defrt_rm(defrt);
923  }
924  }
925 
926 #if UIP_CONF_IPV6_QUEUE_PKT
927  /* If the nbr just became reachable (e.g. it was in NBR_INCOMPLETE state
928  * and we got a SLLAO), check if we had buffered a pkt for it */
929  /* if((nbr != NULL) && (nbr->queue_buf_len != 0)) {
930  uip_len = nbr->queue_buf_len;
931  memcpy(UIP_IP_BUF, nbr->queue_buf, uip_len);
932  nbr->queue_buf_len = 0;
933  return;
934  }*/
935  if(nbr != NULL && uip_packetqueue_buflen(&nbr->packethandle) != 0) {
936  uip_len = uip_packetqueue_buflen(&nbr->packethandle);
937  memcpy(UIP_IP_BUF, uip_packetqueue_buf(&nbr->packethandle), uip_len);
938  uip_packetqueue_free(&nbr->packethandle);
939  return;
940  }
941 
942 #endif /*UIP_CONF_IPV6_QUEUE_PKT */
943 
944 discard:
945  uip_len = 0;
946  return;
947 }
948 #endif /* !UIP_CONF_ROUTER */
949 
950  /** @} */