Contiki-NG
Loading...
Searching...
No Matches
uip-nd6.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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 project 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 PROJECT 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 PROJECT 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/*
30 * Copyright (c) 2006, Swedish Institute of Computer Science.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the Institute nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 */
58
59/**
60 * \addtogroup uip
61 * @{
62 */
63
64/**
65 * \file
66 * Neighbor discovery (RFC 4861)
67 * \author Mathilde Durvy <mdurvy@cisco.com>
68 * \author Julien Abeille <jabeille@cisco.com>
69 */
70
71#include <string.h>
72#include <inttypes.h>
73#include "net/ipv6/uip-icmp6.h"
74#include "net/ipv6/uip-nd6.h"
75#include "net/ipv6/uip-ds6.h"
77#include "lib/random.h"
78
79/* Log configuration */
80#include "sys/log.h"
81#define LOG_MODULE "IPv6 NDP"
82#define LOG_LEVEL LOG_LEVEL_IPV6
83
84/*------------------------------------------------------------------*/
85/** @{ */
86/** \name Pointers to the header structures.
87 */
88
89/**@{ Pointers to messages just after icmp header */
90#define UIP_ND6_RS_BUF ((uip_nd6_rs *)UIP_ICMP_PAYLOAD)
91#define UIP_ND6_RA_BUF ((uip_nd6_ra *)UIP_ICMP_PAYLOAD)
92#define UIP_ND6_NS_BUF ((uip_nd6_ns *)UIP_ICMP_PAYLOAD)
93#define UIP_ND6_NA_BUF ((uip_nd6_na *)UIP_ICMP_PAYLOAD)
94/** @} */
95/** Pointer to ND option */
96#define ND6_OPT(opt) ((unsigned char *)(UIP_ICMP_PAYLOAD + (opt)))
97#define ND6_OPT_HDR_BUF(opt) ((uip_nd6_opt_hdr *)ND6_OPT(opt))
98#define ND6_OPT_PREFIX_BUF(opt) ((uip_nd6_opt_prefix_info *)ND6_OPT(opt))
99#define ND6_OPT_MTU_BUF(opt) ((uip_nd6_opt_mtu *)ND6_OPT(opt))
100#define ND6_OPT_RDNSS_BUF(opt) ((uip_nd6_opt_dns *)ND6_OPT(opt))
101/** @} */
102
103#if UIP_ND6_SEND_NS || UIP_ND6_SEND_NA || UIP_ND6_SEND_RA || !UIP_CONF_ROUTER
104static uint16_t nd6_opt_offset; /** Offset from the end of the icmpv6 header to the option in uip_buf*/
105static uint8_t *nd6_opt_llao; /** Pointer to llao option in uip_buf */
106static uip_ds6_nbr_t *nbr; /** Pointer to a nbr cache entry*/
107static uip_ds6_addr_t *addr; /** Pointer to an interface address */
108#endif /* UIP_ND6_SEND_NS || UIP_ND6_SEND_NA || UIP_ND6_SEND_RA || !UIP_CONF_ROUTER */
109
110#if UIP_ND6_SEND_NS || UIP_ND6_SEND_RA || !UIP_CONF_ROUTER
111static uip_ds6_defrt_t *defrt; /** Pointer to a router list entry */
112#endif /* UIP_ND6_SEND_NS || UIP_ND6_SEND_RA || !UIP_CONF_ROUTER */
113
114#if !UIP_CONF_ROUTER /* TBD see if we move it to ra_input */
115static uip_nd6_opt_prefix_info *nd6_opt_prefix_info; /** Pointer to prefix information option in uip_buf */
116static uip_ipaddr_t ipaddr;
117#endif
118#if (!UIP_CONF_ROUTER || UIP_ND6_SEND_RA)
119static uip_ds6_prefix_t *prefix; /** Pointer to a prefix list entry */
120#endif
121
122#if UIP_ND6_SEND_NA || UIP_ND6_SEND_RA || !UIP_CONF_ROUTER
123/*------------------------------------------------------------------*/
124/* Copy link-layer address from LLAO option to a word-aligned uip_lladdr_t */
125static int
127{
128 if(dest != NULL && nd6_opt_llao != NULL) {
129 memcpy(dest, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], UIP_LLADDR_LEN);
130 return 1;
131 }
132 return 0;
133}
134#endif /* UIP_ND6_SEND_NA || UIP_ND6_SEND_RA || !UIP_CONF_ROUTER */
135/*------------------------------------------------------------------*/
136#if UIP_ND6_SEND_NA /* UIP_ND6_SEND_NA */
137/* create a llao */
138static void
139create_llao(uint8_t *llao, uint8_t type)
140{
141 llao[UIP_ND6_OPT_TYPE_OFFSET] = type;
142 llao[UIP_ND6_OPT_LEN_OFFSET] = UIP_ND6_OPT_LLAO_LEN >> 3;
143 memcpy(&llao[UIP_ND6_OPT_DATA_OFFSET], &uip_lladdr, UIP_LLADDR_LEN);
144 /* padding on some */
145 memset(&llao[UIP_ND6_OPT_DATA_OFFSET + UIP_LLADDR_LEN], 0,
147}
148#endif /* UIP_ND6_SEND_NA */
149/*------------------------------------------------------------------*/
150/**
151 * Neighbor Solicitation Processing
152 *
153 * The NS can be received in 3 cases (procedures):
154 * - sender is performing DAD (ip src = unspecified, no SLLAO option)
155 * - sender is performing NUD (ip dst = unicast)
156 * - sender is performing address resolution (ip dest = solicited node mcast
157 * address)
158 *
159 * We do:
160 * - if the tgt belongs to me, reply, otherwise ignore
161 * - if i was performing DAD for the same address, two cases:
162 * -- I already sent a NS, hence I win
163 * -- I did not send a NS yet, hence I lose
164 *
165 * If we need to send a NA in response (i.e. the NS was done for NUD, or
166 * address resolution, or DAD and there is a conflict), we do it in this
167 * function: set src, dst, tgt address in the three cases, then for all cases
168 * set the rest, including SLLAO
169 *
170 */
171#if UIP_ND6_SEND_NA
172static void
173ns_input(void)
174{
175 if(uip_l3_icmp_hdr_len + sizeof(uip_nd6_ns) > uip_len) {
176 LOG_ERR("Insufficient data for reading ND6 NS header fields");
177 goto discard;
178 }
179
180 uint8_t flags = 0;
181
182 LOG_INFO("Received NS from ");
183 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
184 LOG_INFO_(" to ");
185 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
186 LOG_INFO_(" with target address ");
187 LOG_INFO_6ADDR((uip_ipaddr_t *)(&UIP_ND6_NS_BUF->tgtipaddr));
188 LOG_INFO_("\n");
189 UIP_STAT(++uip_stat.nd6.recv);
190
191 if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
192 (uip_is_addr_mcast(&UIP_ND6_NS_BUF->tgtipaddr)) ||
193 (UIP_ICMP_BUF->icode != 0)) {
194 LOG_ERR("Discarding invalid NS\n");
195 goto discard;
196 }
197
198 /* Options processing */
199 nd6_opt_llao = NULL;
200 nd6_opt_offset = UIP_ND6_NS_LEN;
201 while(uip_l3_icmp_hdr_len + nd6_opt_offset + UIP_ND6_OPT_HDR_LEN < uip_len) {
202 if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) {
203 LOG_ERR("Discarding invalid NS\n");
204 goto discard;
205 }
206
207 switch(ND6_OPT_HDR_BUF(nd6_opt_offset)->type) {
208 case UIP_ND6_OPT_SLLAO:
209 if(uip_l3_icmp_hdr_len + nd6_opt_offset +
210 UIP_ND6_OPT_DATA_OFFSET + UIP_LLADDR_LEN > uip_len) {
211 LOG_ERR("Insufficient data for NS SLLAO option\n");
212 goto discard;
213 }
214 nd6_opt_llao = &uip_buf[uip_l3_icmp_hdr_len + nd6_opt_offset];
215
216 /* There must be NO option in a DAD NS */
217 if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
218 LOG_ERR("Discarding invalid NS\n");
219 goto discard;
220 } else {
221 uip_lladdr_t lladdr_aligned;
222 extract_lladdr_from_llao_aligned(&lladdr_aligned);
223 nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr);
224 if(nbr == NULL) {
225 uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr, &lladdr_aligned,
226 0, NBR_STALE, NBR_TABLE_REASON_IPV6_ND, NULL);
227 } else {
228 const uip_lladdr_t *lladdr = uip_ds6_nbr_get_ll(nbr);
229 if(lladdr == NULL) {
230 goto discard;
231 }
232 if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
233 lladdr, UIP_LLADDR_LEN) != 0) {
235 (const uip_lladdr_t *)&lladdr_aligned)
236 < 0) {
237 /* failed to update the lladdr */
238 goto discard;
239 }
240 nbr->state = NBR_STALE;
241 } else {
242 if(nbr->state == NBR_INCOMPLETE) {
243 nbr->state = NBR_STALE;
244 }
245 }
246 }
247 }
248 break;
249 default:
250 LOG_WARN("ND option not supported in NS");
251 break;
252 }
253 nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3);
254 }
255
256 addr = uip_ds6_addr_lookup(&UIP_ND6_NS_BUF->tgtipaddr);
257 if(addr != NULL) {
258 if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
259 /* DAD CASE */
260#if UIP_ND6_DEF_MAXDADNS > 0
261 if(!uip_is_addr_solicited_node(&UIP_IP_BUF->destipaddr)) {
262 LOG_ERR("Discarding invalid NS\n");
263 goto discard;
264 }
265
266 if(addr->state != ADDR_TENTATIVE) {
268 uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
269 flags = UIP_ND6_NA_FLAG_OVERRIDE;
270 goto create_na;
271 } else {
272 /** \todo if I sent a NS before him, I win */
274 goto discard;
275 }
276#else /* UIP_ND6_DEF_MAXDADNS > 0 */
277 goto discard; /* DAD CASE */
278#endif /* UIP_ND6_DEF_MAXDADNS > 0 */
279 }
280
281 if(uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr)) {
282 /**
283 * \NOTE do we do something here? we both are using the same address.
284 * If we are doing dad, we could cancel it, though we should receive a
285 * NA in response of DAD NS we sent, hence DAD will fail anyway. If we
286 * were not doing DAD, it means there is a duplicate in the network!
287 */
288 LOG_ERR("Discarding invalid NS\n");
289 goto discard;
290 }
291
292 /* Address resolution case */
293 if(uip_is_addr_solicited_node(&UIP_IP_BUF->destipaddr)) {
294 uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
295 uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_ND6_NS_BUF->tgtipaddr);
296 flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
297 goto create_na;
298 }
299
300 /* NUD CASE */
301 if(uip_ds6_addr_lookup(&UIP_IP_BUF->destipaddr) == addr) {
302 uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
303 uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_ND6_NS_BUF->tgtipaddr);
304 flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
305 goto create_na;
306 } else {
307 LOG_ERR("Discarding invalid NS\n");
308 goto discard;
309 }
310 } else {
311 goto discard;
312 }
313
314create_na:
315 /* If the node is a router it should set R flag in NAs */
316#if UIP_CONF_ROUTER
317 flags = flags | UIP_ND6_NA_FLAG_ROUTER;
318#endif
319 uipbuf_clear();
320 UIP_IP_BUF->vtc = 0x60;
321 UIP_IP_BUF->tcflow = 0;
322 UIP_IP_BUF->flow = 0;
323 uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN);
324 UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
326
327 UIP_ICMP_BUF->type = ICMP6_NA;
328 UIP_ICMP_BUF->icode = 0;
329
330 UIP_ND6_NA_BUF->flagsreserved = flags;
331 memcpy(&UIP_ND6_NA_BUF->tgtipaddr, &addr->ipaddr, sizeof(uip_ipaddr_t));
332
333 create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_NA_LEN],
334 UIP_ND6_OPT_TLLAO);
335
336 UIP_ICMP_BUF->icmpchksum = 0;
337 UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
338
339 uipbuf_set_len(UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN);
340
341 UIP_STAT(++uip_stat.nd6.sent);
342 LOG_INFO("Sending NA to ");
343 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
344 LOG_INFO_(" from ");
345 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
346 LOG_INFO_(" with target address ");
347 LOG_INFO_6ADDR(&UIP_ND6_NA_BUF->tgtipaddr);
348 LOG_INFO_("\n");
349 return;
350
351discard:
352 uipbuf_clear();
353 return;
354}
355#endif /* UIP_ND6_SEND_NA */
356
357/*------------------------------------------------------------------*/
358#if UIP_ND6_SEND_NS
359void
360uip_nd6_ns_output(const uip_ipaddr_t *src, const uip_ipaddr_t *dest,
361 uip_ipaddr_t *tgt)
362{
363 uipbuf_clear();
364 UIP_IP_BUF->vtc = 0x60;
365 UIP_IP_BUF->tcflow = 0;
366 UIP_IP_BUF->flow = 0;
367 UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
369
370 if(dest == NULL) {
371 uip_create_solicited_node(tgt, &UIP_IP_BUF->destipaddr);
372 } else {
373 uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, dest);
374 }
375 UIP_ICMP_BUF->type = ICMP6_NS;
376 UIP_ICMP_BUF->icode = 0;
377 UIP_ND6_NS_BUF->reserved = 0;
378 uip_ipaddr_copy((uip_ipaddr_t *)&UIP_ND6_NS_BUF->tgtipaddr, tgt);
379 /*
380 * check if we add a SLLAO option: for DAD, MUST NOT, for NUD, MAY
381 * (here yes), for Address resolution , MUST
382 */
383 if(!(uip_ds6_is_my_addr(tgt))) {
384 if(src != NULL) {
385 uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, src);
386 } else {
387 uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
388 }
389 if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
390 LOG_ERR("Dropping NS due to no suitable source address\n");
391 uipbuf_clear();
392 return;
393 }
394 uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN);
395
396 create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_NS_LEN],
397 UIP_ND6_OPT_SLLAO);
398
399 uip_len =
400 UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN;
401 } else {
403 UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NS_LEN;
404 uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NS_LEN;
405 }
406
407 UIP_ICMP_BUF->icmpchksum = 0;
408 UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
409
410 UIP_STAT(++uip_stat.nd6.sent);
411 LOG_INFO("Sending NS to ");
412 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
413 LOG_INFO_(" from ");
414 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
415 LOG_INFO_(" with target address ");
416 LOG_INFO_6ADDR(tgt);
417 LOG_INFO_("\n");
418 return;
419}
420#endif /* UIP_ND6_SEND_NS */
421
422#if UIP_ND6_SEND_NS
423/*------------------------------------------------------------------*/
424/**
425 * Neighbor Advertisement Processing
426 *
427 * we might have to send a pkt that had been buffered while address
428 * resolution was performed (if we support buffering, see UIP_CONF_QUEUE_PKT)
429 *
430 * As per RFC 4861, on link layer that have addresses, TLLAO options MUST be
431 * included when responding to multicast solicitations, SHOULD be included in
432 * response to unicast (here we assume it is for now)
433 *
434 * NA can be received after sending NS for DAD, Address resolution or NUD. Can
435 * be unsolicited as well.
436 * It can trigger update of the state of the neighbor in the neighbor cache,
437 * router in the router list.
438 * If the NS was for DAD, it means DAD failed
439 *
440 */
441static void
442na_input(void)
443{
444 uint8_t is_llchange;
445 uint8_t is_router;
446 uint8_t is_solicited;
447 uint8_t is_override;
448 uip_lladdr_t lladdr_aligned;
449
450 LOG_INFO("Received NA from ");
451 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
452 LOG_INFO_(" to ");
453 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
454 LOG_INFO_(" with target address ");
455 LOG_INFO_6ADDR((uip_ipaddr_t *)(&UIP_ND6_NA_BUF->tgtipaddr));
456 LOG_INFO_("\n");
457 UIP_STAT(++uip_stat.nd6.recv);
458
459 /*
460 * booleans. the three last one are not 0 or 1 but 0 or 0x80, 0x40, 0x20
461 * but it works. Be careful though, do not use tests such as is_router == 1
462 */
463 is_llchange = 0;
464 is_router = ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_ROUTER));
465 is_solicited =
466 ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_SOLICITED));
467 is_override =
468 ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_OVERRIDE));
469
470 if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
471 (UIP_ICMP_BUF->icode != 0) ||
472 (uip_is_addr_mcast(&UIP_ND6_NA_BUF->tgtipaddr)) ||
473 (is_solicited && uip_is_addr_mcast(&UIP_IP_BUF->destipaddr))) {
474 LOG_ERR("Discarding invalid NA\n");
475 goto discard;
476 }
477
478 /* Options processing: we handle TLLAO, and must ignore others */
479 nd6_opt_offset = UIP_ND6_NA_LEN;
480 nd6_opt_llao = NULL;
481 while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
482 if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) {
483 LOG_ERR("Discarding invalid NA\n");
484 goto discard;
485 }
486
487 switch(ND6_OPT_HDR_BUF(nd6_opt_offset)->type) {
488 case UIP_ND6_OPT_TLLAO:
489 nd6_opt_llao = (uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset);
490 break;
491 default:
492 LOG_WARN("ND option not supported in NA\n");
493 break;
494 }
495 nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3);
496 }
497 addr = uip_ds6_addr_lookup(&UIP_ND6_NA_BUF->tgtipaddr);
498 /* Message processing, including TLLAO if any */
499 if(addr != NULL) {
500#if UIP_ND6_DEF_MAXDADNS > 0
501 if(addr->state == ADDR_TENTATIVE) {
503 }
504#endif /*UIP_ND6_DEF_MAXDADNS > 0 */
505 LOG_ERR("Discarding invalid NA\n");
506 goto discard;
507 } else {
508 const uip_lladdr_t *lladdr;
509 nbr = uip_ds6_nbr_lookup(&UIP_ND6_NA_BUF->tgtipaddr);
510 if(nbr == NULL) {
511 goto discard;
512 }
513 lladdr = uip_ds6_nbr_get_ll(nbr);
514 if(lladdr == NULL) {
515 goto discard;
516 }
517 if(nd6_opt_llao != NULL) {
518 is_llchange =
519 memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], lladdr,
520 UIP_LLADDR_LEN) == 0 ? 0 : 1;
521 }
522 if(nbr->state == NBR_INCOMPLETE) {
523 if(nd6_opt_llao == NULL || !extract_lladdr_from_llao_aligned(&lladdr_aligned)) {
524 goto discard;
525 }
527 (const uip_lladdr_t *)&lladdr_aligned) < 0) {
528 /* failed to update the lladdr */
529 goto discard;
530 }
531
532 /* Note: No need to refresh the state of the nbr here.
533 * It has already been refreshed upon receiving the unicast IPv6 ND packet.
534 * See: uip_ds6_nbr_refresh_reachable_state()
535 */
536 if(!is_solicited) {
537 nbr->state = NBR_STALE;
538 }
539 nbr->isrouter = is_router;
540 } else { /* NBR is not INCOMPLETE */
541 if(!is_override && is_llchange) {
542 if(nbr->state == NBR_REACHABLE) {
543 nbr->state = NBR_STALE;
544 }
545 goto discard;
546 } else {
547 /**
548 * If this is an cache override, or same lladdr, or no llao -
549 * do updates of nbr states.
550 */
551 if(is_override || !is_llchange || nd6_opt_llao == NULL) {
552 if(nd6_opt_llao != NULL && is_llchange) {
553 if(!extract_lladdr_from_llao_aligned(&lladdr_aligned) ||
555 (const uip_lladdr_t *)&lladdr_aligned)
556 < 0) {
557 /* failed to update the lladdr */
558 goto discard;
559 }
560 }
561 /* Note: No need to refresh the state of the nbr here.
562 * It has already been refreshed upon receiving the unicast IPv6 ND packet.
563 * See: uip_ds6_nbr_refresh_reachable_state()
564 */
565 }
566 }
567 if(nbr->isrouter && !is_router) {
568 defrt = uip_ds6_defrt_lookup(&UIP_IP_BUF->srcipaddr);
569 if(defrt != NULL) {
570 uip_ds6_defrt_rm(defrt);
571 }
572 }
573 nbr->isrouter = is_router;
574 }
575 }
576#if UIP_CONF_IPV6_QUEUE_PKT
577 /* The nbr is now reachable, check if we had buffered a pkt for it */
578 if(uip_packetqueue_buflen(&nbr->packethandle) != 0) {
579 uip_len = uip_packetqueue_buflen(&nbr->packethandle);
580 memcpy(UIP_IP_BUF, uip_packetqueue_buf(&nbr->packethandle), uip_len);
581 uip_packetqueue_free(&nbr->packethandle);
582 return;
583 }
584
585#endif /*UIP_CONF_IPV6_QUEUE_PKT */
586
587discard:
588 uipbuf_clear();
589 return;
590}
591#endif /* UIP_ND6_SEND_NS */
592
593#if UIP_CONF_ROUTER
594#if UIP_ND6_SEND_RA
595/*---------------------------------------------------------------------------*/
596static void
597rs_input(void)
598{
599
600 LOG_INFO("Received RS from ");
601 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
602 LOG_INFO_(" to ");
603 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
604 LOG_INFO_("\n");
605 UIP_STAT(++uip_stat.nd6.recv);
606
607 /*
608 * Check hop limit / icmp code
609 * target address must not be multicast
610 * if the NA is solicited, dest must not be multicast
611 */
612 if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) || (UIP_ICMP_BUF->icode != 0)) {
613 LOG_ERR("Discarding invalid RS\n");
614 goto discard;
615 }
616
617 /* Only valid option is Source Link-Layer Address option any thing
618 else is discarded */
619 nd6_opt_offset = UIP_ND6_RS_LEN;
620 nd6_opt_llao = NULL;
621
622 while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
623 if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) {
624 LOG_ERR("Discarding invalid RS\n");
625 goto discard;
626 }
627
628 switch(ND6_OPT_HDR_BUF(nd6_opt_offset)->type) {
629 case UIP_ND6_OPT_SLLAO:
630 nd6_opt_llao = (uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset);
631 break;
632 default:
633 LOG_WARN("ND option not supported in RS\n");
634 break;
635 }
636 nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3);
637 }
638 /* Options processing: only SLLAO */
639 if(nd6_opt_llao != NULL) {
640 if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
641 LOG_ERR("Discarding invalid RS\n");
642 goto discard;
643 } else {
644 uip_lladdr_t lladdr_aligned;
645 extract_lladdr_from_llao_aligned(&lladdr_aligned);
646 if((nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr)) == NULL) {
647 /* we need to add the neighbor */
648 uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr, &lladdr_aligned,
649 0, NBR_STALE, NBR_TABLE_REASON_IPV6_ND, NULL);
650 } else {
651 /* If LL address changed, set neighbor state to stale */
652 const uip_lladdr_t *lladdr = uip_ds6_nbr_get_ll(nbr);
653 if(lladdr == NULL) {
654 goto discard;
655 }
656 if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
657 lladdr, UIP_LLADDR_LEN) != 0) {
658 uip_ds6_nbr_t nbr_data;
659 nbr_data = *nbr;
661 nbr = uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr, &lladdr_aligned,
662 0, NBR_STALE, NBR_TABLE_REASON_IPV6_ND, NULL);
663 if(nbr == NULL) {
664 goto discard;
665 }
666 nbr->reachable = nbr_data.reachable;
667 nbr->sendns = nbr_data.sendns;
668 nbr->nscount = nbr_data.nscount;
669 }
670 nbr->isrouter = 0;
671 }
672 }
673 }
674
675 /* Schedule a sollicited RA */
676 uip_ds6_send_ra_sollicited();
677
678discard:
679 uipbuf_clear();
680 return;
681}
682/*---------------------------------------------------------------------------*/
683void
684uip_nd6_ra_output(const uip_ipaddr_t *dest)
685{
686
687 UIP_IP_BUF->vtc = 0x60;
688 UIP_IP_BUF->tcflow = 0;
689 UIP_IP_BUF->flow = 0;
690 UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
692
693 if(dest == NULL) {
695 } else {
696 /* For sollicited RA */
697 uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, dest);
698 }
699 uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
700
701 UIP_ICMP_BUF->type = ICMP6_RA;
702 UIP_ICMP_BUF->icode = 0;
703
704 UIP_ND6_RA_BUF->cur_ttl = uip_ds6_if.cur_hop_limit;
705
706 UIP_ND6_RA_BUF->flags_reserved =
707 (UIP_ND6_M_FLAG << 7) | (UIP_ND6_O_FLAG << 6);
708
709 UIP_ND6_RA_BUF->router_lifetime = uip_htons(UIP_ND6_ROUTER_LIFETIME);
710 UIP_ND6_RA_BUF->reachable_time = 0;
711 UIP_ND6_RA_BUF->retrans_timer = 0;
712
713 uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_RA_LEN;
714 nd6_opt_offset = UIP_ND6_RA_LEN;
715
716 /* Prefix list */
717 for(prefix = uip_ds6_prefix_list;
718 prefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB; prefix++) {
719 if((prefix->isused) && (prefix->advertise)) {
720 ND6_OPT_PREFIX_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_PREFIX_INFO;
721 ND6_OPT_PREFIX_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_PREFIX_INFO_LEN / 8;
722 ND6_OPT_PREFIX_BUF(nd6_opt_offset)->preflen = prefix->length;
723 ND6_OPT_PREFIX_BUF(nd6_opt_offset)->flagsreserved1 = prefix->l_a_reserved;
724 ND6_OPT_PREFIX_BUF(nd6_opt_offset)->validlt = uip_htonl(prefix->vlifetime);
725 ND6_OPT_PREFIX_BUF(nd6_opt_offset)->preferredlt = uip_htonl(prefix->plifetime);
726 ND6_OPT_PREFIX_BUF(nd6_opt_offset)->reserved2 = 0;
727 uip_ipaddr_copy(&(ND6_OPT_PREFIX_BUF(nd6_opt_offset)->prefix), &(prefix->ipaddr));
728 nd6_opt_offset += UIP_ND6_OPT_PREFIX_INFO_LEN;
729 uip_len += UIP_ND6_OPT_PREFIX_INFO_LEN;
730 }
731 }
732
733 /* Source link-layer option */
734 create_llao((uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset), UIP_ND6_OPT_SLLAO);
735
737 nd6_opt_offset += UIP_ND6_OPT_LLAO_LEN;
738
739 /* MTU */
740 ND6_OPT_MTU_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_MTU;
741 ND6_OPT_MTU_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_MTU_LEN >> 3;
742 ND6_OPT_MTU_BUF(nd6_opt_offset)->reserved = 0;
743 ND6_OPT_MTU_BUF(nd6_opt_offset)->mtu = uip_htonl(1500);
744
745 uip_len += UIP_ND6_OPT_MTU_LEN;
746 nd6_opt_offset += UIP_ND6_OPT_MTU_LEN;
747
748#if UIP_ND6_RA_RDNSS
749 if(uip_nameserver_count() > 0) {
750 uint8_t i = 0;
751 uip_ipaddr_t *ip = &ND6_OPT_RDNSS_BUF(nd6_opt_offset)->ip;
752 uip_ipaddr_t *dns = NULL;
753 ND6_OPT_RDNSS_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_RDNSS;
754 ND6_OPT_RDNSS_BUF(nd6_opt_offset)->reserved = 0;
755 ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime = uip_nameserver_next_expiration();
756 if(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime != UIP_NAMESERVER_INFINITE_LIFETIME) {
757 ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime -= clock_seconds();
758 }
759 while((dns = uip_nameserver_get(i)) != NULL) {
760 uip_ipaddr_copy(ip++, dns);
761 i++;
762 }
763 ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_RDNSS_LEN + (i << 1);
764 LOG_INFO("%d nameservers reported\n", i);
765 uip_len += ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len << 3;
766 nd6_opt_offset += ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len << 3;
767 }
768#endif /* UIP_ND6_RA_RDNSS */
769
770 uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN);
771
772 /*ICMP checksum */
773 UIP_ICMP_BUF->icmpchksum = 0;
774 UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
775
776 UIP_STAT(++uip_stat.nd6.sent);
777 LOG_INFO("Sending RA to ");
778 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
779 LOG_INFO_(" from ");
780 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
781 LOG_INFO_("\n");
782 return;
783}
784#endif /* UIP_ND6_SEND_RA */
785#endif /* UIP_CONF_ROUTER */
786
787#if !UIP_CONF_ROUTER
788/*---------------------------------------------------------------------------*/
789void
791{
792 UIP_IP_BUF->vtc = 0x60;
793 UIP_IP_BUF->tcflow = 0;
794 UIP_IP_BUF->flow = 0;
795 UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
798 uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
799 UIP_ICMP_BUF->type = ICMP6_RS;
800 UIP_ICMP_BUF->icode = 0;
801
802 if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
803 UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_RS_LEN;
804 uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN;
805 } else {
806 uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN;
807 uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN);
808
809 create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN],
810 UIP_ND6_OPT_SLLAO);
811 }
812
813 UIP_ICMP_BUF->icmpchksum = 0;
814 UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
815
816 UIP_STAT(++uip_stat.nd6.sent);
817 LOG_INFO("Sending RS to ");
818 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
819 LOG_INFO_(" from ");
820 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
821 LOG_INFO_("\n");
822 return;
823}
824/*---------------------------------------------------------------------------*/
825/**
826 * Process a Router Advertisement
827 *
828 * - Possible actions when receiving a RA: add router to router list,
829 * recalculate reachable time, update link hop limit, update retrans timer.
830 * - If MTU option: update MTU.
831 * - If SLLAO option: update entry in neighbor cache
832 * - If prefix option: start autoconf, add prefix to prefix list
833 */
834void
836{
837 uip_lladdr_t lladdr_aligned;
838
839 LOG_INFO("Received RA from ");
840 LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
841 LOG_INFO_(" to ");
842 LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
843 LOG_INFO_("\n");
844 UIP_STAT(++uip_stat.nd6.recv);
845
846 if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
847 (!uip_is_addr_linklocal(&UIP_IP_BUF->srcipaddr)) ||
848 (UIP_ICMP_BUF->icode != 0)) {
849 LOG_ERR("Discarding invalid RA");
850 goto discard;
851 }
852
853 if(UIP_ND6_RA_BUF->cur_ttl != 0) {
854 uip_ds6_if.cur_hop_limit = UIP_ND6_RA_BUF->cur_ttl;
855 LOG_INFO("uip_ds6_if.cur_hop_limit %u\n", uip_ds6_if.cur_hop_limit);
856 }
857
858 if(UIP_ND6_RA_BUF->reachable_time != 0) {
859 if(uip_ds6_if.base_reachable_time !=
860 uip_ntohl(UIP_ND6_RA_BUF->reachable_time)) {
861 uip_ds6_if.base_reachable_time = uip_ntohl(UIP_ND6_RA_BUF->reachable_time);
863 }
864 }
865 if(UIP_ND6_RA_BUF->retrans_timer != 0) {
866 uip_ds6_if.retrans_timer = uip_ntohl(UIP_ND6_RA_BUF->retrans_timer);
867 }
868
869 /* Options processing */
870 nd6_opt_offset = UIP_ND6_RA_LEN;
871 while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
872 if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) {
873 LOG_ERR("Discarding invalid RA");
874 goto discard;
875 }
876 switch(ND6_OPT_HDR_BUF(nd6_opt_offset)->type) {
877 case UIP_ND6_OPT_SLLAO:
878 LOG_DBG("Processing SLLAO option in RA\n");
879 nd6_opt_llao = (uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset);
880 nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr);
881 if(!extract_lladdr_from_llao_aligned(&lladdr_aligned)) {
882 /* failed to extract llao - discard packet */
883 goto discard;
884 }
885 if(nbr == NULL) {
886 nbr = uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr, &lladdr_aligned,
887 1, NBR_STALE, NBR_TABLE_REASON_IPV6_ND, NULL);
888 } else {
889 const uip_lladdr_t *lladdr = uip_ds6_nbr_get_ll(nbr);
890 if(lladdr == NULL) {
891 goto discard;
892 }
893 if(nbr->state == NBR_INCOMPLETE) {
894 nbr->state = NBR_STALE;
895 }
896 if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
897 lladdr, UIP_LLADDR_LEN) != 0) {
898 /* change of link layer address */
900 (const uip_lladdr_t *)&lladdr_aligned) < 0) {
901 /* failed to update the lladdr */
902 goto discard;
903 }
904 nbr->state = NBR_STALE;
905 }
906 nbr->isrouter = 1;
907 }
908 break;
909 case UIP_ND6_OPT_MTU:
910 LOG_DBG("Processing MTU option in RA\n");
911 uip_ds6_if.link_mtu =
912 uip_ntohl(((uip_nd6_opt_mtu *)ND6_OPT_HDR_BUF(nd6_opt_offset))->mtu);
913 break;
914 case UIP_ND6_OPT_PREFIX_INFO:
915 LOG_DBG("Processing PREFIX option in RA\n");
916 nd6_opt_prefix_info = (uip_nd6_opt_prefix_info *)ND6_OPT_HDR_BUF(nd6_opt_offset);
917 if((uip_ntohl(nd6_opt_prefix_info->validlt) >=
918 uip_ntohl(nd6_opt_prefix_info->preferredlt))
920 /* on-link flag related processing */
921 if(nd6_opt_prefix_info->flagsreserved1 & UIP_ND6_RA_FLAG_ONLINK) {
922 prefix =
923 uip_ds6_prefix_lookup(&nd6_opt_prefix_info->prefix,
924 nd6_opt_prefix_info->preflen);
925 if(prefix == NULL) {
926 if(nd6_opt_prefix_info->validlt != 0) {
928 prefix = uip_ds6_prefix_add(&nd6_opt_prefix_info->prefix,
929 nd6_opt_prefix_info->preflen,
930 uip_ntohl(nd6_opt_prefix_info->
931 validlt));
932 } else {
933 prefix = uip_ds6_prefix_add(&nd6_opt_prefix_info->prefix,
934 nd6_opt_prefix_info->preflen, 0);
935 }
936 }
937 } else {
938 switch(nd6_opt_prefix_info->validlt) {
939 case 0:
940 uip_ds6_prefix_rm(prefix);
941 break;
943 prefix->isinfinite = 1;
944 break;
945 default:
946 LOG_DBG("Updating timer of prefix ");
947 LOG_DBG_6ADDR(&prefix->ipaddr);
948 LOG_DBG_(" new value %" PRIu32 "\n", uip_ntohl(nd6_opt_prefix_info->validlt));
949 stimer_set(&prefix->vlifetime,
950 uip_ntohl(nd6_opt_prefix_info->validlt));
951 prefix->isinfinite = 0;
952 break;
953 }
954 }
955 }
956 /* End of on-link flag related processing */
957 /* autonomous flag related processing */
958 if((nd6_opt_prefix_info->flagsreserved1 & UIP_ND6_RA_FLAG_AUTONOMOUS)
959 && (nd6_opt_prefix_info->validlt != 0)
960 && (nd6_opt_prefix_info->preflen == UIP_DEFAULT_PREFIX_LEN)) {
961
964 addr = uip_ds6_addr_lookup(&ipaddr);
965 if((addr != NULL) && (addr->type == ADDR_AUTOCONF)) {
967 /* The processing below is defined in RFC4862 section 5.5.3 e */
968 if((uip_ntohl(nd6_opt_prefix_info->validlt) > 2 * 60 * 60) ||
969 (uip_ntohl(nd6_opt_prefix_info->validlt) >
970 stimer_remaining(&addr->vlifetime))) {
971 LOG_DBG("Updating timer of address ");
972 LOG_DBG_6ADDR(&addr->ipaddr);
973 LOG_DBG_(" new value %lu\n",
974 (unsigned long)uip_ntohl(nd6_opt_prefix_info->validlt));
975 stimer_set(&addr->vlifetime,
976 uip_ntohl(nd6_opt_prefix_info->validlt));
977 } else {
978 stimer_set(&addr->vlifetime, 2 * 60 * 60);
979 LOG_DBG("Updating timer of address ");
980 LOG_DBG_6ADDR(&addr->ipaddr);
981 LOG_DBG_(" new value %lu\n", (unsigned long)(2 * 60 * 60));
982 }
983 addr->isinfinite = 0;
984 } else {
985 addr->isinfinite = 1;
986 }
987 } else {
988 if(uip_ntohl(nd6_opt_prefix_info->validlt) ==
990 uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
991 } else {
992 uip_ds6_addr_add(&ipaddr, uip_ntohl(nd6_opt_prefix_info->validlt),
993 ADDR_AUTOCONF);
994 }
995 }
996 }
997 /* End of autonomous flag related processing */
998 }
999 break;
1000#if UIP_ND6_RA_RDNSS
1001 case UIP_ND6_OPT_RDNSS:
1002 LOG_DBG("Processing RDNSS option\n");
1003 uint8_t naddr = (ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len - 1) / 2;
1004 uip_ipaddr_t *ip = (uip_ipaddr_t *)(&ND6_OPT_RDNSS_BUF(nd6_opt_offset)->ip);
1005 LOG_DBG("got %d nameservers\n", naddr);
1006 while(naddr-- > 0) {
1007 LOG_DBG("nameserver: ");
1008 LOG_DBG_6ADDR(ip);
1009 LOG_DBG_(" lifetime: %" PRIx32 "\n", uip_ntohl(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime));
1010 uip_nameserver_update(ip, uip_ntohl(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime));
1011 ip++;
1012 }
1013 break;
1014#endif /* UIP_ND6_RA_RDNSS */
1015 default:
1016 LOG_ERR("ND option not supported in RA\n");
1017 break;
1018 }
1019 nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3);
1020 }
1021
1022 defrt = uip_ds6_defrt_lookup(&UIP_IP_BUF->srcipaddr);
1023 if(UIP_ND6_RA_BUF->router_lifetime != 0) {
1024 if(nbr != NULL) {
1025 nbr->isrouter = 1;
1026 }
1027 if(defrt == NULL) {
1028 uip_ds6_defrt_add(&UIP_IP_BUF->srcipaddr,
1029 (unsigned
1030 long)(uip_ntohs(UIP_ND6_RA_BUF->router_lifetime)));
1031 } else {
1032 stimer_set(&(defrt->lifetime),
1033 (unsigned long)(uip_ntohs(UIP_ND6_RA_BUF->router_lifetime)));
1034 }
1035 } else {
1036 if(defrt != NULL) {
1037 uip_ds6_defrt_rm(defrt);
1038 }
1039 }
1040
1041#if UIP_CONF_IPV6_QUEUE_PKT
1042 /* If the nbr just became reachable (e.g. it was in NBR_INCOMPLETE state
1043 * and we got a SLLAO), check if we had buffered a pkt for it */
1044 /* if((nbr != NULL) && (nbr->queue_buf_len != 0)) {
1045 uip_len = nbr->queue_buf_len;
1046 memcpy(UIP_IP_BUF, nbr->queue_buf, uip_len);
1047 nbr->queue_buf_len = 0;
1048 return;
1049 }*/
1050 if(nbr != NULL && uip_packetqueue_buflen(&nbr->packethandle) != 0) {
1051 uip_len = uip_packetqueue_buflen(&nbr->packethandle);
1052 memcpy(UIP_IP_BUF, uip_packetqueue_buf(&nbr->packethandle), uip_len);
1053 uip_packetqueue_free(&nbr->packethandle);
1054 return;
1055 }
1056
1057#endif /*UIP_CONF_IPV6_QUEUE_PKT */
1058
1059discard:
1060 uipbuf_clear();
1061 return;
1062}
1063#endif /* !UIP_CONF_ROUTER */
1064/*------------------------------------------------------------------*/
1065/* ICMPv6 input handlers */
1066#if UIP_ND6_SEND_NA
1067UIP_ICMP6_HANDLER(ns_input_handler, ICMP6_NS, UIP_ICMP6_HANDLER_CODE_ANY,
1068 ns_input);
1069#endif
1070#if UIP_ND6_SEND_NS
1071UIP_ICMP6_HANDLER(na_input_handler, ICMP6_NA, UIP_ICMP6_HANDLER_CODE_ANY,
1072 na_input);
1073#endif
1074
1075#if UIP_CONF_ROUTER && UIP_ND6_SEND_RA
1076UIP_ICMP6_HANDLER(rs_input_handler, ICMP6_RS, UIP_ICMP6_HANDLER_CODE_ANY,
1077 rs_input);
1078#endif
1079
1080#if !UIP_CONF_ROUTER
1081UIP_ICMP6_HANDLER(ra_input_handler, ICMP6_RA, UIP_ICMP6_HANDLER_CODE_ANY,
1082 ra_input);
1083#endif
1084/*---------------------------------------------------------------------------*/
1085void
1087{
1088#if UIP_ND6_SEND_NA
1089 /* Only handle NSs if we are prepared to send out NAs */
1090 uip_icmp6_register_input_handler(&ns_input_handler);
1091#endif
1092
1093#if UIP_ND6_SEND_NS
1094 /*
1095 * Only handle NAs if we are prepared to send out NSs. */
1096 uip_icmp6_register_input_handler(&na_input_handler);
1097#endif
1098
1099#if UIP_CONF_ROUTER && UIP_ND6_SEND_RA
1100 /* Only accept RS if we are a router and happy to send out RAs */
1101 uip_icmp6_register_input_handler(&rs_input_handler);
1102#endif
1103
1104#if !UIP_CONF_ROUTER
1105 /* Only process RAs if we are not a router */
1106 uip_icmp6_register_input_handler(&ra_input_handler);
1107#endif
1108}
1109/*---------------------------------------------------------------------------*/
1110/** @} */
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition clock.c:130
static void stimer_set(struct stimer *t, unsigned long interval)
Set a timer.
Definition stimer.h:100
static unsigned long stimer_remaining(struct stimer *t)
The time until the timer expires.
Definition stimer.h:121
void uip_nd6_ns_output(const uip_ipaddr_t *src, const uip_ipaddr_t *dest, uip_ipaddr_t *tgt)
Send a neighbor solicitation, send a Neighbor Advertisement.
#define uip_create_linklocal_allrouters_mcast(a)
set IP address a to the link local all-routers multicast address
Definition uip.h:1777
uint32_t uip_nameserver_next_expiration(void)
Get next expiration time.
const uip_lladdr_t * uip_ds6_nbr_get_ll(const uip_ds6_nbr_t *nbr)
Get the link-layer address associated with a specified nbr cache.
#define uip_create_unspecified(a)
set IP address a to unspecified
Definition uip.h:1771
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...
uip_lladdr_t uip_lladdr
Host L2 address.
Definition uip6.c:107
int uip_ds6_nbr_update_ll(uip_ds6_nbr_t **nbr_pp, const uip_lladdr_t *new_ll_addr)
Update the link-layer address associated with an IPv6 address.
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition uip.h:1725
void uip_nameserver_update(const uip_ipaddr_t *nameserver, uint32_t lifetime)
Initialize the module variables.
#define UIP_ICMP_BUF
Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset (global uip_...
Definition uip.h:77
#define ICMP6_NS
Neighbor Solicitation.
Definition uip-icmp6.h:62
#define UIP_LLADDR_LEN
802.15.4 address
Definition uip.h:145
#define NBR_INCOMPLETE
Possible states for the nbr cache entries.
Definition uip-ds6-nbr.h:64
int uip_ds6_nbr_rm(uip_ds6_nbr_t *nbr)
Remove a neighbor cache.
uip_ds6_nbr_t * uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr)
Get the neighbor cache associated with a specified IPv6 address.
void uip_nd6_rs_output(void)
Neighbor Solicitation Processing.
Definition uip-nd6.c:790
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition uip.h:1860
uip_ds6_addr_t * uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
Add a unicast address to the interface.
Definition uip-ds6.c:352
#define ICMP6_NA
Neighbor advertisement.
Definition uip-icmp6.h:63
#define UIP_ND6_INFINITE_LIFETIME
INFINITE lifetime.
Definition uip-nd6.h:57
void uip_nd6_init()
Initialise the uIP ND core.
Definition uip-nd6.c:1086
#define UIP_STAT(s)
The uIP TCP/IP statistics.
Definition uip.h:1351
int uip_ds6_dad_failed(uip_ds6_addr_t *addr)
Callback when DAD failed.
Definition uip-ds6.c:658
#define uip_create_solicited_node(a, b)
put in b the solicited node address corresponding to address a both a and b are of type uip_ipaddr_t*
Definition uip.h:1804
#define ICMP6_RS
Router Solicitation.
Definition uip-icmp6.h:60
#define UIP_ND6_OPT_LLAO_LEN
length of a ND6 LLAO option for default L2 type (e.g.
Definition uip-nd6.h:243
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Definition uip.h:1766
#define uip_is_addr_solicited_node(a)
is addr (a) a solicited node multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition uip.h:1789
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
Definition uip-ds6.c:530
#define UIP_NAMESERVER_INFINITE_LIFETIME
Infinite Lifetime indicator.
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Definition uip-ds6.c:568
uip_ds6_prefix_t uip_ds6_prefix_list[UIP_DS6_PREFIX_NB]
Prefix list.
Definition uip-ds6.c:76
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
uip_ipaddr_t * uip_nameserver_get(uint8_t num)
Get a Nameserver ip address given in RA.
#define UIP_ND6_HOP_LIMIT
HOP LIMIT to be used when sending ND messages (255)
Definition uip-nd6.h:55
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition uip-ds6.h:156
#define ICMP6_RA
Router Advertisement.
Definition uip-icmp6.h:61
uint16_t uip_nameserver_count(void)
Get the number of recorded name servers.
uip_ds6_netif_t uip_ds6_if
The single interface.
Definition uip-ds6.c:75
#define uip_create_linklocal_allnodes_mcast(a)
set IP address a to the link local all-nodes multicast address
Definition uip.h:1774
uint32_t uip_ds6_compute_reachable_time(void)
Compute the reachable time based on base reachable time, see RFC 4861.
Definition uip-ds6.c:745
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition uip.h:71
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition uip.h:969
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition uip6.c:2345
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition uip.h:465
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition uip6.c:159
Header file for the logging system.
Unicast address structure.
Definition uip-ds6.h:205
An entry in the default router list.
The default nbr_table entry (when UIP_DS6_NBR_MULTI_IPV6_ADDRS is disabled), that implements nbr cach...
A prefix list entry.
Definition uip-ds6.h:195
A neighbor solicitation constant part.
Definition uip-nd6.h:268
ND option MTU.
Definition uip-nd6.h:342
ND option prefix information.
Definition uip-nd6.h:330
Header file for IPv6-related data structures.
Header file for ICMPv6 message and error handing (RFC 4443)
uIP Name Server interface
static uip_ds6_nbr_t * nbr
Pointer to llao option in uip_buf.
Definition uip-nd6.c:106
void ra_input(void)
Process a Router Advertisement.
Definition uip-nd6.c:835
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition uip-nd6.c:116
static uip_ds6_defrt_t * defrt
Pointer to an interface address.
Definition uip-nd6.c:111
static uip_nd6_opt_prefix_info * nd6_opt_prefix_info
Pointer to a router list entry.
Definition uip-nd6.c:115
static int extract_lladdr_from_llao_aligned(uip_lladdr_t *dest)
Pointer to a prefix list entry.
Definition uip-nd6.c:126
static uint8_t * nd6_opt_llao
Offset from the end of the icmpv6 header to the option in uip_buf.
Definition uip-nd6.c:105
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition uip-nd6.c:107
Header file for IPv6 Neighbor discovery (RFC 4861)