2016-05-19 17:42:01 +08:00
|
|
|
/** @file
|
|
|
|
* @brief ICMPv6 related functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-05-19 17:42:01 +08:00
|
|
|
*/
|
|
|
|
|
2016-12-15 20:55:01 +08:00
|
|
|
#if defined(CONFIG_NET_DEBUG_ICMPV6)
|
2016-05-19 17:42:01 +08:00
|
|
|
#define SYS_LOG_DOMAIN "net/icmpv6"
|
2016-12-15 20:55:01 +08:00
|
|
|
#define NET_LOG_ENABLED 1
|
2016-05-19 17:42:01 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <misc/slist.h>
|
2016-10-24 18:17:31 +08:00
|
|
|
#include <misc/byteorder.h>
|
2016-05-19 17:42:01 +08:00
|
|
|
#include <net/net_core.h>
|
2017-04-03 23:14:35 +08:00
|
|
|
#include <net/net_pkt.h>
|
2016-05-19 17:42:01 +08:00
|
|
|
#include <net/net_if.h>
|
|
|
|
#include "net_private.h"
|
|
|
|
#include "icmpv6.h"
|
2016-11-03 20:44:44 +08:00
|
|
|
#include "ipv6.h"
|
2016-12-13 21:50:31 +08:00
|
|
|
#include "net_stats.h"
|
2016-05-19 17:42:01 +08:00
|
|
|
|
2017-03-03 22:42:29 +08:00
|
|
|
#if defined(CONFIG_NET_RPL)
|
|
|
|
#include "rpl.h"
|
|
|
|
#endif
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
#define PKT_WAIT_TIME K_SECONDS(1)
|
2017-03-15 16:15:45 +08:00
|
|
|
|
2016-05-19 17:42:01 +08:00
|
|
|
static sys_slist_t handlers;
|
|
|
|
|
2017-03-29 20:53:59 +08:00
|
|
|
const char *net_icmpv6_type2str(int icmpv6_type)
|
|
|
|
{
|
|
|
|
switch (icmpv6_type) {
|
|
|
|
case NET_ICMPV6_DST_UNREACH:
|
|
|
|
return "Destination Unreachable";
|
|
|
|
case NET_ICMPV6_PACKET_TOO_BIG:
|
|
|
|
return "Packet Too Big";
|
|
|
|
case NET_ICMPV6_TIME_EXCEEDED:
|
|
|
|
return "Time Exceeded";
|
|
|
|
case NET_ICMPV6_PARAM_PROBLEM:
|
|
|
|
return "IPv6 Bad Header";
|
|
|
|
case NET_ICMPV6_ECHO_REQUEST:
|
|
|
|
return "Echo Request";
|
|
|
|
case NET_ICMPV6_ECHO_REPLY:
|
|
|
|
return "Echo Reply";
|
|
|
|
case NET_ICMPV6_MLD_QUERY:
|
|
|
|
return "Multicast Listener Query";
|
|
|
|
case NET_ICMPV6_RS:
|
|
|
|
return "Router Solicitation";
|
|
|
|
case NET_ICMPV6_RA:
|
|
|
|
return "Router Advertisement";
|
|
|
|
case NET_ICMPV6_NS:
|
|
|
|
return "Neighbor Solicitation";
|
|
|
|
case NET_ICMPV6_NA:
|
|
|
|
return "Neighbor Advertisement";
|
|
|
|
case NET_ICMPV6_MLDv2:
|
|
|
|
return "Multicast Listener Report v2";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
2016-05-19 17:42:01 +08:00
|
|
|
void net_icmpv6_register_handler(struct net_icmpv6_handler *handler)
|
|
|
|
{
|
|
|
|
sys_slist_prepend(&handlers, &handler->node);
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:15:43 +08:00
|
|
|
void net_icmpv6_unregister_handler(struct net_icmpv6_handler *handler)
|
|
|
|
{
|
|
|
|
sys_slist_find_and_remove(&handlers, &handler->node);
|
|
|
|
}
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
static inline void setup_ipv6_header(struct net_pkt *pkt, uint16_t extra_len,
|
2016-11-24 21:25:50 +08:00
|
|
|
uint8_t hop_limit, uint8_t icmp_type,
|
|
|
|
uint8_t icmp_code)
|
2016-05-19 17:42:01 +08:00
|
|
|
{
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_IPV6_HDR(pkt)->vtc = 0x60;
|
|
|
|
NET_IPV6_HDR(pkt)->tcflow = 0;
|
|
|
|
NET_IPV6_HDR(pkt)->flow = 0;
|
2016-11-24 21:25:50 +08:00
|
|
|
|
|
|
|
sys_put_be16(NET_ICMPH_LEN + extra_len + NET_ICMPV6_UNUSED_LEN,
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_IPV6_HDR(pkt)->len);
|
2016-11-24 21:25:50 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_IPV6_HDR(pkt)->nexthdr = IPPROTO_ICMPV6;
|
|
|
|
NET_IPV6_HDR(pkt)->hop_limit = hop_limit;
|
2016-11-24 21:25:50 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr));
|
2016-11-24 21:25:50 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_ICMP_HDR(pkt)->type = icmp_type;
|
|
|
|
NET_ICMP_HDR(pkt)->code = icmp_code;
|
2016-11-24 21:25:50 +08:00
|
|
|
|
|
|
|
/* ICMPv6 header has 4 unused bytes that must be zero, RFC 4443 ch 3.1
|
2016-05-19 17:42:01 +08:00
|
|
|
*/
|
2017-04-05 14:37:44 +08:00
|
|
|
memset(net_pkt_icmp_data(pkt) + sizeof(struct net_icmp_hdr), 0,
|
2016-11-24 21:25:50 +08:00
|
|
|
NET_ICMPV6_UNUSED_LEN);
|
|
|
|
}
|
2016-05-19 17:42:01 +08:00
|
|
|
|
2017-03-03 16:10:51 +08:00
|
|
|
#if defined(CONFIG_NET_DEBUG_ICMPV6)
|
2017-04-05 14:37:44 +08:00
|
|
|
static inline void echo_request_debug(struct net_pkt *pkt)
|
2017-03-03 16:10:51 +08:00
|
|
|
{
|
|
|
|
char out[NET_IPV6_ADDR_LEN];
|
|
|
|
|
|
|
|
snprintk(out, sizeof(out), "%s",
|
2017-04-10 19:03:41 +08:00
|
|
|
net_sprint_ipv6_addr(&NET_IPV6_HDR(pkt)->dst));
|
2017-03-03 16:10:51 +08:00
|
|
|
NET_DBG("Received Echo Request from %s to %s",
|
2017-04-10 19:03:41 +08:00
|
|
|
net_sprint_ipv6_addr(&NET_IPV6_HDR(pkt)->src), out);
|
2017-03-03 16:10:51 +08:00
|
|
|
}
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
static inline void echo_reply_debug(struct net_pkt *pkt)
|
2017-03-03 16:10:51 +08:00
|
|
|
{
|
|
|
|
char out[NET_IPV6_ADDR_LEN];
|
|
|
|
|
|
|
|
snprintk(out, sizeof(out), "%s",
|
2017-04-10 19:03:41 +08:00
|
|
|
net_sprint_ipv6_addr(&NET_IPV6_HDR(pkt)->dst));
|
2017-03-03 16:10:51 +08:00
|
|
|
NET_DBG("Sending Echo Reply from %s to %s",
|
2017-04-10 19:03:41 +08:00
|
|
|
net_sprint_ipv6_addr(&NET_IPV6_HDR(pkt)->src), out);
|
2017-03-03 16:10:51 +08:00
|
|
|
}
|
|
|
|
#else
|
2017-04-05 14:37:44 +08:00
|
|
|
#define echo_request_debug(pkt)
|
|
|
|
#define echo_reply_debug(pkt)
|
2017-03-03 16:10:51 +08:00
|
|
|
#endif /* CONFIG_NET_DEBUG_ICMPV6 */
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
static enum net_verdict handle_echo_request(struct net_pkt *orig)
|
2016-11-24 21:25:50 +08:00
|
|
|
{
|
2017-04-05 14:37:44 +08:00
|
|
|
struct net_pkt *pkt;
|
2017-03-03 16:03:33 +08:00
|
|
|
struct net_buf *frag;
|
2016-11-24 21:25:50 +08:00
|
|
|
struct net_if *iface;
|
|
|
|
uint16_t payload_len;
|
|
|
|
|
2017-03-03 16:10:51 +08:00
|
|
|
echo_request_debug(orig);
|
2016-05-19 17:42:01 +08:00
|
|
|
|
2017-04-03 23:14:35 +08:00
|
|
|
iface = net_pkt_iface(orig);
|
2016-11-24 21:25:50 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
pkt = net_pkt_get_reserve_tx(0, PKT_WAIT_TIME);
|
|
|
|
if (!pkt) {
|
|
|
|
goto drop_no_pkt;
|
2017-03-15 16:15:45 +08:00
|
|
|
}
|
2016-11-24 21:25:50 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
payload_len = sys_get_be16(NET_IPV6_HDR(orig)->len) -
|
2016-11-24 21:25:50 +08:00
|
|
|
sizeof(NET_ICMPH_LEN) - NET_ICMPV6_UNUSED_LEN;
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
frag = net_pkt_copy_all(orig, 0, PKT_WAIT_TIME);
|
2016-11-24 21:25:50 +08:00
|
|
|
if (!frag) {
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_frag_add(pkt, frag);
|
|
|
|
net_pkt_set_family(pkt, AF_INET6);
|
|
|
|
net_pkt_set_iface(pkt, iface);
|
|
|
|
net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag));
|
|
|
|
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr));
|
2016-11-24 21:25:50 +08:00
|
|
|
|
2017-04-12 17:29:19 +08:00
|
|
|
if (net_pkt_ipv6_ext_len(orig)) {
|
|
|
|
net_pkt_set_ipv6_ext_len(pkt, net_pkt_ipv6_ext_len(orig));
|
2017-03-03 22:42:29 +08:00
|
|
|
} else {
|
2017-04-12 17:29:19 +08:00
|
|
|
net_pkt_set_ipv6_ext_len(pkt, 0);
|
2017-03-03 22:42:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up IPv6 Header fields */
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_IPV6_HDR(pkt)->vtc = 0x60;
|
|
|
|
NET_IPV6_HDR(pkt)->tcflow = 0;
|
|
|
|
NET_IPV6_HDR(pkt)->flow = 0;
|
|
|
|
NET_IPV6_HDR(pkt)->hop_limit = net_if_ipv6_get_hop_limit(iface);
|
2017-03-03 22:42:29 +08:00
|
|
|
|
|
|
|
/* ICMPv6 fields */
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_ICMP_HDR(pkt)->type = NET_ICMPV6_ECHO_REPLY;
|
|
|
|
NET_ICMP_HDR(pkt)->code = 0;
|
|
|
|
NET_ICMP_HDR(pkt)->chksum = 0;
|
2016-11-24 21:25:50 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
if (net_is_ipv6_addr_mcast(&NET_IPV6_HDR(pkt)->dst)) {
|
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->dst,
|
|
|
|
&NET_IPV6_HDR(orig)->src);
|
2016-05-19 17:42:01 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->src,
|
2017-03-03 16:03:33 +08:00
|
|
|
net_if_ipv6_select_src_addr(iface,
|
2017-04-10 19:03:41 +08:00
|
|
|
&NET_IPV6_HDR(orig)->dst));
|
2016-05-19 17:42:01 +08:00
|
|
|
} else {
|
|
|
|
struct in6_addr addr;
|
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
net_ipaddr_copy(&addr, &NET_IPV6_HDR(orig)->src);
|
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->src,
|
|
|
|
&NET_IPV6_HDR(orig)->dst);
|
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->dst, &addr);
|
2016-05-19 17:42:01 +08:00
|
|
|
}
|
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
if (NET_IPV6_HDR(pkt)->nexthdr == NET_IPV6_NEXTHDR_HBHO) {
|
2017-03-03 22:42:29 +08:00
|
|
|
#if defined(CONFIG_NET_RPL)
|
|
|
|
uint16_t offset = NET_IPV6H_LEN;
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
if (net_rpl_revert_header(pkt, offset, &offset) < 0) {
|
2017-03-03 22:42:29 +08:00
|
|
|
/* TODO: Handle error cases */
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(orig)->addr;
|
|
|
|
net_pkt_ll_src(pkt)->len = net_pkt_ll_dst(orig)->len;
|
2016-11-29 21:32:33 +08:00
|
|
|
|
|
|
|
/* We must not set the destination ll address here but trust
|
|
|
|
* that it is set properly using a value from neighbor cache.
|
|
|
|
*/
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_ll_dst(pkt)->addr = NULL;
|
2016-11-24 21:25:50 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_ICMP_HDR(pkt)->chksum = 0;
|
|
|
|
NET_ICMP_HDR(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt);
|
2016-05-19 17:42:01 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
echo_reply_debug(pkt);
|
2016-05-19 17:42:01 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
if (net_send_data(pkt) < 0) {
|
2016-11-24 21:25:50 +08:00
|
|
|
goto drop;
|
2016-05-19 17:42:01 +08:00
|
|
|
}
|
|
|
|
|
2017-04-03 23:14:35 +08:00
|
|
|
net_pkt_unref(orig);
|
2016-12-13 21:50:31 +08:00
|
|
|
net_stats_update_icmp_sent();
|
2016-05-19 17:42:01 +08:00
|
|
|
|
2017-03-03 16:13:36 +08:00
|
|
|
return NET_OK;
|
2016-05-19 17:42:01 +08:00
|
|
|
|
2016-11-24 21:25:50 +08:00
|
|
|
drop:
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_unref(pkt);
|
2017-03-15 16:15:45 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
drop_no_pkt:
|
2016-12-13 21:50:31 +08:00
|
|
|
net_stats_update_icmp_drop();
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2016-11-24 21:25:50 +08:00
|
|
|
return NET_DROP;
|
2016-06-14 14:42:01 +08:00
|
|
|
}
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
int net_icmpv6_send_error(struct net_pkt *orig, uint8_t type, uint8_t code,
|
2016-10-24 18:17:31 +08:00
|
|
|
uint32_t param)
|
2016-06-14 14:42:01 +08:00
|
|
|
{
|
2017-04-05 14:37:44 +08:00
|
|
|
struct net_pkt *pkt;
|
|
|
|
struct net_buf *frag;
|
2016-06-14 14:42:01 +08:00
|
|
|
struct net_if *iface;
|
|
|
|
size_t extra_len, reserve;
|
2017-03-15 16:15:45 +08:00
|
|
|
int err = -EIO;
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
if (NET_IPV6_HDR(orig)->nexthdr == IPPROTO_ICMPV6) {
|
|
|
|
if (NET_ICMP_HDR(orig)->code < 128) {
|
2016-06-14 14:42:01 +08:00
|
|
|
/* We must not send ICMP errors back */
|
2017-03-15 16:15:45 +08:00
|
|
|
err = -EINVAL;
|
2017-04-05 14:37:44 +08:00
|
|
|
goto drop_no_pkt;
|
2016-06-14 14:42:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 23:14:35 +08:00
|
|
|
iface = net_pkt_iface(orig);
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
pkt = net_pkt_get_reserve_tx(0, PKT_WAIT_TIME);
|
|
|
|
if (!pkt) {
|
2017-03-15 16:15:45 +08:00
|
|
|
err = -ENOMEM;
|
2017-04-05 14:37:44 +08:00
|
|
|
goto drop_no_pkt;
|
2017-03-15 16:15:45 +08:00
|
|
|
}
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2016-10-24 18:17:31 +08:00
|
|
|
/* There is unsed part in ICMPv6 error msg header what we might need
|
|
|
|
* to store the param variable.
|
|
|
|
*/
|
2016-06-14 14:42:01 +08:00
|
|
|
reserve = sizeof(struct net_ipv6_hdr) + sizeof(struct net_icmp_hdr) +
|
|
|
|
NET_ICMPV6_UNUSED_LEN;
|
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
if (NET_IPV6_HDR(orig)->nexthdr == IPPROTO_UDP) {
|
2016-06-14 14:42:01 +08:00
|
|
|
extra_len = sizeof(struct net_ipv6_hdr) +
|
|
|
|
sizeof(struct net_udp_hdr);
|
2017-04-10 19:03:41 +08:00
|
|
|
} else if (NET_IPV6_HDR(orig)->nexthdr == IPPROTO_TCP) {
|
2017-02-22 19:51:21 +08:00
|
|
|
extra_len = sizeof(struct net_ipv6_hdr) +
|
|
|
|
sizeof(struct net_tcp_hdr);
|
2016-06-14 14:42:01 +08:00
|
|
|
} else {
|
2017-04-03 23:14:35 +08:00
|
|
|
size_t space = CONFIG_NET_BUF_DATA_SIZE -
|
2017-03-20 22:14:50 +08:00
|
|
|
net_if_get_ll_reserve(iface,
|
2017-04-10 19:03:41 +08:00
|
|
|
&NET_IPV6_HDR(orig)->dst);
|
2016-06-14 14:42:01 +08:00
|
|
|
|
|
|
|
if (reserve > space) {
|
|
|
|
extra_len = 0;
|
|
|
|
} else {
|
|
|
|
extra_len = space - reserve;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We only copy minimal IPv6 + next header from original message.
|
|
|
|
* This is so that the memory pressure is minimized.
|
|
|
|
*/
|
2017-04-05 14:37:44 +08:00
|
|
|
frag = net_pkt_copy(orig, extra_len, reserve, PKT_WAIT_TIME);
|
2016-06-14 14:42:01 +08:00
|
|
|
if (!frag) {
|
2017-03-15 16:15:45 +08:00
|
|
|
err = -ENOMEM;
|
2016-06-14 14:42:01 +08:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_frag_add(pkt, frag);
|
|
|
|
net_pkt_set_family(pkt, AF_INET6);
|
|
|
|
net_pkt_set_iface(pkt, iface);
|
|
|
|
net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag));
|
2017-04-12 17:29:19 +08:00
|
|
|
net_pkt_set_ipv6_ext_len(pkt, 0);
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
setup_ipv6_header(pkt, extra_len, net_if_ipv6_get_hop_limit(iface),
|
2016-06-14 14:42:01 +08:00
|
|
|
type, code);
|
|
|
|
|
2016-10-24 18:17:31 +08:00
|
|
|
/* Depending on error option, we store the param into the ICMP message.
|
|
|
|
*/
|
|
|
|
if (type == NET_ICMPV6_PARAM_PROBLEM) {
|
2017-04-05 14:37:44 +08:00
|
|
|
sys_put_be32(param, net_pkt_icmp_data(pkt) +
|
2016-10-24 18:17:31 +08:00
|
|
|
sizeof(struct net_icmp_hdr));
|
|
|
|
}
|
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
if (net_is_ipv6_addr_mcast(&NET_IPV6_HDR(orig)->dst)) {
|
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->dst,
|
|
|
|
&NET_IPV6_HDR(orig)->src);
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->src,
|
2017-03-20 22:14:50 +08:00
|
|
|
net_if_ipv6_select_src_addr(iface,
|
2017-04-10 19:03:41 +08:00
|
|
|
&NET_IPV6_HDR(orig)->dst));
|
2016-06-14 14:42:01 +08:00
|
|
|
} else {
|
|
|
|
struct in6_addr addr;
|
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
net_ipaddr_copy(&addr, &NET_IPV6_HDR(orig)->src);
|
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->src,
|
|
|
|
&NET_IPV6_HDR(orig)->dst);
|
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->dst, &addr);
|
2016-06-14 14:42:01 +08:00
|
|
|
}
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(orig)->addr;
|
|
|
|
net_pkt_ll_src(pkt)->len = net_pkt_ll_dst(orig)->len;
|
|
|
|
net_pkt_ll_dst(pkt)->addr = net_pkt_ll_src(orig)->addr;
|
|
|
|
net_pkt_ll_dst(pkt)->len = net_pkt_ll_src(orig)->len;
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_ICMP_HDR(pkt)->chksum = 0;
|
|
|
|
NET_ICMP_HDR(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt);
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2016-12-15 20:55:01 +08:00
|
|
|
#if defined(CONFIG_NET_DEBUG_ICMPV6)
|
2016-06-14 14:42:01 +08:00
|
|
|
do {
|
2016-10-26 02:50:44 +08:00
|
|
|
char out[NET_IPV6_ADDR_LEN];
|
2017-01-23 19:05:40 +08:00
|
|
|
snprintk(out, sizeof(out), "%s",
|
2017-04-10 19:03:41 +08:00
|
|
|
net_sprint_ipv6_addr(&NET_IPV6_HDR(pkt)->dst));
|
2016-10-24 18:17:31 +08:00
|
|
|
NET_DBG("Sending ICMPv6 Error Message type %d code %d param %d"
|
|
|
|
" from %s to %s", type, code, param,
|
2017-04-10 19:03:41 +08:00
|
|
|
net_sprint_ipv6_addr(&NET_IPV6_HDR(pkt)->src), out);
|
2016-06-14 14:42:01 +08:00
|
|
|
} while (0);
|
2016-12-15 20:55:01 +08:00
|
|
|
#endif /* CONFIG_NET_DEBUG_ICMPV6 */
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
if (net_send_data(pkt) >= 0) {
|
2016-12-13 21:50:31 +08:00
|
|
|
net_stats_update_icmp_sent();
|
2017-03-15 18:40:51 +08:00
|
|
|
return 0;
|
2016-06-14 14:42:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
drop:
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_unref(pkt);
|
2017-03-15 16:15:45 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
drop_no_pkt:
|
2016-12-13 21:50:31 +08:00
|
|
|
net_stats_update_icmp_drop();
|
2016-06-14 14:42:01 +08:00
|
|
|
|
2017-03-15 16:15:45 +08:00
|
|
|
return err;
|
2016-06-14 14:42:01 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 20:44:44 +08:00
|
|
|
int net_icmpv6_send_echo_request(struct net_if *iface,
|
|
|
|
struct in6_addr *dst,
|
|
|
|
uint16_t identifier,
|
|
|
|
uint16_t sequence)
|
|
|
|
{
|
|
|
|
const struct in6_addr *src;
|
2017-04-05 14:37:44 +08:00
|
|
|
struct net_pkt *pkt;
|
2016-11-03 20:44:44 +08:00
|
|
|
|
|
|
|
src = net_if_ipv6_select_src_addr(iface, dst);
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
pkt = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, dst),
|
2017-02-24 21:28:17 +08:00
|
|
|
K_FOREVER);
|
2016-11-03 20:44:44 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
pkt = net_ipv6_create_raw(pkt, src, dst, iface, IPPROTO_ICMPV6);
|
2016-11-03 20:44:44 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_set_family(pkt, AF_INET6);
|
|
|
|
net_pkt_set_iface(pkt, iface);
|
2016-11-03 20:44:44 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_append_u8(pkt, NET_ICMPV6_ECHO_REQUEST);
|
|
|
|
net_pkt_append_u8(pkt, 0); /* code */
|
|
|
|
net_pkt_append_be16(pkt, 0); /* checksum */
|
|
|
|
net_pkt_append_be16(pkt, identifier);
|
|
|
|
net_pkt_append_be16(pkt, sequence);
|
2016-11-03 20:44:44 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->src, src);
|
|
|
|
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->dst, dst);
|
2016-11-03 20:44:44 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_ICMP_HDR(pkt)->chksum = 0;
|
|
|
|
NET_ICMP_HDR(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt);
|
2016-11-03 20:44:44 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
if (net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6) < 0) {
|
2017-03-10 17:27:32 +08:00
|
|
|
goto drop;
|
|
|
|
}
|
2016-11-03 20:44:44 +08:00
|
|
|
|
2016-12-15 20:55:01 +08:00
|
|
|
#if defined(CONFIG_NET_DEBUG_ICMPV6)
|
2016-11-03 20:44:44 +08:00
|
|
|
do {
|
|
|
|
char out[NET_IPV6_ADDR_LEN];
|
|
|
|
|
2017-01-23 19:05:40 +08:00
|
|
|
snprintk(out, sizeof(out), "%s",
|
2017-04-10 19:03:41 +08:00
|
|
|
net_sprint_ipv6_addr(&NET_IPV6_HDR(pkt)->dst));
|
2016-11-03 20:44:44 +08:00
|
|
|
NET_DBG("Sending ICMPv6 Echo Request type %d"
|
|
|
|
" from %s to %s", NET_ICMPV6_ECHO_REQUEST,
|
2017-04-10 19:03:41 +08:00
|
|
|
net_sprint_ipv6_addr(&NET_IPV6_HDR(pkt)->src), out);
|
2016-11-03 20:44:44 +08:00
|
|
|
} while (0);
|
2016-12-15 20:55:01 +08:00
|
|
|
#endif /* CONFIG_NET_DEBUG_ICMPV6 */
|
2016-11-03 20:44:44 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
if (net_send_data(pkt) >= 0) {
|
2016-12-13 21:50:31 +08:00
|
|
|
net_stats_update_icmp_sent();
|
2016-11-03 20:44:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:27:32 +08:00
|
|
|
drop:
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_unref(pkt);
|
2016-12-13 21:50:31 +08:00
|
|
|
net_stats_update_icmp_drop();
|
2016-11-03 20:44:44 +08:00
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
enum net_verdict net_icmpv6_input(struct net_pkt *pkt,
|
2016-05-19 17:42:01 +08:00
|
|
|
uint8_t type, uint8_t code)
|
|
|
|
{
|
|
|
|
struct net_icmpv6_handler *cb;
|
|
|
|
|
2017-03-15 17:02:26 +08:00
|
|
|
net_stats_update_icmp_recv();
|
|
|
|
|
2017-02-08 22:08:23 +08:00
|
|
|
SYS_SLIST_FOR_EACH_CONTAINER(&handlers, cb, node) {
|
2016-05-19 17:42:01 +08:00
|
|
|
if (cb->type == type && (cb->code == code || cb->code == 0)) {
|
2017-04-05 14:37:44 +08:00
|
|
|
return cb->handler(pkt);
|
2016-05-19 17:42:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 17:02:26 +08:00
|
|
|
net_stats_update_icmp_drop();
|
|
|
|
|
2016-05-19 17:42:01 +08:00
|
|
|
return NET_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct net_icmpv6_handler echo_request_handler = {
|
|
|
|
.type = NET_ICMPV6_ECHO_REQUEST,
|
|
|
|
.code = 0,
|
|
|
|
.handler = handle_echo_request,
|
|
|
|
};
|
|
|
|
|
|
|
|
void net_icmpv6_init(void)
|
|
|
|
{
|
|
|
|
net_icmpv6_register_handler(&echo_request_handler);
|
|
|
|
}
|