#include "lwip/opt.h"
#if LWIP_IPV6
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/netif.h"
#include "lwip/ip.h"
#include "lwip/ip6.h"
#include "lwip/ip6_addr.h"
#include "lwip/ip6_frag.h"
#include "lwip/icmp6.h"
#include "lwip/priv/raw_priv.h"
#include "lwip/udp.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/dhcp6.h"
#include "lwip/nd6.h"
#include "lwip/mld6.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif
struct netif *
ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest)
{
#if TUN2SOCKS
return netif_list;
#endif
#if LWIP_SINGLE_NETIF
LWIP_UNUSED_ARG(src);
LWIP_UNUSED_ARG(dest);
#else
struct netif *netif;
s8_t i;
LWIP_ASSERT_CORE_LOCKED();
if ((netif_list != NULL) && (netif_list->next == NULL)) {
if (!netif_is_up(netif_list) || !netif_is_link_up(netif_list) ||
(ip6_addr_has_zone(dest) && !ip6_addr_test_zone(dest, netif_list))) {
return NULL;
}
return netif_list;
}
#if LWIP_IPV6_SCOPES
if (ip6_addr_has_zone(dest)) {
IP6_ADDR_ZONECHECK(dest);
NETIF_FOREACH(netif) {
if (ip6_addr_test_zone(dest, netif) &&
netif_is_up(netif) && netif_is_link_up(netif)) {
return netif;
}
}
return NULL;
}
#endif
#if LWIP_IPV6_SCOPES
if (ip6_addr_has_scope(dest, IP6_UNKNOWN) ||
ip6_addr_has_scope(src, IP6_UNICAST) ||
#else
if (ip6_addr_islinklocal(dest) || ip6_addr_ismulticast_iflocal(dest) ||
ip6_addr_ismulticast_linklocal(dest) || ip6_addr_islinklocal(src) ||
#endif
ip6_addr_isloopback(src)) {
#if LWIP_IPV6_SCOPES
if (ip6_addr_has_zone(src)) {
NETIF_FOREACH(netif) {
if (netif_is_up(netif) && netif_is_link_up(netif) &&
ip6_addr_test_zone(src, netif)) {
return netif;
}
}
} else
#endif
{
NETIF_FOREACH(netif) {
if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
continue;
}
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
ip6_addr_cmp_zoneless(src, netif_ip6_addr(netif, i))) {
return netif;
}
}
}
}
return NULL;
}
IP6_ADDR_ZONECHECK(src);
#ifdef LWIP_HOOK_IP6_ROUTE
netif = LWIP_HOOK_IP6_ROUTE(src, dest);
if (netif != NULL) {
return netif;
}
#endif
NETIF_FOREACH(netif) {
if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
continue;
}
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
ip6_addr_netcmp(dest, netif_ip6_addr(netif, i)) &&
(netif_ip6_addr_isstatic(netif, i) ||
ip6_addr_nethostcmp(dest, netif_ip6_addr(netif, i)))) {
return netif;
}
}
}
netif = nd6_find_route(dest);
if (netif != NULL) {
return netif;
}
if (!ip6_addr_isany(src)) {
NETIF_FOREACH(netif) {
if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
continue;
}
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
ip6_addr_cmp(src, netif_ip6_addr(netif, i))) {
return netif;
}
}
}
}
#if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
if (ip6_addr_isloopback(dest)) {
if (netif_default != NULL && netif_is_up(netif_default)) {
return netif_default;
}
NETIF_FOREACH(netif) {
if (netif_is_up(netif)) {
return netif;
}
}
return NULL;
}
#endif
#endif
if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default)) {
return NULL;
}
return netif_default;
}
const ip_addr_t *
ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest)
{
const ip_addr_t *best_addr;
const ip6_addr_t *cand_addr;
s8_t dest_scope, cand_scope;
s8_t best_scope = IP6_MULTICAST_SCOPE_RESERVED;
u8_t i, cand_pref, cand_bits;
u8_t best_pref = 0;
u8_t best_bits = 0;
if (ip6_addr_isglobal(dest)) {
dest_scope = IP6_MULTICAST_SCOPE_GLOBAL;
} else if (ip6_addr_islinklocal(dest) || ip6_addr_isloopback(dest)) {
dest_scope = IP6_MULTICAST_SCOPE_LINK_LOCAL;
} else if (ip6_addr_isuniquelocal(dest)) {
dest_scope = IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL;
} else if (ip6_addr_ismulticast(dest)) {
dest_scope = ip6_addr_multicast_scope(dest);
} else if (ip6_addr_issitelocal(dest)) {
dest_scope = IP6_MULTICAST_SCOPE_SITE_LOCAL;
} else {
dest_scope = IP6_MULTICAST_SCOPE_GLOBAL;
}
best_addr = NULL;
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
continue;
}
cand_addr = netif_ip6_addr(netif, i);
if (ip6_addr_isglobal(cand_addr)) {
cand_scope = IP6_MULTICAST_SCOPE_GLOBAL;
} else if (ip6_addr_islinklocal(cand_addr)) {
cand_scope = IP6_MULTICAST_SCOPE_LINK_LOCAL;
} else if (ip6_addr_isuniquelocal(cand_addr)) {
cand_scope = IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL;
} else if (ip6_addr_issitelocal(cand_addr)) {
cand_scope = IP6_MULTICAST_SCOPE_SITE_LOCAL;
} else {
cand_scope = IP6_MULTICAST_SCOPE_RESERVEDF;
}
cand_pref = ip6_addr_ispreferred(netif_ip6_addr_state(netif, i));
cand_bits = ip6_addr_netcmp_zoneless(cand_addr, dest);
if (cand_bits && ip6_addr_nethostcmp(cand_addr, dest)) {
return netif_ip_addr6(netif, i);
}
if ((best_addr == NULL) ||
((cand_scope < best_scope) && (cand_scope >= dest_scope)) ||
((cand_scope > best_scope) && (best_scope < dest_scope)) ||
((cand_scope == best_scope) && ((cand_pref > best_pref) ||
((cand_pref == best_pref) && (cand_bits > best_bits))))) {
best_addr = netif_ip_addr6(netif, i);
best_scope = cand_scope;
best_pref = cand_pref;
best_bits = cand_bits;
}
}
return best_addr;
}
#if LWIP_IPV6_FORWARD
static void
ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp)
{
struct netif *netif;
if (ip6_addr_islinklocal(ip6_current_dest_addr()) ||
ip6_addr_isloopback(ip6_current_dest_addr())) {
LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding link-local address.\n"));
IP6_STATS_INC(ip6.rterr);
IP6_STATS_INC(ip6.drop);
return;
}
netif = ip6_route(IP6_ADDR_ANY6, ip6_current_dest_addr());
if (netif == NULL) {
LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
#if LWIP_ICMP6
if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
icmp6_dest_unreach(p, ICMP6_DUR_NO_ROUTE);
}
#endif
IP6_STATS_INC(ip6.rterr);
IP6_STATS_INC(ip6.drop);
return;
}
#if LWIP_IPV6_SCOPES
if ((ip6_addr_has_zone(ip6_current_src_addr()) &&
!ip6_addr_test_zone(ip6_current_src_addr(), netif)) ||
ip6_addr_isloopback(ip6_current_src_addr())) {
LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding packet beyond its source address zone.\n"));
IP6_STATS_INC(ip6.rterr);
IP6_STATS_INC(ip6.drop);
return;
}
#endif
if (netif == inp) {
LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not bouncing packets back on incoming interface.\n"));
IP6_STATS_INC(ip6.rterr);
IP6_STATS_INC(ip6.drop);
return;
}
IP6H_HOPLIM_SET(iphdr, IP6H_HOPLIM(iphdr) - 1);
if (IP6H_HOPLIM(iphdr) == 0) {
#if LWIP_ICMP6
if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
icmp6_time_exceeded(p, ICMP6_TE_HL);
}
#endif
IP6_STATS_INC(ip6.drop);
return;
}
if (netif->mtu && (p->tot_len > netif->mtu)) {
#if LWIP_ICMP6
if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
icmp6_packet_too_big(p, netif->mtu);
}
#endif
IP6_STATS_INC(ip6.drop);
return;
}
LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: forwarding packet to %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
netif->output_ip6(netif, p, ip6_current_dest_addr());
IP6_STATS_INC(ip6.fw);
IP6_STATS_INC(ip6.xmit);
return;
}
#endif
static int
ip6_input_accept(struct netif *netif)
{
#if TUN2SOCKS
return 1;
#endif
if (netif_is_up(netif)) {
u8_t i;
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
ip6_addr_cmp(ip6_current_dest_addr(), netif_ip6_addr(netif, i))
#if IPV6_CUSTOM_SCOPES
&& (!ip6_addr_has_zone(ip6_current_src_addr()) ||
ip6_addr_test_zone(ip6_current_src_addr(), netif))
#endif
) {
return 1;
}
}
}
return 0;
}
err_t
ip6_input(struct pbuf *p, struct netif *inp)
{
struct ip6_hdr *ip6hdr;
struct netif *netif;
const u8_t *nexth;
u16_t hlen, hlen_tot;
#if 0#endif
#if LWIP_RAW
raw_input_state_t raw_status;
#endif
LWIP_ASSERT_CORE_LOCKED();
IP6_STATS_INC(ip6.recv);
ip6hdr = (struct ip6_hdr *)p->payload;
if (IP6H_V(ip6hdr) != 6) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IPv6 packet dropped due to bad version number %"U32_F"\n",
IP6H_V(ip6hdr)));
pbuf_free(p);
IP6_STATS_INC(ip6.err);
IP6_STATS_INC(ip6.drop);
return ERR_OK;
}
#ifdef LWIP_HOOK_IP6_INPUT
if (LWIP_HOOK_IP6_INPUT(p, inp)) {
return ERR_OK;
}
#endif
if ((IP6_HLEN > p->len) || (IP6H_PLEN(ip6hdr) > (p->tot_len - IP6_HLEN))) {
if (IP6_HLEN > p->len) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
("IPv6 header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
(u16_t)IP6_HLEN, p->len));
}
if ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
("IPv6 (plen %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
(u16_t)(IP6H_PLEN(ip6hdr) + IP6_HLEN), p->tot_len));
}
pbuf_free(p);
IP6_STATS_INC(ip6.lenerr);
IP6_STATS_INC(ip6.drop);
return ERR_OK;
}
pbuf_realloc(p, (u16_t)(IP6_HLEN + IP6H_PLEN(ip6hdr)));
ip_addr_copy_from_ip6_packed(ip_data.current_iphdr_dest, ip6hdr->dest);
ip_addr_copy_from_ip6_packed(ip_data.current_iphdr_src, ip6hdr->src);
if (ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_dest)) ||
ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_src)) ||
ip6_addr_ismulticast(ip_2_ip6(&ip_data.current_iphdr_src))) {
pbuf_free(p);
IP6_STATS_INC(ip6.err);
IP6_STATS_INC(ip6.drop);
return ERR_OK;
}
ip6_addr_assign_zone(ip_2_ip6(&ip_data.current_iphdr_dest), IP6_UNKNOWN, inp);
ip6_addr_assign_zone(ip_2_ip6(&ip_data.current_iphdr_src), IP6_UNICAST, inp);
ip_data.current_ip6_header = ip6hdr;
ip_data.current_netif = inp;
ip_data.current_input_netif = inp;
if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
if (ip6_addr_isallnodes_iflocal(ip6_current_dest_addr()) ||
ip6_addr_isallnodes_linklocal(ip6_current_dest_addr())) {
netif = inp;
}
#if LWIP_IPV6_MLD
else if (mld6_lookfor_group(inp, ip6_current_dest_addr())) {
netif = inp;
}
#else
else if (ip6_addr_issolicitednode(ip6_current_dest_addr())) {
u8_t i;
netif = NULL;
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) &&
ip6_addr_cmp_solicitednode(ip6_current_dest_addr(), netif_ip6_addr(inp, i))) {
netif = inp;
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: solicited node packet accepted on interface %c%c\n",
netif->name[0], netif->name[1]));
break;
}
}
}
#endif
else {
netif = NULL;
}
} else {
if (ip6_input_accept(inp)) {
netif = inp;
} else {
netif = NULL;
#if !IPV6_CUSTOM_SCOPES
if (ip6_addr_islinklocal(ip6_current_dest_addr()) ||
ip6_addr_islinklocal(ip6_current_src_addr())) {
goto netif_found;
}
#endif
#if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
if (ip6_addr_isloopback(ip6_current_dest_addr()) ||
ip6_addr_isloopback(ip6_current_src_addr())) {
goto netif_found;
}
#endif
#if !LWIP_SINGLE_NETIF
NETIF_FOREACH(netif) {
if (netif == inp) {
continue;
}
if (ip6_input_accept(netif)) {
break;
}
}
#endif
}
netif_found:
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet accepted on interface %c%c\n",
netif ? netif->name[0] : 'X', netif? netif->name[1] : 'X'));
}
if (ip6_addr_isany(ip6_current_src_addr()) &&
(!ip6_addr_issolicitednode(ip6_current_dest_addr()))) {
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with src ANY_ADDRESS dropped\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
}
if (netif == NULL) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_TRACE, ("ip6_input: packet not for us.\n"));
#if LWIP_IPV6_FORWARD
if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
ip6_forward(p, ip6hdr, inp);
}
#endif
pbuf_free(p);
goto ip6_input_cleanup;
}
ip_data.current_netif = netif;
nexth = &IP6H_NEXTH(ip6hdr);
hlen = hlen_tot = IP6_HLEN;
pbuf_remove_header(p, IP6_HLEN);
while (*nexth != IP6_NEXTH_NONE)
{
switch (*nexth) {
case IP6_NEXTH_HOPBYHOP:
{
s32_t opt_offset;
struct ip6_hbh_hdr *hbh_hdr;
struct ip6_opt_hdr *opt_hdr;
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header\n"));
hbh_hdr = (struct ip6_hbh_hdr *)p->payload;
nexth = &IP6_HBH_NEXTH(hbh_hdr);
hlen = (u16_t)(8 * (1 + hbh_hdr->_hlen));
if ((p->len < 8) || (hlen > p->len)) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
hlen, p->len));
pbuf_free(p);
IP6_STATS_INC(ip6.lenerr);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
}
hlen_tot = (u16_t)(hlen_tot + hlen);
opt_offset = IP6_HBH_HLEN;
while (opt_offset < hlen)
{
s32_t opt_dlen = 0;
opt_hdr = (struct ip6_opt_hdr *)((u8_t *)hbh_hdr + opt_offset);
switch (IP6_OPT_TYPE(opt_hdr)) {
case IP6_PAD1_OPTION:
opt_dlen = -1;
break;
case IP6_PADN_OPTION:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
case IP6_ROUTER_ALERT_OPTION:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
case IP6_JUMBO_OPTION:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
default:
switch (IP6_OPT_TYPE_ACTION(opt_hdr)) {
case 1:
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
case 2:
icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr);
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
case 3:
if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr);
}
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
default:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
}
break;
}
opt_offset = opt_offset + IP6_OPT_HLEN + opt_dlen;
}
pbuf_remove_header(p, hlen);
break;
}
case IP6_NEXTH_DESTOPTS:
{
s32_t opt_offset;
struct ip6_dest_hdr *dest_hdr;
struct ip6_opt_hdr *opt_hdr;
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Destination options header\n"));
dest_hdr = (struct ip6_dest_hdr *)p->payload;
nexth = &IP6_DEST_NEXTH(dest_hdr);
hlen = 8 * (1 + dest_hdr->_hlen);
if ((p->len < 8) || (hlen > p->len)) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
hlen, p->len));
pbuf_free(p);
IP6_STATS_INC(ip6.lenerr);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
}
hlen_tot = (u16_t)(hlen_tot + hlen);
opt_offset = IP6_DEST_HLEN;
while (opt_offset < hlen)
{
s32_t opt_dlen = 0;
opt_hdr = (struct ip6_opt_hdr *)((u8_t *)dest_hdr + opt_offset);
switch (IP6_OPT_TYPE(opt_hdr))
{
case IP6_PAD1_OPTION:
opt_dlen = -1;
break;
case IP6_PADN_OPTION:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
case IP6_ROUTER_ALERT_OPTION:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
case IP6_JUMBO_OPTION:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
case IP6_HOME_ADDRESS_OPTION:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
default:
switch (IP6_OPT_TYPE_ACTION(opt_hdr))
{
case 1:
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
case 2:
icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr);
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
case 3:
if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr);
}
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
default:
opt_dlen = IP6_OPT_DLEN(opt_hdr);
break;
}
break;
}
opt_offset = opt_offset + IP6_OPT_HLEN + opt_dlen;
}
pbuf_remove_header(p, hlen);
break;
}
case IP6_NEXTH_ROUTING:
{
struct ip6_rout_hdr *rout_hdr;
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Routing header\n"));
rout_hdr = (struct ip6_rout_hdr *)p->payload;
nexth = &IP6_ROUT_NEXTH(rout_hdr);
hlen = 8 * (1 + rout_hdr->_hlen);
if ((p->len < 8) || (hlen > p->len)) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
hlen, p->len));
pbuf_free(p);
IP6_STATS_INC(ip6.lenerr);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
}
hlen_tot = (u16_t)(hlen_tot + hlen);
if (IP6_ROUT_SEG_LEFT(rout_hdr)) {
if (rout_hdr->_hlen & 0x1) {
icmp6_param_problem(p, ICMP6_PP_FIELD, &rout_hdr->_hlen);
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid routing type dropped\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
}
switch (IP6_ROUT_TYPE(rout_hdr))
{
case IP6_ROUT_TYPE2:
break;
case IP6_ROUT_RPL:
break;
default:
icmp6_param_problem(p, ICMP6_PP_FIELD, &IP6_ROUT_TYPE(rout_hdr));
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid routing type dropped\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
}
}
pbuf_remove_header(p, hlen);
break;
}
case IP6_NEXTH_FRAGMENT:
{
struct ip6_frag_hdr *frag_hdr;
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header\n"));
frag_hdr = (struct ip6_frag_hdr *)p->payload;
nexth = &IP6_FRAG_NEXTH(frag_hdr);
hlen = 8;
if (hlen > p->len) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
hlen, p->len));
pbuf_free(p);
IP6_FRAG_STATS_INC(ip6_frag.lenerr);
IP6_FRAG_STATS_INC(ip6_frag.drop);
goto ip6_input_cleanup;
}
hlen_tot = (u16_t)(hlen_tot + hlen);
if (IP6_FRAG_MBIT(frag_hdr) && (IP6H_PLEN(ip6hdr) & 0x7)) {
icmp6_param_problem(p, ICMP6_PP_FIELD, LWIP_PACKED_CAST(const void *, &ip6hdr->_plen));
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid payload length dropped\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
}
if ((frag_hdr->_fragment_offset &
PP_HTONS(IP6_FRAG_OFFSET_MASK | IP6_FRAG_MORE_FLAG)) == 0) {
pbuf_remove_header(p, hlen);
} else {
#if LWIP_IPV6_REASS
ip_data.current_ip_header_tot_len = hlen_tot;
p = ip6_reass(p);
if (p == NULL) {
goto ip6_input_cleanup;
}
ip6hdr = (struct ip6_hdr *)p->payload;
nexth = &IP6H_NEXTH(ip6hdr);
hlen = hlen_tot = IP6_HLEN;
pbuf_remove_header(p, IP6_HLEN);
#else
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header dropped (with LWIP_IPV6_REASS==0)\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.opterr);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
#endif
}
break;
}
default:
goto options_done;
}
if (*nexth == IP6_NEXTH_HOPBYHOP) {
icmp6_param_problem(p, ICMP6_PP_HEADER, nexth);
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header dropped (only valid as a first option)\n"));
pbuf_free(p);
IP6_STATS_INC(ip6.drop);
goto ip6_input_cleanup;
}
}
options_done:
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: \n"));
ip6_debug_print(p);
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
ip_data.current_ip_header_tot_len = hlen_tot;
#if LWIP_RAW
pbuf_add_header_force(p, hlen_tot);
raw_status = raw_input(p, inp);
if (raw_status != RAW_INPUT_EATEN)
{
pbuf_remove_header(p, hlen_tot);
#else
{
#endif
switch (*nexth) {
case IP6_NEXTH_NONE:
pbuf_free(p);
break;
#if LWIP_UDP
case IP6_NEXTH_UDP:
#if LWIP_UDPLITE
case IP6_NEXTH_UDPLITE:
#endif
udp_input(p, inp);
break;
#endif
#if LWIP_TCP
case IP6_NEXTH_TCP:
tcp_input(p, inp);
break;
#endif
#if LWIP_ICMP6
case IP6_NEXTH_ICMP6:
icmp6_input(p, inp);
break;
#endif
default:
#if LWIP_RAW
if (raw_status == RAW_INPUT_DELIVERED) {
} else
#endif
{
#if LWIP_ICMP6
pbuf_add_header_force(p, hlen_tot);
if ((!ip6_addr_ismulticast(ip6_current_dest_addr())) &&
(IP6H_NEXTH(ip6hdr) != IP6_NEXTH_ICMP6)) {
icmp6_param_problem(p, ICMP6_PP_HEADER, nexth);
}
#endif
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_input: Unsupported transport protocol %"U16_F"\n", (u16_t)IP6H_NEXTH(ip6hdr)));
IP6_STATS_INC(ip6.proterr);
IP6_STATS_INC(ip6.drop);
}
pbuf_free(p);
break;
}
}
ip6_input_cleanup:
ip_data.current_netif = NULL;
ip_data.current_input_netif = NULL;
ip_data.current_ip6_header = NULL;
ip_data.current_ip_header_tot_len = 0;
ip6_addr_set_zero(ip6_current_src_addr());
ip6_addr_set_zero(ip6_current_dest_addr());
return ERR_OK;
}
err_t
ip6_output_if(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
u8_t hl, u8_t tc,
u8_t nexth, struct netif *netif)
{
const ip6_addr_t *src_used = src;
if (dest != LWIP_IP_HDRINCL) {
if (src != NULL && ip6_addr_isany(src)) {
src_used = ip_2_ip6(ip6_select_source_address(netif, dest));
if ((src_used == NULL) || ip6_addr_isany(src_used)) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: No suitable source address for packet.\n"));
IP6_STATS_INC(ip6.rterr);
return ERR_RTE;
}
}
}
return ip6_output_if_src(p, src_used, dest, hl, tc, nexth, netif);
}
err_t
ip6_output_if_src(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
u8_t hl, u8_t tc,
u8_t nexth, struct netif *netif)
{
struct ip6_hdr *ip6hdr;
ip6_addr_t dest_addr;
LWIP_ASSERT_CORE_LOCKED();
LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
if (dest != LWIP_IP_HDRINCL) {
#if LWIP_IPV6_SCOPES
if (ip6_addr_lacks_zone(dest, IP6_UNKNOWN)) {
ip6_addr_copy(dest_addr, *dest);
ip6_addr_assign_zone(&dest_addr, IP6_UNKNOWN, netif);
dest = &dest_addr;
}
#endif
if (pbuf_add_header(p, IP6_HLEN)) {
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: not enough room for IPv6 header in pbuf\n"));
IP6_STATS_INC(ip6.err);
return ERR_BUF;
}
ip6hdr = (struct ip6_hdr *)p->payload;
LWIP_ASSERT("check that first pbuf can hold struct ip6_hdr",
(p->len >= sizeof(struct ip6_hdr)));
IP6H_HOPLIM_SET(ip6hdr, hl);
IP6H_NEXTH_SET(ip6hdr, nexth);
ip6_addr_copy_to_packed(ip6hdr->dest, *dest);
IP6H_VTCFL_SET(ip6hdr, 6, tc, 0);
IP6H_PLEN_SET(ip6hdr, (u16_t)(p->tot_len - IP6_HLEN));
if (src == NULL) {
src = IP6_ADDR_ANY6;
}
ip6_addr_copy_to_packed(ip6hdr->src, *src);
} else {
ip6hdr = (struct ip6_hdr *)p->payload;
ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
ip6_addr_assign_zone(&dest_addr, IP6_UNKNOWN, netif);
dest = &dest_addr;
}
IP6_STATS_INC(ip6.xmit);
LWIP_DEBUGF(IP6_DEBUG, ("ip6_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));
ip6_debug_print(p);
#if ENABLE_LOOPBACK
{
int i;
#if !LWIP_HAVE_LOOPIF
if (ip6_addr_isloopback(dest)) {
return netif_loop_output(netif, p);
}
#endif
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
ip6_addr_cmp(dest, netif_ip6_addr(netif, i))) {
LWIP_DEBUGF(IP6_DEBUG, ("netif_loop_output()\n"));
return netif_loop_output(netif, p);
}
}
}
#if LWIP_MULTICAST_TX_OPTIONS
if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
netif_loop_output(netif, p);
}
#endif
#endif
#if LWIP_IPV6_FRAG
if (netif_mtu6(netif) && (p->tot_len > nd6_get_destination_mtu(dest, netif))) {
return ip6_frag(p, netif, dest);
}
#endif
LWIP_DEBUGF(IP6_DEBUG, ("netif->output_ip6()\n"));
return netif->output_ip6(netif, p, dest);
}
err_t
ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
u8_t hl, u8_t tc, u8_t nexth)
{
struct netif *netif;
struct ip6_hdr *ip6hdr;
ip6_addr_t src_addr, dest_addr;
LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
if (dest != LWIP_IP_HDRINCL) {
netif = ip6_route(src, dest);
} else {
ip6hdr = (struct ip6_hdr *)p->payload;
ip6_addr_copy_from_packed(src_addr, ip6hdr->src);
ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
netif = ip6_route(&src_addr, &dest_addr);
}
if (netif == NULL) {
LWIP_DEBUGF(IP6_DEBUG, ("ip6_output: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
IP6_ADDR_BLOCK1(dest),
IP6_ADDR_BLOCK2(dest),
IP6_ADDR_BLOCK3(dest),
IP6_ADDR_BLOCK4(dest),
IP6_ADDR_BLOCK5(dest),
IP6_ADDR_BLOCK6(dest),
IP6_ADDR_BLOCK7(dest),
IP6_ADDR_BLOCK8(dest)));
IP6_STATS_INC(ip6.rterr);
return ERR_RTE;
}
return ip6_output_if(p, src, dest, hl, tc, nexth, netif);
}
#if LWIP_NETIF_USE_HINTS
err_t
ip6_output_hinted(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
u8_t hl, u8_t tc, u8_t nexth, struct netif_hint *netif_hint)
{
struct netif *netif;
struct ip6_hdr *ip6hdr;
ip6_addr_t src_addr, dest_addr;
err_t err;
LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
if (dest != LWIP_IP_HDRINCL) {
netif = ip6_route(src, dest);
} else {
ip6hdr = (struct ip6_hdr *)p->payload;
ip6_addr_copy_from_packed(src_addr, ip6hdr->src);
ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
netif = ip6_route(&src_addr, &dest_addr);
}
if (netif == NULL) {
LWIP_DEBUGF(IP6_DEBUG, ("ip6_output: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
IP6_ADDR_BLOCK1(dest),
IP6_ADDR_BLOCK2(dest),
IP6_ADDR_BLOCK3(dest),
IP6_ADDR_BLOCK4(dest),
IP6_ADDR_BLOCK5(dest),
IP6_ADDR_BLOCK6(dest),
IP6_ADDR_BLOCK7(dest),
IP6_ADDR_BLOCK8(dest)));
IP6_STATS_INC(ip6.rterr);
return ERR_RTE;
}
NETIF_SET_HINTS(netif, netif_hint);
err = ip6_output_if(p, src, dest, hl, tc, nexth, netif);
NETIF_RESET_HINTS(netif);
return err;
}
#endif
#if LWIP_IPV6_MLD
err_t
ip6_options_add_hbh_ra(struct pbuf *p, u8_t nexth, u8_t value)
{
u8_t *opt_data;
u32_t offset = 0;
struct ip6_hbh_hdr *hbh_hdr;
struct ip6_opt_hdr *opt_hdr;
const u8_t hlen = (sizeof(struct ip6_opt_hdr) * 2) + IP6_ROUTER_ALERT_DLEN;
if (pbuf_add_header(p, sizeof(struct ip6_hbh_hdr) + hlen)) {
LWIP_DEBUGF(IP6_DEBUG, ("ip6_options: no space for options header\n"));
IP6_STATS_INC(ip6.err);
return ERR_BUF;
}
hbh_hdr = (struct ip6_hbh_hdr *)p->payload;
IP6_HBH_NEXTH(hbh_hdr) = nexth;
hbh_hdr->_hlen = 0;
offset = IP6_HBH_HLEN;
opt_hdr = (struct ip6_opt_hdr *)((u8_t *)hbh_hdr + offset);
IP6_OPT_TYPE(opt_hdr) = IP6_ROUTER_ALERT_OPTION;
IP6_OPT_DLEN(opt_hdr) = IP6_ROUTER_ALERT_DLEN;
offset += IP6_OPT_HLEN;
opt_data = (u8_t *)hbh_hdr + offset;
opt_data[0] = value;
opt_data[1] = 0;
offset += IP6_OPT_DLEN(opt_hdr);
opt_hdr = (struct ip6_opt_hdr *)((u8_t *)hbh_hdr + offset);
IP6_OPT_TYPE(opt_hdr) = IP6_PADN_OPTION;
IP6_OPT_DLEN(opt_hdr) = 0;
return ERR_OK;
}
#endif
#if IP6_DEBUG
void
ip6_debug_print(struct pbuf *p)
{
struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
LWIP_DEBUGF(IP6_DEBUG, ("IPv6 header:\n"));
LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(IP6_DEBUG, ("| %2"U16_F" | %3"U16_F" | %7"U32_F" | (ver, class, flow)\n",
IP6H_V(ip6hdr),
IP6H_TC(ip6hdr),
IP6H_FL(ip6hdr)));
LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(IP6_DEBUG, ("| %5"U16_F" | %3"U16_F" | %3"U16_F" | (plen, nexth, hopl)\n",
IP6H_PLEN(ip6hdr),
IP6H_NEXTH(ip6hdr),
IP6H_HOPLIM(ip6hdr)));
LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" | (src)\n",
IP6_ADDR_BLOCK1(&(ip6hdr->src)),
IP6_ADDR_BLOCK2(&(ip6hdr->src)),
IP6_ADDR_BLOCK3(&(ip6hdr->src)),
IP6_ADDR_BLOCK4(&(ip6hdr->src))));
LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" |\n",
IP6_ADDR_BLOCK5(&(ip6hdr->src)),
IP6_ADDR_BLOCK6(&(ip6hdr->src)),
IP6_ADDR_BLOCK7(&(ip6hdr->src)),
IP6_ADDR_BLOCK8(&(ip6hdr->src))));
LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" | (dest)\n",
IP6_ADDR_BLOCK1(&(ip6hdr->dest)),
IP6_ADDR_BLOCK2(&(ip6hdr->dest)),
IP6_ADDR_BLOCK3(&(ip6hdr->dest)),
IP6_ADDR_BLOCK4(&(ip6hdr->dest))));
LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" |\n",
IP6_ADDR_BLOCK5(&(ip6hdr->dest)),
IP6_ADDR_BLOCK6(&(ip6hdr->dest)),
IP6_ADDR_BLOCK7(&(ip6hdr->dest)),
IP6_ADDR_BLOCK8(&(ip6hdr->dest))));
LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
}
#endif
#endif