75 #include "lib/random.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])
86 #define PRINT6ADDR(addr)
87 #define PRINTLLADDR(addr)
94 #define UIP_LOG(m) uip_log(m)
112 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
113 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
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])
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])
126 static uint8_t nd6_opt_offset;
127 static uint8_t *nd6_opt_llao;
129 #if !UIP_CONF_ROUTER // TBD see if we move it to ra_input
142 create_llao(uint8_t *llao, uint8_t type) {
143 llao[UIP_ND6_OPT_TYPE_OFFSET] = type;
145 memcpy(&llao[UIP_ND6_OPT_DATA_OFFSET], &
uip_lladdr, UIP_LLADDR_LEN);
147 memset(&llao[UIP_ND6_OPT_DATA_OFFSET + UIP_LLADDR_LEN], 0,
157 PRINTF(
"Received NS from");
161 PRINTF(
"with target address");
162 PRINT6ADDR((
uip_ipaddr_t *) (&UIP_ND6_NS_BUF->tgtipaddr));
168 #if UIP_CONF_IPV6_CHECKS
170 (uip_is_addr_mcast(&UIP_ND6_NS_BUF->tgtipaddr)) ||
172 PRINTF(
"NS received is bad\n");
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
183 PRINTF(
"NS received is bad\n");
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
192 if(uip_is_addr_unspecified(&
UIP_IP_BUF->srcipaddr)) {
193 PRINTF(
"NS received is bad\n");
197 nbr = uip_ds6_nbr_lookup(&
UIP_IP_BUF->srcipaddr);
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],
207 nbr->state = NBR_STALE;
210 nbr->state = NBR_STALE;
214 #if UIP_CONF_IPV6_CHECKS
219 PRINTF(
"ND option not supported in NS");
225 addr = uip_ds6_addr_lookup(&UIP_ND6_NS_BUF->tgtipaddr);
227 if(uip_is_addr_unspecified(&
UIP_IP_BUF->srcipaddr)) {
229 #if UIP_CONF_IPV6_CHECKS
230 if(!uip_is_addr_solicited_node(&
UIP_IP_BUF->destipaddr)) {
231 PRINTF(
"NS received is bad\n");
236 uip_create_linklocal_allnodes_mcast(&
UIP_IP_BUF->destipaddr);
238 flags = UIP_ND6_NA_FLAG_OVERRIDE;
246 #if UIP_CONF_IPV6_CHECKS
247 if(uip_ds6_is_my_addr(&
UIP_IP_BUF->srcipaddr)) {
254 PRINTF(
"NS received is bad\n");
260 if(uip_is_addr_solicited_node(&
UIP_IP_BUF->destipaddr)) {
263 flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
268 if(uip_ds6_addr_lookup(&
UIP_IP_BUF->destipaddr) == addr) {
271 flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
274 #if UIP_CONF_IPV6_CHECKS
275 PRINTF(
"NS received is bad\n");
297 UIP_ND6_NA_BUF->flagsreserved =
flags;
298 memcpy(&UIP_ND6_NA_BUF->tgtipaddr, &addr->ipaddr,
sizeof(
uip_ipaddr_t));
300 create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NA_LEN],
310 PRINTF(
"Sending NA to");
314 PRINTF(
"with target address");
315 PRINT6ADDR(&UIP_ND6_NA_BUF->tgtipaddr);
338 uip_create_solicited_node(tgt, &
UIP_IP_BUF->destipaddr);
344 UIP_ND6_NS_BUF->reserved = 0;
351 if(!(uip_ds6_is_my_addr(tgt))) {
360 create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NS_LEN],
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;
375 PRINTF(
"Sending NS to");
379 PRINTF(
"with target address");
391 PRINTF(
"Received NA from");
395 PRINTF(
"with target address");
396 PRINT6ADDR((
uip_ipaddr_t *) (&UIP_ND6_NA_BUF->tgtipaddr));
404 u8_t is_llchange = 0;
405 u8_t is_router = ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_ROUTER));
407 ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_SOLICITED));
409 ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_OVERRIDE));
411 #if UIP_CONF_IPV6_CHECKS
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");
422 nd6_opt_offset = UIP_ND6_NA_LEN;
424 while(uip_l3_icmp_hdr_len + nd6_opt_offset <
uip_len) {
425 #if UIP_CONF_IPV6_CHECKS
427 PRINTF(
"NA received is bad\n");
432 case UIP_ND6_OPT_TLLAO:
436 PRINTF(
"ND option not supported in NA\n");
441 addr = uip_ds6_addr_lookup(&UIP_ND6_NA_BUF->tgtipaddr);
447 PRINTF(
"NA received is bad\n");
450 nbr = uip_ds6_nbr_lookup(&UIP_ND6_NA_BUF->tgtipaddr);
454 if(nd6_opt_llao != 0) {
456 memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], (
void *)(&nbr->lladdr),
460 if(nd6_opt_llao ==
NULL) {
463 memcpy(&nbr->lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
466 nbr->state = NBR_REACHABLE;
470 stimer_set(&(nbr->reachable), uip_ds6_if.reachable_time / 1000);
473 nbr->state = NBR_STALE;
475 nbr->isrouter = is_router;
477 if(!is_override && is_llchange) {
478 if(nbr->state == NBR_REACHABLE) {
479 nbr->state = NBR_STALE;
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],
490 nbr->state = NBR_REACHABLE;
492 stimer_set(&(nbr->reachable), uip_ds6_if.reachable_time / 1000);
494 if(nd6_opt_llao != 0 && is_llchange) {
495 nbr->state = NBR_STALE;
500 if(nbr->isrouter && !is_router) {
501 defrt = uip_ds6_defrt_lookup(&
UIP_IP_BUF->srcipaddr);
503 uip_ds6_defrt_rm(defrt);
506 nbr->isrouter = is_router;
509 #if UIP_CONF_IPV6_QUEUE_PKT
517 if(uip_packetqueue_buflen(&nbr->packethandle) != 0) {
518 uip_len = uip_packetqueue_buflen(&nbr->packethandle);
520 uip_packetqueue_free(&nbr->packethandle);
536 uip_nd6_rs_input(
void)
539 PRINTF(
"Received RS from");
547 #if UIP_CONF_IPV6_CHECKS
554 PRINTF(
"RS received is bad\n");
561 nd6_opt_offset = UIP_ND6_RS_LEN;
564 while(uip_l3_icmp_hdr_len + nd6_opt_offset <
uip_len) {
565 #if UIP_CONF_IPV6_CHECKS
567 PRINTF(
"RS received is bad\n");
572 case UIP_ND6_OPT_SLLAO:
576 PRINTF(
"ND option not supported in RS\n");
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");
592 &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], 0, NBR_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],
599 nbr->state = NBR_STALE;
603 #if UIP_CONF_IPV6_CHECKS
609 uip_ds6_send_ra_sollicited();
628 uip_create_linklocal_allnodes_mcast(&
UIP_IP_BUF->destipaddr);
638 UIP_ND6_RA_BUF->cur_ttl = uip_ds6_if.cur_hop_limit;
640 UIP_ND6_RA_BUF->flags_reserved =
641 (UIP_ND6_M_FLAG << 7) | (UIP_ND6_O_FLAG << 6);
643 UIP_ND6_RA_BUF->router_lifetime =
uip_htons(UIP_ND6_ROUTER_LIFETIME);
646 UIP_ND6_RA_BUF->reachable_time = 0;
647 UIP_ND6_RA_BUF->retrans_timer = 0;
649 uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_RA_LEN;
650 nd6_opt_offset = UIP_ND6_RA_LEN;
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;
665 nd6_opt_offset += UIP_ND6_OPT_PREFIX_INFO_LEN;
666 uip_len += UIP_ND6_OPT_PREFIX_INFO_LEN;
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;
681 UIP_ND6_OPT_MTU_BUF->mtu = uip_htonl(1500);
683 uip_len += UIP_ND6_OPT_MTU_LEN;
684 nd6_opt_offset += UIP_ND6_OPT_MTU_LEN;
693 PRINTF(
"Sending RA to");
713 uip_create_linklocal_allrouters_mcast(&
UIP_IP_BUF->destipaddr);
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;
727 create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_RS_LEN],
735 PRINTF(
"Sendin RS to");
748 PRINTF(
"Received RA from");
755 #if UIP_CONF_IPV6_CHECKS
757 (!uip_is_addr_link_local(&
UIP_IP_BUF->srcipaddr)) ||
759 PRINTF(
"RA received is bad");
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);
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);
776 if(UIP_ND6_RA_BUF->retrans_timer != 0) {
777 uip_ds6_if.retrans_timer = uip_ntohl(UIP_ND6_RA_BUF->retrans_timer);
781 nd6_opt_offset = UIP_ND6_RA_LEN;
782 while(uip_l3_icmp_hdr_len + nd6_opt_offset <
uip_len) {
784 PRINTF(
"RA received is bad");
788 case UIP_ND6_OPT_SLLAO:
789 PRINTF(
"Processing SLLAO option in RA\n");
791 nbr = uip_ds6_nbr_lookup(&
UIP_IP_BUF->srcipaddr);
798 nbr->state = NBR_STALE;
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],
804 nbr->state = NBR_STALE;
809 case UIP_ND6_OPT_MTU:
810 PRINTF(
"Processing MTU option in RA\n");
811 uip_ds6_if.link_mtu =
814 case UIP_ND6_OPT_PREFIX_INFO:
815 PRINTF(
"Processing PREFIX option in RA\n");
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))) {
821 if(nd6_opt_prefix_info->flagsreserved1 & UIP_ND6_RA_FLAG_ONLINK) {
823 uip_ds6_prefix_lookup(&nd6_opt_prefix_info->prefix,
824 nd6_opt_prefix_info->preflen);
826 if(nd6_opt_prefix_info->validlt != 0) {
828 prefix = uip_ds6_prefix_add(&nd6_opt_prefix_info->prefix,
829 nd6_opt_prefix_info->preflen,
830 uip_ntohl(nd6_opt_prefix_info->
833 prefix = uip_ds6_prefix_add(&nd6_opt_prefix_info->prefix,
834 nd6_opt_prefix_info->preflen, 0);
838 switch (nd6_opt_prefix_info->validlt) {
840 uip_ds6_prefix_rm(prefix);
843 prefix->isinfinite = 1;
846 PRINTF(
"Updating timer of prefix");
847 PRINT6ADDR(&prefix->ipaddr);
848 PRINTF(
"new value %lu\n", uip_ntohl(nd6_opt_prefix_info->validlt));
850 uip_ntohl(nd6_opt_prefix_info->validlt));
851 prefix->isinfinite = 0;
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)) {
864 addr = uip_ds6_addr_lookup(&ipaddr);
865 if((addr !=
NULL) && (addr->type == ADDR_AUTOCONF)) {
868 if((uip_ntohl(nd6_opt_prefix_info->validlt) > 2 * 60 * 60) ||
869 (uip_ntohl(nd6_opt_prefix_info->validlt) >
871 PRINTF(
"Updating timer of address");
872 PRINT6ADDR(&addr->ipaddr);
873 PRINTF(
"new value %lu\n",
874 uip_ntohl(nd6_opt_prefix_info->validlt));
876 uip_ntohl(nd6_opt_prefix_info->validlt));
879 PRINTF(
"Updating timer of address ");
880 PRINT6ADDR(&addr->ipaddr);
881 PRINTF(
"new value %lu\n", (
unsigned long)(2 * 60 * 60));
883 addr->isinfinite = 0;
885 addr->isinfinite = 1;
888 if(uip_ntohl(nd6_opt_prefix_info->validlt) ==
890 uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
892 uip_ds6_addr_add(&ipaddr, uip_ntohl(nd6_opt_prefix_info->validlt),
901 PRINTF(
"ND option not supported in RA");
907 defrt = uip_ds6_defrt_lookup(&
UIP_IP_BUF->srcipaddr);
908 if(UIP_ND6_RA_BUF->router_lifetime != 0) {
915 long)(uip_ntohs(UIP_ND6_RA_BUF->router_lifetime)));
918 (
unsigned long)(uip_ntohs(UIP_ND6_RA_BUF->router_lifetime)));
922 uip_ds6_defrt_rm(defrt);
926 #if UIP_CONF_IPV6_QUEUE_PKT
935 if(nbr !=
NULL && uip_packetqueue_buflen(&nbr->packethandle) != 0) {
936 uip_len = uip_packetqueue_buflen(&nbr->packethandle);
938 uip_packetqueue_free(&nbr->packethandle);