From 7c09695344d8f865809f094e6ac2e37f10caae69 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 5 Mar 2019 18:38:53 +0100 Subject: [PATCH] net/icmpv6: Allow for arbitrary payload data in ICMP echo Allow for including arbitrary data in net_icmpv6_send_echo_request() that will be echoed verbatim by the receiver. This allows to use ICMP echo for diagnostic use cases, e.g. by testing packet framentation (large payload) or measuring round-trip-time. Signed-off-by: Benjamin Valentin --- samples/net/zperf/src/zperf_shell.c | 2 +- subsys/net/ip/icmpv6.c | 8 ++++++-- subsys/net/ip/icmpv6.h | 7 ++++++- subsys/net/ip/net_shell.c | 4 +++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/samples/net/zperf/src/zperf_shell.c b/samples/net/zperf/src/zperf_shell.c index 7ddac6b46a..f8eaff8edb 100644 --- a/samples/net/zperf/src/zperf_shell.c +++ b/samples/net/zperf/src/zperf_shell.c @@ -561,7 +561,7 @@ static int execute_upload(const struct shell *shell, * some time and start the test after that. */ net_icmpv6_send_echo_request(net_if_get_default(), - &ipv6->sin6_addr, 0, 0); + &ipv6->sin6_addr, 0, 0, NULL, 0); k_sleep(K_SECONDS(1)); } diff --git a/subsys/net/ip/icmpv6.c b/subsys/net/ip/icmpv6.c index 8d9d0f7f8f..fe78293f42 100644 --- a/subsys/net/ip/icmpv6.c +++ b/subsys/net/ip/icmpv6.c @@ -297,7 +297,9 @@ drop_no_pkt: int net_icmpv6_send_echo_request(struct net_if *iface, struct in6_addr *dst, u16_t identifier, - u16_t sequence) + u16_t sequence, + const void *data, + size_t data_size) { NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(icmpv6_access, struct net_icmpv6_echo_req); @@ -309,7 +311,8 @@ int net_icmpv6_send_echo_request(struct net_if *iface, src = net_if_ipv6_select_src_addr(iface, dst); pkt = net_pkt_alloc_with_buffer(iface, - sizeof(struct net_icmpv6_echo_req), + sizeof(struct net_icmpv6_echo_req) + + data_size, AF_INET6, IPPROTO_ICMPV6, PKT_WAIT_TIME); if (!pkt) { @@ -331,6 +334,7 @@ int net_icmpv6_send_echo_request(struct net_if *iface, echo_req->sequence = htons(sequence); net_pkt_set_data(pkt, &icmpv6_access); + net_pkt_write(pkt, data, data_size); net_pkt_cursor_init(pkt); net_ipv6_finalize(pkt, IPPROTO_ICMPV6); diff --git a/subsys/net/ip/icmpv6.h b/subsys/net/ip/icmpv6.h index 5083f439c0..779a09d6d8 100644 --- a/subsys/net/ip/icmpv6.h +++ b/subsys/net/ip/icmpv6.h @@ -178,13 +178,18 @@ int net_icmpv6_send_error(struct net_pkt *pkt, u8_t type, u8_t code, * to this Echo Request. May be zero. * @param sequence A sequence number to aid in matching Echo Replies * to this Echo Request. May be zero. + * @param data Arbitrary payload data that will be included in the + * Echo Reply verbatim. May be zero. + * @param data_size Size of the Payload Data in bytes. May be zero. * * @return Return 0 if the sending succeed, <0 otherwise. */ int net_icmpv6_send_echo_request(struct net_if *iface, struct in6_addr *dst, u16_t identifier, - u16_t sequence); + u16_t sequence, + const void *data, + size_t data_size); void net_icmpv6_register_handler(struct net_icmpv6_handler *handler); void net_icmpv6_unregister_handler(struct net_icmpv6_handler *handler); diff --git a/subsys/net/ip/net_shell.c b/subsys/net/ip/net_shell.c index 8df6d1d6b3..0e4bda413d 100644 --- a/subsys/net/ip/net_shell.c +++ b/subsys/net/ip/net_shell.c @@ -2703,7 +2703,9 @@ static int ping_ipv6(const struct shell *shell, char *host) ret = net_icmpv6_send_echo_request(iface, &ipv6_target, sys_rand32_get(), - sys_rand32_get()); + sys_rand32_get(), + NULL, + 0); if (ret) { remove_ipv6_ping_handler(); } else {