Beispiel #1
0
/*---------------------------------------------------------------------------*/
uip_ds6_maddr_t *
uip_ds6_maddr_lookup(uip_ipaddr_t *ipaddr)
{
  if(uip_ds6_list_loop
     ((uip_ds6_element_t *)uip_ds6_if.maddr_list, UIP_DS6_MADDR_NB,
      sizeof(uip_ds6_maddr_t), ipaddr, 128,
      (uip_ds6_element_t **)&locmaddr) == FOUND) {
    if (uip_is_addr_solicited_node(&locmaddr->ipaddr)) {
      return uip_ds6_maddr_solicited_node_verify(locmaddr);
    } else {
      return locmaddr;
    }
  }
  return NULL;
}
Beispiel #2
0
static void
ns_input(void)
{
    uint8_t flags;
#if CETIC_6LBR_SMARTBRIDGE
    uip_ds6_route_t * route;
#endif
    uip_ipaddr_t tgtipaddr;

    PRINTF("Received NS from ");
    PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
    PRINTF(" to ");
    PRINT6ADDR(&UIP_IP_BUF->destipaddr);
    PRINTF(" with target address");
    PRINT6ADDR((uip_ipaddr_t *) (&UIP_ND6_NS_BUF->tgtipaddr));
    PRINTF("\n");
    UIP_STAT(++uip_stat.nd6.recv);

#if UIP_CONF_IPV6_CHECKS
    if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
            (uip_is_addr_mcast(&UIP_ND6_NS_BUF->tgtipaddr)) ||
            (UIP_ICMP_BUF->icode != 0)) {
        PRINTF("NS received is bad\n");
        goto discard;
    }
#endif /* UIP_CONF_IPV6_CHECKS */

    /* Options processing */
    nd6_opt_llao = NULL;
    nd6_opt_offset = UIP_ND6_NS_LEN;
    while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
#if UIP_CONF_IPV6_CHECKS
        if(UIP_ND6_OPT_HDR_BUF->len == 0) {
            PRINTF("NS received is bad\n");
            goto discard;
        }
#endif /* UIP_CONF_IPV6_CHECKS */
        switch (UIP_ND6_OPT_HDR_BUF->type) {
        case UIP_ND6_OPT_SLLAO:
            nd6_opt_llao = &uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset];
#if UIP_CONF_IPV6_CHECKS
            /* There must be NO option in a DAD NS */
            if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
                PRINTF("NS received is bad\n");
                goto discard;
            } else {
#endif /*UIP_CONF_IPV6_CHECKS */
                nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr);
                if(nbr == NULL) {
                    /* Copy link address to a uip_lladdr_t first
                     * to ensure the second argument to uip_ds6_nbr_add is word-aligned */
                    uip_lladdr_t lladdr;
                    memcpy(&lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], UIP_LLADDR_LEN);
                    uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr, &lladdr, 0, NBR_STALE);
                } else {
                    uip_lladdr_t *lladdr = (uip_lladdr_t *)uip_ds6_nbr_get_ll(nbr);
                    if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
                              lladdr, UIP_LLADDR_LEN) != 0) {
                        memcpy(lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], UIP_LLADDR_LEN);
                        nbr->state = NBR_STALE;
                    } else {
                        if(nbr->state == NBR_INCOMPLETE) {
                            nbr->state = NBR_STALE;
                        }
                    }
                }
#if UIP_CONF_IPV6_CHECKS
            }
#endif /*UIP_CONF_IPV6_CHECKS */
            break;
        default:
            PRINTF("ND option not supported in NS");
            break;
        }
        nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
    }

    memcpy(&tgtipaddr, &UIP_ND6_NS_BUF->tgtipaddr, sizeof(tgtipaddr));
    addr = uip_ds6_addr_lookup(&tgtipaddr);
#if CETIC_6LBR_SMARTBRIDGE
    //ND Proxy implementation
    if ( addr == NULL ) {
        if ( (route = uip_ds6_route_lookup(&tgtipaddr)) != NULL ) {
            if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
                /* DAD CASE */
                uip_create_linklocal_allnodes_mcast(&tgtipaddr);
                uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &tgtipaddr);
                flags = UIP_ND6_NA_FLAG_OVERRIDE;
                goto create_na;
            } else {
                uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
                uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &tgtipaddr);
                flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
                goto create_na;
            }
        }
    }
#endif
    if(addr != NULL) {
#if UIP_ND6_DEF_MAXDADNS > 0
        if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
            /* DAD CASE */
#if UIP_CONF_IPV6_CHECKS
            if(!uip_is_addr_solicited_node(&UIP_IP_BUF->destipaddr)) {
                PRINTF("NS received is bad\n");
                goto discard;
            }
#endif /* UIP_CONF_IPV6_CHECKS */
            if(addr->state != ADDR_TENTATIVE) {
                uip_create_linklocal_allnodes_mcast(&UIP_IP_BUF->destipaddr);
                uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
                flags = UIP_ND6_NA_FLAG_OVERRIDE;
                goto create_na;
            } else {
                /** \todo if I sent a NS before him, I win */
                uip_ds6_dad_failed(addr);
                goto discard;
            }
#else /* UIP_ND6_DEF_MAXDADNS > 0 */
        if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
            /* DAD CASE */
            goto discard;
#endif /* UIP_ND6_DEF_MAXDADNS > 0 */
        }
#if UIP_CONF_IPV6_CHECKS
        if(uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr)) {
            /**
             * \NOTE do we do something here? we both are using the same address.
             * If we are doing dad, we could cancel it, though we should receive a
             * NA in response of DAD NS we sent, hence DAD will fail anyway. If we
             * were not doing DAD, it means there is a duplicate in the network!
             */
            PRINTF("NS received is bad\n");
            goto discard;
        }
#endif /*UIP_CONF_IPV6_CHECKS */

        /* Address resolution case */
        if(uip_is_addr_solicited_node(&UIP_IP_BUF->destipaddr)) {
            uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
            uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &tgtipaddr);
            flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
            goto create_na;
        }

        /* NUD CASE */
        if(uip_ds6_addr_lookup(&UIP_IP_BUF->destipaddr) == addr) {
            uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
            uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &tgtipaddr);
            flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
            goto create_na;
        } else {
#if UIP_CONF_IPV6_CHECKS
            PRINTF("NS received is bad\n");
            goto discard;
#endif /* UIP_CONF_IPV6_CHECKS */
        }
    } else {
        goto discard;
    }


create_na:
    /* If the node is a router it should set R flag in NAs */
#if UIP_CONF_ROUTER
    flags = flags | UIP_ND6_NA_FLAG_ROUTER;
#endif
    uip_ext_len = 0;
    UIP_IP_BUF->vtc = 0x60;
    UIP_IP_BUF->tcflow = 0;
    UIP_IP_BUF->flow = 0;
    UIP_IP_BUF->len[0] = 0;       /* length will not be more than 255 */
    UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN;
    UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
    UIP_IP_BUF->ttl = UIP_ND6_HOP_LIMIT;

    UIP_ICMP_BUF->type = ICMP6_NA;
    UIP_ICMP_BUF->icode = 0;

    UIP_ND6_NA_BUF->flagsreserved = flags;
    memcpy(&UIP_ND6_NA_BUF->tgtipaddr, &tgtipaddr, sizeof(uip_ipaddr_t));

    create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NA_LEN],
                UIP_ND6_OPT_TLLAO);

    UIP_ICMP_BUF->icmpchksum = 0;
    UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();

    uip_len =
        UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN;

    UIP_STAT(++uip_stat.nd6.sent);
    PRINTF("Sending NA to ");
    PRINT6ADDR(&UIP_IP_BUF->destipaddr);
    PRINTF(" from ");
    PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
    PRINTF(" with target address ");
    PRINT6ADDR(&UIP_ND6_NA_BUF->tgtipaddr);
    PRINTF("\n");
    return;

discard:
    uip_clear_buf();
    return;
}
#endif /* UIP_ND6_SEND_NA */


/*------------------------------------------------------------------*/
void
uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt)
{
    uip_ext_len = 0;
    UIP_IP_BUF->vtc = 0x60;
    UIP_IP_BUF->tcflow = 0;
    UIP_IP_BUF->flow = 0;
    UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
    UIP_IP_BUF->ttl = UIP_ND6_HOP_LIMIT;

    if(dest == NULL) {
        uip_create_solicited_node(tgt, &UIP_IP_BUF->destipaddr);
    } else {
        uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, dest);
    }
    UIP_ICMP_BUF->type = ICMP6_NS;
    UIP_ICMP_BUF->icode = 0;
    UIP_ND6_NS_BUF->reserved = 0;
    uip_ipaddr_copy((uip_ipaddr_t *) &UIP_ND6_NS_BUF->tgtipaddr, tgt);
    UIP_IP_BUF->len[0] = 0;       /* length will not be more than 255 */
    /*
     * check if we add a SLLAO option: for DAD, MUST NOT, for NUD, MAY
     * (here yes), for Address resolution , MUST
     */
    if(!(uip_ds6_is_my_addr(tgt))) {
        if(src != NULL) {
            uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, src);
        } else {
            uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
        }
        if (uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
            PRINTF("Dropping NS due to no suitable source address\n");
            uip_clear_buf();
            return;
        }
        UIP_IP_BUF->len[1] =
            UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN;

        create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NS_LEN],
                    UIP_ND6_OPT_SLLAO);

        uip_len =
            UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN;
    } else {
        uip_create_unspecified(&UIP_IP_BUF->srcipaddr);
        UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NS_LEN;
        uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NS_LEN;
    }

    UIP_ICMP_BUF->icmpchksum = 0;
    UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();

    UIP_STAT(++uip_stat.nd6.sent);
    PRINTF("Sending NS to");
    PRINT6ADDR(&UIP_IP_BUF->destipaddr);
    PRINTF("from");
    PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
    PRINTF("with target address");
    PRINT6ADDR(tgt);
    PRINTF("\n");
    return;
}
Beispiel #3
0
void
uip_nd6_ns_input(void)
{
  u8_t flags;
  PRINTF("Received NS from");
  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
  PRINTF("to");
  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
  PRINTF("with target address");
  PRINT6ADDR((uip_ipaddr_t *) (&UIP_ND6_NS_BUF->tgtipaddr));
  PRINTF("\n");
  UIP_STAT(++uip_stat.nd6.recv);

#if UIP_CONF_IPV6_CHECKS
  if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
     (uip_is_addr_mcast(&UIP_ND6_NS_BUF->tgtipaddr)) ||
     (UIP_ICMP_BUF->icode != 0)) {
    PRINTF("NS received is bad\n");
    goto discard;
  }
#endif /* UIP_CONF_IPV6_CHECKS */

  /* Options processing */
  nd6_opt_llao = NULL;
  nd6_opt_offset = UIP_ND6_NS_LEN;
  while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
#if UIP_CONF_IPV6_CHECKS
    if(UIP_ND6_OPT_HDR_BUF->len == 0) {
      PRINTF("NS received is bad\n");
      goto discard;
    }
#endif /* UIP_CONF_IPV6_CHECKS */
    switch (UIP_ND6_OPT_HDR_BUF->type) {
    case UIP_ND6_OPT_SLLAO:
      nd6_opt_llao = &uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset];
#if UIP_CONF_IPV6_CHECKS
      /* There must be NO option in a DAD NS */
      if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
        PRINTF("NS received is bad\n");
        goto discard;
      } else {
#endif /*UIP_CONF_IPV6_CHECKS */
        nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr);
        if(nbr == NULL) {
          uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr,
			  (uip_lladdr_t *)&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
			  0, NBR_STALE);
        } else {
          if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
		    &nbr->lladdr, UIP_LLADDR_LEN) != 0) {
            memcpy(&nbr->lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET],
		   UIP_LLADDR_LEN);
            nbr->state = NBR_STALE;
          } else {
            if(nbr->state == NBR_INCOMPLETE) {
              nbr->state = NBR_STALE;
            }
          }
        }
#if UIP_CONF_IPV6_CHECKS
      }
#endif /*UIP_CONF_IPV6_CHECKS */
      break;
    default:
      PRINTF("ND option not supported in NS");
      break;
    }
    nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
  }

  addr = uip_ds6_addr_lookup(&UIP_ND6_NS_BUF->tgtipaddr);
  if(addr != NULL) {
    if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
      /* DAD CASE */
#if UIP_CONF_IPV6_CHECKS
      if(!uip_is_addr_solicited_node(&UIP_IP_BUF->destipaddr)) {
        PRINTF("NS received is bad\n");
        goto discard;
      }
#endif /* UIP_CONF_IPV6_CHECKS */
      if(addr->state != ADDR_TENTATIVE) {
        uip_create_linklocal_allnodes_mcast(&UIP_IP_BUF->destipaddr);
        uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
        flags = UIP_ND6_NA_FLAG_OVERRIDE;
        goto create_na;
      } else {
          /** \todo if I sent a NS before him, I win */
        uip_ds6_dad_failed(addr);
        goto discard;
      }
    }
#if UIP_CONF_IPV6_CHECKS
    if(uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr)) {
        /**
         * \NOTE do we do something here? we both are using the same address.
         * If we are doing dad, we could cancel it, though we should receive a
         * NA in response of DAD NS we sent, hence DAD will fail anyway. If we
         * were not doing DAD, it means there is a duplicate in the network!
         */
      PRINTF("NS received is bad\n");
      goto discard;
    }
#endif /*UIP_CONF_IPV6_CHECKS */

    /* Address resolution case */
    if(uip_is_addr_solicited_node(&UIP_IP_BUF->destipaddr)) {
      uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
      uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_ND6_NS_BUF->tgtipaddr);
      flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
      goto create_na;
    }

    /* NUD CASE */
    if(uip_ds6_addr_lookup(&UIP_IP_BUF->destipaddr) == addr) {
      uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
      uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_ND6_NS_BUF->tgtipaddr);
      flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
      goto create_na;
    } else {
#if UIP_CONF_IPV6_CHECKS
      PRINTF("NS received is bad\n");
      goto discard;
#endif /* UIP_CONF_IPV6_CHECKS */
    }
  } else {
    goto discard;
  }


create_na:
  uip_ext_len = 0;
  UIP_IP_BUF->vtc = 0x60;
  UIP_IP_BUF->tcflow = 0;
  UIP_IP_BUF->flow = 0;
  UIP_IP_BUF->len[0] = 0;       /* length will not be more than 255 */
  UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN;
  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
  UIP_IP_BUF->ttl = UIP_ND6_HOP_LIMIT;

  UIP_ICMP_BUF->type = ICMP6_NA;
  UIP_ICMP_BUF->icode = 0;

  UIP_ND6_NA_BUF->flagsreserved = flags;
  memcpy(&UIP_ND6_NA_BUF->tgtipaddr, &addr->ipaddr, sizeof(uip_ipaddr_t));

  create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NA_LEN],
              UIP_ND6_OPT_TLLAO);

  UIP_ICMP_BUF->icmpchksum = 0;
  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();

  uip_len =
    UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN;

  UIP_STAT(++uip_stat.nd6.sent);
  PRINTF("Sending NA to");
  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
  PRINTF("from");
  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
  PRINTF("with target address");
  PRINT6ADDR(&UIP_ND6_NA_BUF->tgtipaddr);
  PRINTF("\n");
  return;

discard:
  uip_len = 0;
  return;
}