#include "lwip/opt.h"
#if LWIP_IPV6
#include "lwip/nd6.h"
#include "lwip/priv/nd6_priv.h"
#include "lwip/prot/nd6.h"
#include "lwip/prot/icmp6.h"
#include "lwip/pbuf.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/ip6.h"
#include "lwip/ip6_addr.h"
#include "lwip/inet_chksum.h"
#include "lwip/netif.h"
#include "lwip/icmp6.h"
#include "lwip/mld6.h"
#include "lwip/dhcp6.h"
#include "lwip/ip.h"
#include "lwip/stats.h"
#include "lwip/dns.h"
#include <string.h>
#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif
#if LWIP_IPV6_DUP_DETECT_ATTEMPTS > IP6_ADDR_TENTATIVE_COUNT_MASK
#error LWIP_IPV6_DUP_DETECT_ATTEMPTS > IP6_ADDR_TENTATIVE_COUNT_MASK
#endif
#if LWIP_ND6_NUM_NEIGHBORS > 127
#error LWIP_ND6_NUM_NEIGHBORS must fit into an s8_t (max value: 127)
#endif
#if LWIP_ND6_NUM_DESTINATIONS > 32767
#error LWIP_ND6_NUM_DESTINATIONS must fit into an s16_t (max value: 32767)
#endif
#if LWIP_ND6_NUM_PREFIXES > 127
#error LWIP_ND6_NUM_PREFIXES must fit into an s8_t (max value: 127)
#endif
#if LWIP_ND6_NUM_ROUTERS > 127
#error LWIP_ND6_NUM_ROUTERS must fit into an s8_t (max value: 127)
#endif
struct nd6_neighbor_cache_entry neighbor_cache[LWIP_ND6_NUM_NEIGHBORS];
struct nd6_destination_cache_entry destination_cache[LWIP_ND6_NUM_DESTINATIONS];
struct nd6_prefix_list_entry prefix_list[LWIP_ND6_NUM_PREFIXES];
struct nd6_router_list_entry default_router_list[LWIP_ND6_NUM_ROUTERS];
u32_t reachable_time = LWIP_ND6_REACHABLE_TIME;
u32_t retrans_timer = LWIP_ND6_RETRANS_TIMER;
#if LWIP_ND6_QUEUEING
static u8_t nd6_queue_size = 0;
#endif
static netif_addr_idx_t nd6_cached_destination_index;
static ip6_addr_t multicast_address;
static u8_t nd6_tmr_rs_reduction;
union ra_options {
struct lladdr_option lladdr;
struct mtu_option mtu;
struct prefix_option prefix;
#if LWIP_ND6_RDNSS_MAX_DNS_SERVERS
struct rdnss_option rdnss;
#endif
};
static union ra_options nd6_ra_buffer;
static s8_t nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr);
static s8_t nd6_new_neighbor_cache_entry(void);
static void nd6_free_neighbor_cache_entry(s8_t i);
static s16_t nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr);
static s16_t nd6_new_destination_cache_entry(void);
static int nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif);
static s8_t nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif);
static s8_t nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif);
static s8_t nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif);
static s8_t nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif);
static s8_t nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif);
static s8_t nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif);
static err_t nd6_queue_packet(s8_t neighbor_index, struct pbuf *q);
#define ND6_SEND_FLAG_MULTICAST_DEST 0x01
#define ND6_SEND_FLAG_ALLNODES_DEST 0x02
#define ND6_SEND_FLAG_ANY_SRC 0x04
static void nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags);
static void nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags);
static void nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags);
#if LWIP_IPV6_SEND_ROUTER_SOLICIT
static err_t nd6_send_rs(struct netif *netif);
#endif
#if LWIP_ND6_QUEUEING
static void nd6_free_q(struct nd6_q_entry *q);
#else
#define nd6_free_q(q) pbuf_free(q)
#endif
static void nd6_send_q(s8_t i);
static void
nd6_duplicate_addr_detected(struct netif *netif, s8_t addr_idx)
{
netif_ip6_addr_set_state(netif, addr_idx, IP6_ADDR_DUPLICATED);
#if LWIP_IPV6_AUTOCONFIG
if (addr_idx == 0) {
s8_t i;
for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
!netif_ip6_addr_isstatic(netif, i)) {
netif_ip6_addr_set_state(netif, i, IP6_ADDR_DUPLICATED);
}
}
}
#endif
}
#if LWIP_IPV6_AUTOCONFIG
static void
nd6_process_autoconfig_prefix(struct netif *netif,
struct prefix_option *prefix_opt, const ip6_addr_t *prefix_addr)
{
ip6_addr_t ip6addr;
u32_t valid_life, pref_life;
u8_t addr_state;
s8_t i, free_idx;
valid_life = lwip_htonl(prefix_opt->valid_lifetime);
pref_life = lwip_htonl(prefix_opt->preferred_lifetime);
if (pref_life > valid_life || prefix_opt->prefix_length != 64) {
return;
}
for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
addr_state = netif_ip6_addr_state(netif, i);
if (!ip6_addr_isinvalid(addr_state) && !netif_ip6_addr_isstatic(netif, i) &&
ip6_addr_net_eq(prefix_addr, netif_ip6_addr(netif, i))) {
u32_t remaining_life = netif_ip6_addr_valid_life(netif, i);
if (valid_life > ND6_2HRS || valid_life > remaining_life) {
netif_ip6_addr_set_valid_life(netif, i, valid_life);
} else if (remaining_life > ND6_2HRS) {
netif_ip6_addr_set_valid_life(netif, i, ND6_2HRS);
}
LWIP_ASSERT("bad valid lifetime", !netif_ip6_addr_isstatic(netif, i));
if (pref_life > 0 && addr_state == IP6_ADDR_DEPRECATED) {
netif_ip6_addr_set_state(netif, i, IP6_ADDR_PREFERRED);
}
netif_ip6_addr_set_pref_life(netif, i, pref_life);
return;
}
}
addr_state = netif_ip6_addr_state(netif, 0);
if (!netif->ip6_autoconfig_enabled || valid_life == IP6_ADDR_LIFE_STATIC ||
ip6_addr_isinvalid(addr_state) || ip6_addr_isduplicated(addr_state)) {
return;
}
IP6_ADDR(&ip6addr, prefix_addr->addr[0], prefix_addr->addr[1],
netif_ip6_addr(netif, 0)->addr[2], netif_ip6_addr(netif, 0)->addr[3]);
ip6_addr_assign_zone(&ip6addr, IP6_UNICAST, netif);
free_idx = 0;
for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i))) {
if (ip6_addr_eq(&ip6addr, netif_ip6_addr(netif, i))) {
return;
}
} else if (free_idx == 0) {
free_idx = i;
}
}
if (free_idx == 0) {
return;
}
ip_addr_copy_from_ip6(netif->ip6_addr[free_idx], ip6addr);
netif_ip6_addr_set_valid_life(netif, free_idx, valid_life);
netif_ip6_addr_set_pref_life(netif, free_idx, pref_life);
netif_ip6_addr_set_state(netif, free_idx, IP6_ADDR_TENTATIVE);
}
#endif
void
nd6_input(struct pbuf *p, struct netif *inp)
{
u8_t msg_type;
s8_t i;
s16_t dest_idx;
ND6_STATS_INC(nd6.recv);
msg_type = *((u8_t *)p->payload);
switch (msg_type) {
case ICMP6_TYPE_NA:
{
struct na_header *na_hdr;
struct lladdr_option *lladdr_opt;
ip6_addr_t target_address;
if (p->len < (sizeof(struct na_header))) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
na_hdr = (struct na_header *)p->payload;
ip6_addr_copy_from_packed(target_address, na_hdr->target_address);
ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp);
if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || na_hdr->code != 0 ||
ip6_addr_ismulticast(&target_address)) {
pbuf_free(p);
ND6_STATS_INC(nd6.proterr);
ND6_STATS_INC(nd6.drop);
return;
}
if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
#if LWIP_IPV6_DUP_DETECT_ATTEMPTS
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) &&
!ip6_addr_isduplicated(netif_ip6_addr_state(inp, i)) &&
ip6_addr_eq(&target_address, netif_ip6_addr(inp, i))) {
nd6_duplicate_addr_detected(inp, i);
pbuf_free(p);
return;
}
}
#endif
if (p->len < (sizeof(struct na_header) + 2)) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
i = nd6_find_neighbor_cache_entry(&target_address);
if (i >= 0) {
if (na_hdr->flags & ND6_FLAG_OVERRIDE) {
MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
}
}
} else {
i = nd6_find_neighbor_cache_entry(&target_address);
if (i < 0) {
pbuf_free(p);
return;
}
if ((na_hdr->flags & ND6_FLAG_OVERRIDE) ||
(neighbor_cache[i].state == ND6_INCOMPLETE)) {
if (p->len < (sizeof(struct na_header) + 2)) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
}
neighbor_cache[i].netif = inp;
neighbor_cache[i].state = ND6_REACHABLE;
neighbor_cache[i].counter.reachable_time = reachable_time;
if (neighbor_cache[i].q != NULL) {
nd6_send_q(i);
}
}
break;
}
case ICMP6_TYPE_NS:
{
struct ns_header *ns_hdr;
struct lladdr_option *lladdr_opt;
ip6_addr_t target_address;
u8_t accepted;
if (p->len < sizeof(struct ns_header)) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
ns_hdr = (struct ns_header *)p->payload;
ip6_addr_copy_from_packed(target_address, ns_hdr->target_address);
ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp);
if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ns_hdr->code != 0 ||
ip6_addr_ismulticast(&target_address)) {
pbuf_free(p);
ND6_STATS_INC(nd6.proterr);
ND6_STATS_INC(nd6.drop);
return;
}
if (p->len >= (sizeof(struct ns_header) + 2)) {
lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
if (p->len < (sizeof(struct ns_header) + (lladdr_opt->length << 3))) {
lladdr_opt = NULL;
}
} else {
lladdr_opt = NULL;
}
accepted = 0;
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
if ((ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) ||
(ip6_addr_istentative(netif_ip6_addr_state(inp, i)) &&
ip6_addr_isany(ip6_current_src_addr()))) &&
ip6_addr_eq(&target_address, netif_ip6_addr(inp, i))) {
accepted = 1;
break;
}
}
if (!accepted) {
pbuf_free(p);
return;
}
if (ip6_addr_isany(ip6_current_src_addr())) {
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) &&
ip6_addr_eq(&target_address, netif_ip6_addr(inp, i))) {
nd6_send_na(inp, netif_ip6_addr(inp, i), ND6_FLAG_OVERRIDE | ND6_SEND_FLAG_ALLNODES_DEST);
if (ip6_addr_istentative(netif_ip6_addr_state(inp, i))) {
nd6_duplicate_addr_detected(inp, i);
}
}
}
} else {
if (lladdr_opt == NULL) {
pbuf_free(p);
ND6_STATS_INC(nd6.proterr);
ND6_STATS_INC(nd6.drop);
return;
}
i = nd6_find_neighbor_cache_entry(ip6_current_src_addr());
if (i>= 0) {
if (neighbor_cache[i].state == ND6_INCOMPLETE) {
neighbor_cache[i].netif = inp;
MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
neighbor_cache[i].state = ND6_DELAY;
neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
}
} else {
i = nd6_new_neighbor_cache_entry();
if (i < 0) {
pbuf_free(p);
ND6_STATS_INC(nd6.memerr);
return;
}
neighbor_cache[i].netif = inp;
MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
ip6_addr_set(&(neighbor_cache[i].next_hop_address), ip6_current_src_addr());
neighbor_cache[i].state = ND6_DELAY;
neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
}
nd6_send_na(inp, &target_address, ND6_FLAG_SOLICITED | ND6_FLAG_OVERRIDE);
}
break;
}
case ICMP6_TYPE_RA:
{
struct ra_header *ra_hdr;
u8_t *buffer;
u16_t offset;
#if LWIP_ND6_RDNSS_MAX_DNS_SERVERS
u8_t rdnss_server_idx = 0;
#endif
if (p->len < sizeof(struct ra_header)) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
ra_hdr = (struct ra_header *)p->payload;
if (!ip6_addr_islinklocal(ip6_current_src_addr()) ||
IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ra_hdr->code != 0) {
pbuf_free(p);
ND6_STATS_INC(nd6.proterr);
ND6_STATS_INC(nd6.drop);
return;
}
#if LWIP_IPV6_SEND_ROUTER_SOLICIT
if ((inp->rs_count < LWIP_ND6_MAX_MULTICAST_SOLICIT) ||
(nd6_send_rs(inp) == ERR_OK)) {
inp->rs_count = 0;
} else {
inp->rs_count = 1;
}
#endif
i = nd6_get_router(ip6_current_src_addr(), inp);
if (i < 0) {
i = nd6_new_router(ip6_current_src_addr(), inp);
}
if (i < 0) {
pbuf_free(p);
ND6_STATS_INC(nd6.memerr);
return;
}
default_router_list[i].invalidation_timer = lwip_htons(ra_hdr->router_lifetime);
#if LWIP_ND6_ALLOW_RA_UPDATES
if (ra_hdr->retrans_timer > 0) {
retrans_timer = lwip_htonl(ra_hdr->retrans_timer);
}
if (ra_hdr->reachable_time > 0) {
reachable_time = lwip_htonl(ra_hdr->reachable_time);
}
#endif
default_router_list[i].flags = ra_hdr->flags;
#if LWIP_IPV6_DHCP6
dhcp6_nd6_ra_trigger(inp, ra_hdr->flags & ND6_RA_FLAG_MANAGED_ADDR_CONFIG,
ra_hdr->flags & ND6_RA_FLAG_OTHER_CONFIG);
#endif
offset = sizeof(struct ra_header);
while ((p->tot_len - offset) >= 2) {
u8_t option_type;
u16_t option_len;
int option_len8 = pbuf_try_get_at(p, offset + 1);
if (option_len8 <= 0) {
goto lenerr_drop_free_return;
}
option_len = ((u8_t)option_len8) << 3;
if (option_len > p->tot_len - offset) {
goto lenerr_drop_free_return;
}
if (p->len == p->tot_len) {
buffer = &((u8_t*)p->payload)[offset];
} else {
if (option_len > sizeof(nd6_ra_buffer)) {
option_type = pbuf_get_at(p, offset);
if (option_type != ND6_OPTION_TYPE_RDNSS) {
goto lenerr_drop_free_return;
}
option_len = sizeof(nd6_ra_buffer);
}
buffer = (u8_t*)&nd6_ra_buffer;
option_len = pbuf_copy_partial(p, &nd6_ra_buffer, option_len, offset);
}
option_type = buffer[0];
switch (option_type) {
case ND6_OPTION_TYPE_SOURCE_LLADDR:
{
struct lladdr_option *lladdr_opt;
if (option_len < (ND6_LLADDR_OPTION_MIN_LENGTH + inp->hwaddr_len)) {
goto lenerr_drop_free_return;
}
lladdr_opt = (struct lladdr_option *)buffer;
if ((default_router_list[i].neighbor_entry != NULL) &&
(default_router_list[i].neighbor_entry->state == ND6_INCOMPLETE)) {
SMEMCPY(default_router_list[i].neighbor_entry->lladdr, lladdr_opt->addr, inp->hwaddr_len);
default_router_list[i].neighbor_entry->state = ND6_REACHABLE;
default_router_list[i].neighbor_entry->counter.reachable_time = reachable_time;
}
break;
}
case ND6_OPTION_TYPE_MTU:
{
struct mtu_option *mtu_opt;
u32_t mtu32;
if (option_len < sizeof(struct mtu_option)) {
goto lenerr_drop_free_return;
}
mtu_opt = (struct mtu_option *)buffer;
mtu32 = lwip_htonl(mtu_opt->mtu);
if ((mtu32 >= IP6_MIN_MTU_LENGTH) && (mtu32 <= 0xffff)) {
#if LWIP_ND6_ALLOW_RA_UPDATES
if (inp->mtu) {
inp->mtu6 = LWIP_MIN(LWIP_MIN(inp->mtu, inp->mtu6), (u16_t)mtu32);
} else {
inp->mtu6 = (u16_t)mtu32;
}
#endif
}
break;
}
case ND6_OPTION_TYPE_PREFIX_INFO:
{
struct prefix_option *prefix_opt;
ip6_addr_t prefix_addr;
if (option_len < sizeof(struct prefix_option)) {
goto lenerr_drop_free_return;
}
prefix_opt = (struct prefix_option *)buffer;
ip6_addr_copy_from_packed(prefix_addr, prefix_opt->prefix);
ip6_addr_assign_zone(&prefix_addr, IP6_UNICAST, inp);
if (!ip6_addr_islinklocal(&prefix_addr)) {
if ((prefix_opt->flags & ND6_PREFIX_FLAG_ON_LINK) &&
(prefix_opt->prefix_length == 64)) {
u32_t valid_life;
s8_t prefix;
valid_life = lwip_htonl(prefix_opt->valid_lifetime);
prefix = nd6_get_onlink_prefix(&prefix_addr, inp);
if (prefix < 0 && valid_life > 0) {
prefix = nd6_new_onlink_prefix(&prefix_addr, inp);
}
if (prefix >= 0) {
prefix_list[prefix].invalidation_timer = valid_life;
}
}
#if LWIP_IPV6_AUTOCONFIG
if (prefix_opt->flags & ND6_PREFIX_FLAG_AUTONOMOUS) {
nd6_process_autoconfig_prefix(inp, prefix_opt, &prefix_addr);
}
#endif
}
break;
}
case ND6_OPTION_TYPE_ROUTE_INFO:
break;
#if LWIP_ND6_RDNSS_MAX_DNS_SERVERS
case ND6_OPTION_TYPE_RDNSS:
{
u8_t num, n;
u16_t copy_offset = offset + SIZEOF_RDNSS_OPTION_BASE;
struct rdnss_option * rdnss_opt;
if (option_len < SIZEOF_RDNSS_OPTION_BASE) {
goto lenerr_drop_free_return;
}
rdnss_opt = (struct rdnss_option *)buffer;
num = (rdnss_opt->length - 1) / 2;
for (n = 0; (rdnss_server_idx < DNS_MAX_SERVERS) && (n < num); n++, copy_offset += sizeof(ip6_addr_p_t)) {
ip_addr_t rdnss_address;
if (pbuf_copy_partial(p, &rdnss_address, sizeof(ip6_addr_p_t), copy_offset) == sizeof(ip6_addr_p_t)) {
IP_SET_TYPE_VAL(rdnss_address, IPADDR_TYPE_V6);
ip6_addr_assign_zone(ip_2_ip6(&rdnss_address), IP6_UNKNOWN, inp);
if (htonl(rdnss_opt->lifetime) > 0) {
dns_setserver(rdnss_server_idx++, &rdnss_address);
} else {
u8_t s;
for (s = 0; s < DNS_MAX_SERVERS; s++) {
const ip_addr_t *addr = dns_getserver(s);
if(ip_addr_eq(addr, &rdnss_address)) {
dns_setserver(s, NULL);
}
}
}
}
}
break;
}
#endif
default:
ND6_STATS_INC(nd6.proterr);
break;
}
offset += 8 * (u8_t)option_len8;
}
break;
}
case ICMP6_TYPE_RD:
{
struct redirect_header *redir_hdr;
struct lladdr_option *lladdr_opt;
ip6_addr_t destination_address, target_address;
if (p->len < sizeof(struct redirect_header)) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
redir_hdr = (struct redirect_header *)p->payload;
ip6_addr_copy_from_packed(destination_address, redir_hdr->destination_address);
ip6_addr_assign_zone(&destination_address, IP6_UNICAST, inp);
if (!ip6_addr_islinklocal(ip6_current_src_addr()) ||
IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM ||
redir_hdr->code != 0 || ip6_addr_ismulticast(&destination_address)) {
pbuf_free(p);
ND6_STATS_INC(nd6.proterr);
ND6_STATS_INC(nd6.drop);
return;
}
if (p->len >= (sizeof(struct redirect_header) + 2)) {
lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct redirect_header));
if (p->len < (sizeof(struct redirect_header) + (lladdr_opt->length << 3))) {
lladdr_opt = NULL;
}
} else {
lladdr_opt = NULL;
}
dest_idx = nd6_find_destination_cache_entry(&destination_address);
if (dest_idx < 0) {
pbuf_free(p);
return;
}
ip6_addr_copy_from_packed(target_address, redir_hdr->target_address);
ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp);
ip6_addr_copy(destination_cache[dest_idx].next_hop_addr, target_address);
if (lladdr_opt != NULL) {
if (lladdr_opt->type == ND6_OPTION_TYPE_TARGET_LLADDR) {
i = nd6_find_neighbor_cache_entry(&target_address);
if (i < 0) {
i = nd6_new_neighbor_cache_entry();
if (i >= 0) {
neighbor_cache[i].netif = inp;
MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
ip6_addr_copy(neighbor_cache[i].next_hop_address, target_address);
neighbor_cache[i].state = ND6_DELAY;
neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
}
}
if (i >= 0) {
if (neighbor_cache[i].state == ND6_INCOMPLETE) {
MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
neighbor_cache[i].state = ND6_DELAY;
neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
}
}
}
}
break;
}
case ICMP6_TYPE_PTB:
{
struct icmp6_hdr *icmp6hdr;
struct ip6_hdr *ip6hdr;
u32_t pmtu;
ip6_addr_t destination_address;
if (p->len < (sizeof(struct icmp6_hdr) + IP6_HLEN)) {
pbuf_free(p);
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
return;
}
icmp6hdr = (struct icmp6_hdr *)p->payload;
ip6hdr = (struct ip6_hdr *)((u8_t*)p->payload + sizeof(struct icmp6_hdr));
ip6_addr_copy_from_packed(destination_address, ip6hdr->dest);
ip6_addr_assign_zone(&destination_address, IP6_UNKNOWN, inp);
dest_idx = nd6_find_destination_cache_entry(&destination_address);
if (dest_idx < 0) {
pbuf_free(p);
return;
}
pmtu = lwip_htonl(icmp6hdr->data);
destination_cache[dest_idx].pmtu = (u16_t)LWIP_MIN(pmtu, 0xFFFF);
break;
}
default:
ND6_STATS_INC(nd6.proterr);
ND6_STATS_INC(nd6.drop);
break;
}
pbuf_free(p);
return;
lenerr_drop_free_return:
ND6_STATS_INC(nd6.lenerr);
ND6_STATS_INC(nd6.drop);
pbuf_free(p);
}
void
nd6_tmr(void)
{
s8_t i;
struct netif *netif;
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
switch (neighbor_cache[i].state) {
case ND6_INCOMPLETE:
if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) &&
(!neighbor_cache[i].isrouter)) {
nd6_free_neighbor_cache_entry(i);
} else {
neighbor_cache[i].counter.probes_sent++;
nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST);
}
break;
case ND6_REACHABLE:
if (neighbor_cache[i].q != NULL) {
nd6_send_q(i);
}
if (neighbor_cache[i].counter.reachable_time <= ND6_TMR_INTERVAL) {
neighbor_cache[i].state = ND6_STALE;
neighbor_cache[i].counter.stale_time = 0;
} else {
neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL;
}
break;
case ND6_STALE:
neighbor_cache[i].counter.stale_time++;
break;
case ND6_DELAY:
if (neighbor_cache[i].counter.delay_time <= 1) {
neighbor_cache[i].state = ND6_PROBE;
neighbor_cache[i].counter.probes_sent = 0;
} else {
neighbor_cache[i].counter.delay_time--;
}
break;
case ND6_PROBE:
if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) &&
(!neighbor_cache[i].isrouter)) {
nd6_free_neighbor_cache_entry(i);
} else {
neighbor_cache[i].counter.probes_sent++;
nd6_send_neighbor_cache_probe(&neighbor_cache[i], 0);
}
break;
case ND6_NO_ENTRY:
default:
break;
}
}
for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
destination_cache[i].age++;
}
for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
if (default_router_list[i].neighbor_entry != NULL) {
if (default_router_list[i].invalidation_timer <= ND6_TMR_INTERVAL / 1000) {
s8_t j;
for (j = 0; j < LWIP_ND6_NUM_DESTINATIONS; j++) {
if (ip6_addr_eq(&destination_cache[j].next_hop_addr,
&default_router_list[i].neighbor_entry->next_hop_address)) {
ip6_addr_set_any(&destination_cache[j].destination_addr);
}
}
default_router_list[i].neighbor_entry->isrouter = 0;
default_router_list[i].neighbor_entry = NULL;
default_router_list[i].invalidation_timer = 0;
default_router_list[i].flags = 0;
} else {
default_router_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
}
}
}
for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
if (prefix_list[i].netif != NULL) {
if (prefix_list[i].invalidation_timer <= ND6_TMR_INTERVAL / 1000) {
prefix_list[i].invalidation_timer = 0;
prefix_list[i].netif = NULL;
} else {
prefix_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
}
}
}
NETIF_FOREACH(netif) {
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
u8_t addr_state;
#if LWIP_IPV6_ADDRESS_LIFETIMES
addr_state = netif_ip6_addr_state(netif, i);
if (!ip6_addr_isinvalid(addr_state) &&
!netif_ip6_addr_isstatic(netif, i)) {
u32_t life = netif_ip6_addr_valid_life(netif, i);
if (life <= ND6_TMR_INTERVAL / 1000) {
netif_ip6_addr_set_valid_life(netif, i, 0);
netif_ip6_addr_set_pref_life(netif, i, 0);
netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
} else {
if (!ip6_addr_life_isinfinite(life)) {
life -= ND6_TMR_INTERVAL / 1000;
LWIP_ASSERT("bad valid lifetime", life != IP6_ADDR_LIFE_STATIC);
netif_ip6_addr_set_valid_life(netif, i, life);
}
life = netif_ip6_addr_pref_life(netif, i);
if (life <= ND6_TMR_INTERVAL / 1000) {
netif_ip6_addr_set_pref_life(netif, i, 0);
if (addr_state == IP6_ADDR_PREFERRED)
netif_ip6_addr_set_state(netif, i, IP6_ADDR_DEPRECATED);
} else if (!ip6_addr_life_isinfinite(life)) {
life -= ND6_TMR_INTERVAL / 1000;
netif_ip6_addr_set_pref_life(netif, i, life);
}
}
}
#endif
addr_state = netif_ip6_addr_state(netif, i);
if (ip6_addr_istentative(addr_state)) {
if ((addr_state & IP6_ADDR_TENTATIVE_COUNT_MASK) >= LWIP_IPV6_DUP_DETECT_ATTEMPTS) {
addr_state = IP6_ADDR_PREFERRED;
#if LWIP_IPV6_ADDRESS_LIFETIMES
if (!netif_ip6_addr_isstatic(netif, i) &&
netif_ip6_addr_pref_life(netif, i) == 0) {
addr_state = IP6_ADDR_DEPRECATED;
}
#endif
netif_ip6_addr_set_state(netif, i, addr_state);
} else if (netif_is_up(netif) && netif_is_link_up(netif)) {
netif_ip6_addr_set_state(netif, i, addr_state + 1);
nd6_send_ns(netif, netif_ip6_addr(netif, i),
ND6_SEND_FLAG_MULTICAST_DEST | ND6_SEND_FLAG_ANY_SRC);
}
}
}
}
#if LWIP_IPV6_SEND_ROUTER_SOLICIT
if (!nd6_tmr_rs_reduction) {
nd6_tmr_rs_reduction = (ND6_RTR_SOLICITATION_INTERVAL / ND6_TMR_INTERVAL) - 1;
NETIF_FOREACH(netif) {
if ((netif->rs_count > 0) && netif_is_up(netif) &&
netif_is_link_up(netif) &&
!ip6_addr_isinvalid(netif_ip6_addr_state(netif, 0)) &&
!ip6_addr_isduplicated(netif_ip6_addr_state(netif, 0))) {
if (nd6_send_rs(netif) == ERR_OK) {
netif->rs_count--;
}
}
}
} else {
nd6_tmr_rs_reduction--;
}
#endif
}
static void
nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags)
{
nd6_send_ns(entry->netif, &entry->next_hop_address, flags);
}
static void
nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags)
{
struct ns_header *ns_hdr;
struct pbuf *p;
const ip6_addr_t *src_addr = NULL;
u16_t lladdr_opt_len;
LWIP_ASSERT("target address is required", target_addr != NULL);
if (!(flags & ND6_SEND_FLAG_ANY_SRC)) {
int i;
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
ip6_addr_net_eq(target_addr, netif_ip6_addr(netif, i))) {
src_addr = netif_ip6_addr(netif, i);
break;
}
}
if (i == LWIP_IPV6_NUM_ADDRESSES) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, ("ICMPv6 NS: no available src address\n"));
ND6_STATS_INC(nd6.err);
return;
}
lladdr_opt_len = ((netif->hwaddr_len + 2) + 7) >> 3;
} else {
src_addr = IP6_ADDR_ANY6;
lladdr_opt_len = 0;
}
p = pbuf_alloc(PBUF_IP, sizeof(struct ns_header) + (lladdr_opt_len << 3), PBUF_RAM);
if (p == NULL) {
ND6_STATS_INC(nd6.memerr);
return;
}
ns_hdr = (struct ns_header *)p->payload;
ns_hdr->type = ICMP6_TYPE_NS;
ns_hdr->code = 0;
ns_hdr->chksum = 0;
ns_hdr->reserved = 0;
ip6_addr_copy_to_packed(ns_hdr->target_address, *target_addr);
if (lladdr_opt_len != 0) {
struct lladdr_option *lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
lladdr_opt->length = (u8_t)lladdr_opt_len;
SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
}
if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
target_addr = &multicast_address;
}
#if CHECKSUM_GEN_ICMP6
IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
target_addr);
}
#endif
ND6_STATS_INC(nd6.xmit);
ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, target_addr,
ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif);
pbuf_free(p);
}
static void
nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags)
{
struct na_header *na_hdr;
struct lladdr_option *lladdr_opt;
struct pbuf *p;
const ip6_addr_t *src_addr;
const ip6_addr_t *dest_addr;
u16_t lladdr_opt_len;
LWIP_ASSERT("target address is required", target_addr != NULL);
src_addr = target_addr;
lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
p = pbuf_alloc(PBUF_IP, sizeof(struct na_header) + (lladdr_opt_len << 3), PBUF_RAM);
if (p == NULL) {
ND6_STATS_INC(nd6.memerr);
return;
}
na_hdr = (struct na_header *)p->payload;
lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
na_hdr->type = ICMP6_TYPE_NA;
na_hdr->code = 0;
na_hdr->chksum = 0;
na_hdr->flags = flags & 0xf0;
na_hdr->reserved[0] = 0;
na_hdr->reserved[1] = 0;
na_hdr->reserved[2] = 0;
ip6_addr_copy_to_packed(na_hdr->target_address, *target_addr);
lladdr_opt->type = ND6_OPTION_TYPE_TARGET_LLADDR;
lladdr_opt->length = (u8_t)lladdr_opt_len;
SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
dest_addr = &multicast_address;
} else if (flags & ND6_SEND_FLAG_ALLNODES_DEST) {
ip6_addr_set_allnodes_linklocal(&multicast_address);
ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
dest_addr = &multicast_address;
} else {
dest_addr = ip6_current_src_addr();
}
#if CHECKSUM_GEN_ICMP6
IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
dest_addr);
}
#endif
ND6_STATS_INC(nd6.xmit);
ip6_output_if(p, src_addr, dest_addr,
ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif);
pbuf_free(p);
}
#if LWIP_IPV6_SEND_ROUTER_SOLICIT
static err_t
nd6_send_rs(struct netif *netif)
{
struct rs_header *rs_hdr;
struct lladdr_option *lladdr_opt;
struct pbuf *p;
const ip6_addr_t *src_addr;
err_t err;
u16_t lladdr_opt_len = 0;
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) {
src_addr = netif_ip6_addr(netif, 0);
} else {
src_addr = IP6_ADDR_ANY6;
}
ip6_addr_set_allrouters_linklocal(&multicast_address);
ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
if (src_addr != IP6_ADDR_ANY6) {
lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
}
p = pbuf_alloc(PBUF_IP, sizeof(struct rs_header) + (lladdr_opt_len << 3), PBUF_RAM);
if (p == NULL) {
ND6_STATS_INC(nd6.memerr);
return ERR_BUF;
}
rs_hdr = (struct rs_header *)p->payload;
rs_hdr->type = ICMP6_TYPE_RS;
rs_hdr->code = 0;
rs_hdr->chksum = 0;
rs_hdr->reserved = 0;
if (src_addr != IP6_ADDR_ANY6) {
lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct rs_header));
lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
lladdr_opt->length = (u8_t)lladdr_opt_len;
SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
}
#if CHECKSUM_GEN_ICMP6
IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
&multicast_address);
}
#endif
ND6_STATS_INC(nd6.xmit);
err = ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, &multicast_address,
ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif);
pbuf_free(p);
return err;
}
#endif
static s8_t
nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr)
{
s8_t i;
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if (ip6_addr_eq(ip6addr, &(neighbor_cache[i].next_hop_address))) {
return i;
}
}
return -1;
}
static s8_t
nd6_new_neighbor_cache_entry(void)
{
s8_t i;
s8_t j;
u32_t time;
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if (neighbor_cache[i].state == ND6_NO_ENTRY) {
return i;
}
}
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if ((neighbor_cache[i].state == ND6_STALE) &&
(!neighbor_cache[i].isrouter)) {
nd6_free_neighbor_cache_entry(i);
return i;
}
}
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if ((neighbor_cache[i].state == ND6_PROBE) &&
(!neighbor_cache[i].isrouter)) {
nd6_free_neighbor_cache_entry(i);
return i;
}
}
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if ((neighbor_cache[i].state == ND6_DELAY) &&
(!neighbor_cache[i].isrouter)) {
nd6_free_neighbor_cache_entry(i);
return i;
}
}
time = 0xfffffffful;
j = -1;
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if ((neighbor_cache[i].state == ND6_REACHABLE) &&
(!neighbor_cache[i].isrouter)) {
if (neighbor_cache[i].counter.reachable_time < time) {
j = i;
time = neighbor_cache[i].counter.reachable_time;
}
}
}
if (j >= 0) {
nd6_free_neighbor_cache_entry(j);
return j;
}
time = 0;
j = -1;
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if (
(neighbor_cache[i].q == NULL) &&
(neighbor_cache[i].state == ND6_INCOMPLETE) &&
(!neighbor_cache[i].isrouter)) {
if (neighbor_cache[i].counter.probes_sent >= time) {
j = i;
time = neighbor_cache[i].counter.probes_sent;
}
}
}
if (j >= 0) {
nd6_free_neighbor_cache_entry(j);
return j;
}
time = 0;
j = -1;
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if ((neighbor_cache[i].state == ND6_INCOMPLETE) &&
(!neighbor_cache[i].isrouter)) {
if (neighbor_cache[i].counter.probes_sent >= time) {
j = i;
time = neighbor_cache[i].counter.probes_sent;
}
}
}
if (j >= 0) {
nd6_free_neighbor_cache_entry(j);
return j;
}
return -1;
}
static void
nd6_free_neighbor_cache_entry(s8_t i)
{
if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
return;
}
if (neighbor_cache[i].isrouter) {
return;
}
if (neighbor_cache[i].q != NULL) {
nd6_free_q(neighbor_cache[i].q);
neighbor_cache[i].q = NULL;
}
neighbor_cache[i].state = ND6_NO_ENTRY;
neighbor_cache[i].isrouter = 0;
neighbor_cache[i].netif = NULL;
neighbor_cache[i].counter.reachable_time = 0;
ip6_addr_set_zero(&(neighbor_cache[i].next_hop_address));
}
static s16_t
nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr)
{
s16_t i;
IP6_ADDR_ZONECHECK(ip6addr);
for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
if (ip6_addr_eq(ip6addr, &(destination_cache[i].destination_addr))) {
return i;
}
}
return -1;
}
static s16_t
nd6_new_destination_cache_entry(void)
{
s16_t i, j;
u32_t age;
for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
if (ip6_addr_isany(&(destination_cache[i].destination_addr))) {
return i;
}
}
age = 0;
j = LWIP_ND6_NUM_DESTINATIONS - 1;
for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
if (destination_cache[i].age > age) {
j = i;
}
}
return j;
}
void
nd6_clear_destination_cache(void)
{
int i;
for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
ip6_addr_set_any(&destination_cache[i].destination_addr);
}
}
static int
nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif)
{
s8_t i;
for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
if ((prefix_list[i].netif == netif) &&
(prefix_list[i].invalidation_timer > 0) &&
ip6_addr_net_eq(ip6addr, &(prefix_list[i].prefix))) {
return 1;
}
}
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
netif_ip6_addr_isstatic(netif, i) &&
ip6_addr_net_eq(ip6addr, netif_ip6_addr(netif, i))) {
return 1;
}
}
return 0;
}
static s8_t
nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif)
{
struct netif *router_netif;
s8_t i, j, valid_router;
static s8_t last_router;
LWIP_UNUSED_ARG(ip6addr);
valid_router = -1;
for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
if (default_router_list[i].neighbor_entry != NULL) {
router_netif = default_router_list[i].neighbor_entry->netif;
if ((router_netif != NULL) && (netif != NULL ? netif == router_netif :
(netif_is_up(router_netif) && netif_is_link_up(router_netif)))) {
if (default_router_list[i].neighbor_entry->state != ND6_INCOMPLETE) {
if (default_router_list[i].neighbor_entry->state == ND6_REACHABLE) {
return i;
} else if (valid_router < 0) {
valid_router = i;
}
}
}
}
}
if (valid_router >= 0) {
return valid_router;
}
if ((netif == NULL) && (++last_router >= LWIP_ND6_NUM_ROUTERS)) {
last_router = 0;
}
i = last_router;
for (j = 0; j < LWIP_ND6_NUM_ROUTERS; j++) {
if (default_router_list[i].neighbor_entry != NULL) {
router_netif = default_router_list[i].neighbor_entry->netif;
if ((router_netif != NULL) && (netif != NULL ? netif == router_netif :
(netif_is_up(router_netif) && netif_is_link_up(router_netif)))) {
return i;
}
}
if (++i >= LWIP_ND6_NUM_ROUTERS) {
i = 0;
}
}
return -1;
}
struct netif *
nd6_find_route(const ip6_addr_t *ip6addr)
{
struct netif *netif;
s8_t i;
for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
netif = prefix_list[i].netif;
if ((netif != NULL) && ip6_addr_net_eq(&prefix_list[i].prefix, ip6addr) &&
netif_is_up(netif) && netif_is_link_up(netif)) {
return netif;
}
}
i = nd6_select_router(ip6addr, NULL);
if (i >= 0) {
LWIP_ASSERT("selected router must have a neighbor entry",
default_router_list[i].neighbor_entry != NULL);
return default_router_list[i].neighbor_entry->netif;
}
return NULL;
}
static s8_t
nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif)
{
s8_t i;
IP6_ADDR_ZONECHECK_NETIF(router_addr, netif);
for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
if ((default_router_list[i].neighbor_entry != NULL) &&
((netif != NULL) ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
ip6_addr_eq(router_addr, &(default_router_list[i].neighbor_entry->next_hop_address))) {
return i;
}
}
return -1;
}
static s8_t
nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif)
{
s8_t router_index;
s8_t free_router_index;
s8_t neighbor_index;
IP6_ADDR_ZONECHECK_NETIF(router_addr, netif);
neighbor_index = nd6_find_neighbor_cache_entry(router_addr);
if (neighbor_index < 0) {
neighbor_index = nd6_new_neighbor_cache_entry();
if (neighbor_index < 0) {
return -1;
}
ip6_addr_set(&(neighbor_cache[neighbor_index].next_hop_address), router_addr);
neighbor_cache[neighbor_index].netif = netif;
neighbor_cache[neighbor_index].q = NULL;
neighbor_cache[neighbor_index].state = ND6_INCOMPLETE;
neighbor_cache[neighbor_index].counter.probes_sent = 1;
nd6_send_neighbor_cache_probe(&neighbor_cache[neighbor_index], ND6_SEND_FLAG_MULTICAST_DEST);
}
neighbor_cache[neighbor_index].isrouter = 1;
free_router_index = LWIP_ND6_NUM_ROUTERS;
for (router_index = LWIP_ND6_NUM_ROUTERS - 1; router_index >= 0; router_index--) {
if(default_router_list[router_index].neighbor_entry == &(neighbor_cache[neighbor_index])){
return router_index;
}
if (default_router_list[router_index].neighbor_entry == NULL) {
free_router_index = router_index;
}
}
if (free_router_index < LWIP_ND6_NUM_ROUTERS) {
default_router_list[free_router_index].neighbor_entry = &(neighbor_cache[neighbor_index]);
return free_router_index;
}
neighbor_cache[neighbor_index].isrouter = 0;
return -1;
}
static s8_t
nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif)
{
s8_t i;
for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
if ((ip6_addr_net_eq(&(prefix_list[i].prefix), prefix)) &&
(prefix_list[i].netif == netif)) {
return i;
}
}
return -1;
}
static s8_t
nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif)
{
s8_t i;
for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
if ((prefix_list[i].netif == NULL) ||
(prefix_list[i].invalidation_timer == 0)) {
prefix_list[i].netif = netif;
ip6_addr_set(&(prefix_list[i].prefix), prefix);
return i;
}
}
return -1;
}
static s8_t
nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif)
{
#ifdef LWIP_HOOK_ND6_GET_GW
const ip6_addr_t *next_hop_addr;
#endif
s8_t i;
s16_t dst_idx;
struct nd6_destination_cache_entry *dest;
IP6_ADDR_ZONECHECK_NETIF(ip6addr, netif);
#if LWIP_NETIF_HWADDRHINT
if (netif->hints != NULL) {
netif_addr_idx_t addr_hint = netif->hints->addr_hint;
if (addr_hint < LWIP_ND6_NUM_DESTINATIONS) {
nd6_cached_destination_index = addr_hint;
}
}
#endif
LWIP_ASSERT("sane cache index", nd6_cached_destination_index < LWIP_ND6_NUM_DESTINATIONS);
dest = &destination_cache[nd6_cached_destination_index];
if (ip6_addr_eq(ip6addr, &dest->destination_addr)) {
ND6_STATS_INC(nd6.cachehit);
} else {
dst_idx = nd6_find_destination_cache_entry(ip6addr);
if (dst_idx >= 0) {
LWIP_ASSERT("type overflow", (size_t)dst_idx < NETIF_ADDR_IDX_MAX);
nd6_cached_destination_index = (netif_addr_idx_t)dst_idx;
dest = &destination_cache[dst_idx];
} else {
dst_idx = nd6_new_destination_cache_entry();
if (dst_idx >= 0) {
LWIP_ASSERT("type overflow", (size_t)dst_idx < NETIF_ADDR_IDX_MAX);
nd6_cached_destination_index = (netif_addr_idx_t)dst_idx;
dest = &destination_cache[dst_idx];
} else {
return ERR_MEM;
}
ip6_addr_set(&dest->destination_addr, ip6addr);
if (ip6_addr_islinklocal(ip6addr) ||
nd6_is_prefix_in_netif(ip6addr, netif)) {
dest->pmtu = netif_mtu6(netif);
ip6_addr_copy(dest->next_hop_addr, dest->destination_addr);
#ifdef LWIP_HOOK_ND6_GET_GW
} else if ((next_hop_addr = LWIP_HOOK_ND6_GET_GW(netif, ip6addr)) != NULL) {
dest->pmtu = netif->mtu;
ip6_addr_set(&dest->next_hop_addr, next_hop_addr);
#endif
} else {
i = nd6_select_router(ip6addr, netif);
if (i < 0) {
ip6_addr_set_any(&dest->destination_addr);
return ERR_RTE;
}
dest->pmtu = netif_mtu6(netif);
ip6_addr_copy(dest->next_hop_addr, default_router_list[i].neighbor_entry->next_hop_address);
}
}
#if LWIP_NETIF_HWADDRHINT
if (netif->hints != NULL) {
netif->hints->addr_hint = nd6_cached_destination_index;
}
#endif
}
if (ip6_addr_eq(&dest->next_hop_addr,
&(neighbor_cache[dest->cached_neighbor_idx].next_hop_address))) {
ND6_STATS_INC(nd6.cachehit);
} else {
i = nd6_find_neighbor_cache_entry(&dest->next_hop_addr);
if (i >= 0) {
dest->cached_neighbor_idx = i;
} else {
i = nd6_new_neighbor_cache_entry();
if (i >= 0) {
dest->cached_neighbor_idx = i;
} else {
return ERR_MEM;
}
ip6_addr_copy(neighbor_cache[i].next_hop_address, dest->next_hop_addr);
neighbor_cache[i].isrouter = 0;
neighbor_cache[i].netif = netif;
neighbor_cache[i].state = ND6_INCOMPLETE;
neighbor_cache[i].counter.probes_sent = 1;
nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST);
}
}
dest->age = 0;
return dest->cached_neighbor_idx;
}
static err_t
nd6_queue_packet(s8_t neighbor_index, struct pbuf *q)
{
err_t result = ERR_MEM;
struct pbuf *p;
int copy_needed = 0;
#if LWIP_ND6_QUEUEING
struct nd6_q_entry *new_entry, *r;
#endif
if ((neighbor_index < 0) || (neighbor_index >= LWIP_ND6_NUM_NEIGHBORS)) {
return ERR_ARG;
}
p = q;
while (p) {
if (PBUF_NEEDS_COPY(p)) {
copy_needed = 1;
break;
}
p = p->next;
}
if (copy_needed) {
p = pbuf_clone(PBUF_LINK, PBUF_RAM, q);
while ((p == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
#if LWIP_ND6_QUEUEING
r = neighbor_cache[neighbor_index].q;
neighbor_cache[neighbor_index].q = r->next;
r->next = NULL;
nd6_free_q(r);
#else
pbuf_free(neighbor_cache[neighbor_index].q);
neighbor_cache[neighbor_index].q = NULL;
#endif
p = pbuf_clone(PBUF_LINK, PBUF_RAM, q);
}
} else {
p = q;
pbuf_ref(p);
}
if (p != NULL) {
#if LWIP_ND6_QUEUEING
new_entry = NULL;
if (nd6_queue_size < MEMP_NUM_ND6_QUEUE) {
new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
nd6_queue_size++;
}
if ((new_entry == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
r = neighbor_cache[neighbor_index].q;
neighbor_cache[neighbor_index].q = r->next;
r->next = NULL;
nd6_free_q(r);
new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
nd6_queue_size++;
}
if (new_entry != NULL) {
new_entry->next = NULL;
new_entry->p = p;
if (neighbor_cache[neighbor_index].q != NULL) {
r = neighbor_cache[neighbor_index].q;
while (r->next != NULL) {
r = r->next;
}
r->next = new_entry;
} else {
neighbor_cache[neighbor_index].q = new_entry;
}
LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
result = ERR_OK;
} else {
pbuf_free(p);
LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)p));
}
#else
if (neighbor_cache[neighbor_index].q != NULL) {
pbuf_free(neighbor_cache[neighbor_index].q);
}
neighbor_cache[neighbor_index].q = p;
LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
result = ERR_OK;
#endif
} else {
LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)q));
}
return result;
}
#if LWIP_ND6_QUEUEING
static void
nd6_free_q(struct nd6_q_entry *q)
{
struct nd6_q_entry *r;
LWIP_ASSERT("q != NULL", q != NULL);
LWIP_ASSERT("q->p != NULL", q->p != NULL);
while (q) {
r = q;
q = q->next;
LWIP_ASSERT("r->p != NULL", (r->p != NULL));
pbuf_free(r->p);
memp_free(MEMP_ND6_QUEUE, r);
nd6_queue_size--;
}
}
#endif
static void
nd6_send_q(s8_t i)
{
struct ip6_hdr *ip6hdr;
ip6_addr_t dest;
#if LWIP_ND6_QUEUEING
struct nd6_q_entry *q;
#endif
if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
return;
}
#if LWIP_ND6_QUEUEING
while (neighbor_cache[i].q != NULL) {
q = neighbor_cache[i].q;
neighbor_cache[i].q = q->next;
ip6hdr = (struct ip6_hdr *)(q->p->payload);
ip6_addr_copy_from_packed(dest, ip6hdr->dest);
ip6_addr_assign_zone(&dest, IP6_UNKNOWN, neighbor_cache[i].netif);
(neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, q->p, &dest);
pbuf_free(q->p);
memp_free(MEMP_ND6_QUEUE, q);
nd6_queue_size--;
}
#else
if (neighbor_cache[i].q != NULL) {
ip6hdr = (struct ip6_hdr *)(neighbor_cache[i].q->payload);
ip6_addr_copy_from_packed(dest, ip6hdr->dest);
ip6_addr_assign_zone(&dest, IP6_UNKNOWN, neighbor_cache[i].netif);
(neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, neighbor_cache[i].q, &dest);
pbuf_free(neighbor_cache[i].q);
neighbor_cache[i].q = NULL;
}
#endif
}
err_t
nd6_get_next_hop_addr_or_queue(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr, const u8_t **hwaddrp)
{
s8_t i;
i = nd6_get_next_hop_entry(ip6addr, netif);
if (i < 0) {
return i;
}
if (neighbor_cache[i].state == ND6_STALE) {
neighbor_cache[i].state = ND6_DELAY;
neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
}
if ((neighbor_cache[i].state == ND6_REACHABLE) ||
(neighbor_cache[i].state == ND6_DELAY) ||
(neighbor_cache[i].state == ND6_PROBE)) {
*hwaddrp = neighbor_cache[i].lladdr;
return ERR_OK;
}
*hwaddrp = NULL;
return nd6_queue_packet(i, q);
}
u16_t
nd6_get_destination_mtu(const ip6_addr_t *ip6addr, struct netif *netif)
{
s16_t i;
i = nd6_find_destination_cache_entry(ip6addr);
if (i >= 0) {
if (destination_cache[i].pmtu > 0) {
return destination_cache[i].pmtu;
}
}
if (netif != NULL) {
return netif_mtu6(netif);
}
return IP6_MIN_MTU_LENGTH;
}
#if LWIP_ND6_TCP_REACHABILITY_HINTS
void
nd6_reachability_hint(const ip6_addr_t *ip6addr)
{
s8_t i;
s16_t dst_idx;
struct nd6_destination_cache_entry *dest;
if (ip6_addr_eq(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) {
dst_idx = nd6_cached_destination_index;
ND6_STATS_INC(nd6.cachehit);
} else {
dst_idx = nd6_find_destination_cache_entry(ip6addr);
}
if (dst_idx < 0) {
return;
}
dest = &destination_cache[dst_idx];
if (ip6_addr_eq(&dest->next_hop_addr, &(neighbor_cache[dest->cached_neighbor_idx].next_hop_address))) {
i = dest->cached_neighbor_idx;
ND6_STATS_INC(nd6.cachehit);
} else {
i = nd6_find_neighbor_cache_entry(&dest->next_hop_addr);
}
if (i < 0) {
return;
}
if (neighbor_cache[i].state == ND6_INCOMPLETE || neighbor_cache[i].state == ND6_NO_ENTRY) {
return;
}
neighbor_cache[i].state = ND6_REACHABLE;
neighbor_cache[i].counter.reachable_time = reachable_time;
}
#endif
void
nd6_cleanup_netif(struct netif *netif)
{
u8_t i;
s8_t router_index;
for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
if (prefix_list[i].netif == netif) {
prefix_list[i].netif = NULL;
}
}
for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
if (neighbor_cache[i].netif == netif) {
for (router_index = 0; router_index < LWIP_ND6_NUM_ROUTERS; router_index++) {
if (default_router_list[router_index].neighbor_entry == &neighbor_cache[i]) {
default_router_list[router_index].neighbor_entry = NULL;
default_router_list[router_index].flags = 0;
}
}
neighbor_cache[i].isrouter = 0;
nd6_free_neighbor_cache_entry(i);
}
}
nd6_clear_destination_cache();
}
#if LWIP_IPV6_MLD
void
nd6_adjust_mld_membership(struct netif *netif, s8_t addr_idx, u8_t new_state)
{
u8_t old_state, old_member, new_member;
old_state = netif_ip6_addr_state(netif, addr_idx);
old_member = (old_state != IP6_ADDR_INVALID && old_state != IP6_ADDR_DUPLICATED && old_state != IP6_ADDR_TENTATIVE);
new_member = (new_state != IP6_ADDR_INVALID && new_state != IP6_ADDR_DUPLICATED && new_state != IP6_ADDR_TENTATIVE);
if (old_member != new_member) {
ip6_addr_set_solicitednode(&multicast_address, netif_ip6_addr(netif, addr_idx)->addr[3]);
ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
if (new_member) {
mld6_joingroup_netif(netif, &multicast_address);
} else {
mld6_leavegroup_netif(netif, &multicast_address);
}
}
}
#endif
void
nd6_restart_netif(struct netif *netif)
{
#if LWIP_IPV6_SEND_ROUTER_SOLICIT
netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
#endif
}
#endif