diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2015-01-14 16:10:38 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2015-01-14 16:10:38 -0600 |
commit | eac10c055d6b7a6f5ed825186f736ea8a840cbf6 (patch) | |
tree | e82cff258dd2a61632d2bdb04087ece64f22371b /nuttx | |
parent | a9ccb8c85c22dbbc63deda648ac0e539d854e81a (diff) | |
download | px4-nuttx-eac10c055d6b7a6f5ed825186f736ea8a840cbf6.tar.gz px4-nuttx-eac10c055d6b7a6f5ed825186f736ea8a840cbf6.tar.bz2 px4-nuttx-eac10c055d6b7a6f5ed825186f736ea8a840cbf6.zip |
Networking: (1) Copied all ICMP sources files to net/icmpv6 with proper renaming and removal of IPv4 logic, (2) remove IPv6 logic from files in net/icmp, (3) copied include/nuttx/icmp.h to icmpv6.h and removed IPv4 specific logic, (4) removed all IPv6 logic from icmp.h, (5) IP_HDRLEN became IPv4_HDRLEN and IPv6_HDRLEN, (6) ip_chksum() became ipv4_chksum() and ipv6_chksum(), and (7) added partial support for ICMPv6 statistics.
Diffstat (limited to 'nuttx')
25 files changed, 1326 insertions, 105 deletions
diff --git a/nuttx/drivers/net/slip.c b/nuttx/drivers/net/slip.c index 29cc94b26..665cebaf2 100644 --- a/nuttx/drivers/net/slip.c +++ b/nuttx/drivers/net/slip.c @@ -705,7 +705,7 @@ static int slip_rxtask(int argc, FAR char *argv[]) * enough to hold an IP header. */ - if (priv->rxlen >= IP_HDRLEN) + if (priv->rxlen >= IPv4_HDRLEN) { /* Handle the IP input. Get exclusive access to uIP. */ diff --git a/nuttx/include/nuttx/net/icmp.h b/nuttx/include/nuttx/net/icmp.h index 2e376b4f5..ec912f4b9 100644 --- a/nuttx/include/nuttx/net/icmp.h +++ b/nuttx/include/nuttx/net/icmp.h @@ -57,9 +57,9 @@ * Pre-processor Definitions ****************************************************************************/ -/* ICMP/ICMP6 definitions */ +/* ICMP definitions */ -/* ICMP/ICMP6 Message Types */ +/* ICMP Message Types */ #define ICMP_ECHO_REPLY 0 /* RFC 792 */ #define ICMP_DEST_UNREACHABLE 3 /* RFC 792 */ @@ -90,22 +90,10 @@ #define ICMP_PHOTURIS_SECURITY_FAIL 40 #define ICMP_EXP_MOBILE_PROTO 41 /* RFC 4065 */ -/* ICMP6 Message Types */ - -#define ICMP6_ECHO_REPLY 129 -#define ICMP6_ECHO_REQUEST 128 -#define ICMP6_NEIGHBOR_SOLICITATION 135 -#define ICMP6_NEIGHBOR_ADVERTISEMENT 136 - -#define ICMP6_FLAG_S (1 << 6) - -#define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1 -#define ICMP6_OPTION_TARGET_LINK_ADDRESS 2 - /* Header sizes */ -#define ICMP_HDRLEN 4 /* Size of ICMP header */ -#define IPICMP_HDRLEN (ICMP_HDRLEN + IP_HDRLEN) /* Size of IP + ICMP header */ +#define ICMP_HDRLEN 4 /* Size of ICMP header */ +#define IPICMP_HDRLEN (ICMP_HDRLEN + IPv4_HDRLEN) /* Size of IP + ICMP header */ /**************************************************************************** * Public Type Definitions @@ -115,21 +103,6 @@ struct icmp_iphdr_s { -#ifdef CONFIG_NET_IPv6 - - /* IPv6 Ip header */ - - uint8_t vtc; /* Bits 0-3: version, bits 4-7: traffic class (MS) */ - uint8_t tcf; /* Bits 0-3: traffic class (LS), bits 4-7: flow label (MS) */ - uint16_t flow; /* 16-bit flow label (LS) */ - uint8_t len[2]; /* 16-bit Payload length */ - uint8_t proto; /* 8-bit Next header (same as IPv4 protocol field) */ - uint8_t ttl; /* 8-bit Hop limit (like IPv4 TTL field) */ - net_ip6addr_t srcipaddr; /* 128-bit Source address */ - net_ip6addr_t destipaddr; /* 128-bit Destination address */ - -#else /* CONFIG_NET_IPv6 */ - /* IPv4 IP header */ uint8_t vhl; /* 8-bit Version (4) and header length (5 or 6) */ @@ -143,8 +116,6 @@ struct icmp_iphdr_s uint16_t srcipaddr[2]; /* 32-bit Source IP address */ uint16_t destipaddr[2]; /* 32-bit Destination IP address */ -#endif /* CONFIG_NET_IPv6 */ - /* ICMP header */ uint8_t type; /* Defines the format of the ICMP message */ @@ -155,25 +126,10 @@ struct icmp_iphdr_s * message type indicated by the Type and Code fields. */ -#ifndef CONFIG_NET_IPv6 - /* ICMP_ECHO_REQUEST and ICMP_ECHO_REPLY data */ uint16_t id; /* Used to match requests with replies */ uint16_t seqno; /* " " "" " " " " " " " " */ - -#else /* !CONFIG_NET_IPv6 */ - - /* ICMP6_ECHO_REQUEST and ICMP6_ECHO_REPLY data */ - - uint8_t flags; - uint8_t reserved1; - uint8_t reserved2; - uint8_t reserved3; - uint8_t icmp6data[16]; - uint8_t options[1]; - -#endif /* !CONFIG_NET_IPv6 */ }; /* The structure holding the ICMP statistics that are gathered if diff --git a/nuttx/include/nuttx/net/icmpv6.h b/nuttx/include/nuttx/net/icmpv6.h new file mode 100644 index 000000000..d0b1e9f66 --- /dev/null +++ b/nuttx/include/nuttx/net/icmpv6.h @@ -0,0 +1,210 @@ +/**************************************************************************** + * include/nuttx/net/icmpv6.h + * Header file for the uIP ICMPv6 stack. + * + * Copyright (C) 2007-2009, 2012, 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * This logic was leveraged from uIP which also has a BSD-style license: + * + * Author Adam Dunkels <adam@dunkels.com> + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_NET_ICMPv6_H +#define __INCLUDE_NUTTX_NET_ICMPv6_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> + +#include <nuttx/net/netconfig.h> +#include <nuttx/net/ip.h> +#include <nuttx/net/tcp.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* ICMP definitions */ + +/* ICMP Message Types */ + +#define ICMPv6_ECHO_REPLY 0 /* RFC 792 */ +#define ICMPv6_DEST_UNREACHABLE 3 /* RFC 792 */ +#define ICMPv6_SRC_QUENCH 4 /* RFC 792 */ +#define ICMPv6_REDIRECT 5 /* RFC 792 */ +#define ICMPv6_ALT_HOST_ADDRESS 6 +#define ICMPv6_ECHO_REQUEST 8 /* RFC 792 */ +#define ICMPv6_ROUTER_ADVERTISEMENT 9 /* RFC 1256 */ +#define ICMPv6_ROUTER_SOLICITATION 10 /* RFC 1256 */ +#define ICMPv6_TIME_EXCEEDED 11 /* RFC 792 */ +#define ICMPv6_PARAMETER_PROBLEM 12 +#define ICMPv6_TIMESTAMP_REQUEST 13 +#define ICMPv6_TIMESTAMP_REPLY 14 +#define ICMPv6_INFORMATION_REQUEST 15 +#define ICMPv6_INFORMATION_REPLY 16 +#define ICMPv6_ADDRESS_MASK_REQUEST 17 +#define ICMPv6_ADDRESS_MASK_REPLY 18 +#define ICMPv6_TRACEROUTE 30 +#define ICMPv6_CONVERSION_ERROR 31 +#define ICMPv6_MOBILE_HOST_REDIRECT 32 +#define ICMPv6_IPV6_WHEREAREYOU 33 +#define ICMPv6_IPV6_IAMHERE 34 +#define ICMPv6_MOBILE_REGIS_REQUEST 35 +#define ICMPv6_MOBILE_REGIS_REPLY 36 +#define ICMPv6_DOMAIN_NAME_REQUEST 37 +#define ICMPv6_DOMAIN_NAME_REPLY 38 +#define ICMPv6_SKIP_DISCOVERY_PROTO 39 +#define ICMPv6_PHOTURIS_SECURITY_FAIL 40 +#define ICMPv6_EXP_MOBILE_PROTO 41 /* RFC 4065 */ +#define ICMPv6_ECHO_REPLY 129 +#define ICMPv6_ECHO_REQUEST 128 +#define ICMPv6_NEIGHBOR_SOLICITATION 135 +#define ICMPv6_NEIGHBOR_ADVERTISEMENT 136 + +#define ICMPv6_FLAG_S (1 << 6) + +#define ICMPv6_OPTION_SOURCE_LINK_ADDRESS 1 +#define ICMPv6_OPTION_TARGET_LINK_ADDRESS 2 + +/* Header sizes */ + +#define ICMPv6_HDRLEN 4 /* Size of ICMPv6 header */ +#define IPICMPv6_HDRLEN (ICMPv6_HDRLEN + IPv6_HDRLEN) /* Size of IPv6 + ICMPv6 header */ + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +/* The ICMP and IP headers */ + +struct icmpv6_iphdr_s +{ + /* IPv6 Ip header */ + + uint8_t vtc; /* Bits 0-3: version, bits 4-7: traffic class (MS) */ + uint8_t tcf; /* Bits 0-3: traffic class (LS), bits 4-7: flow label (MS) */ + uint16_t flow; /* 16-bit flow label (LS) */ + uint8_t len[2]; /* 16-bit Payload length */ + uint8_t proto; /* 8-bit Next header (same as IPv4 protocol field) */ + uint8_t ttl; /* 8-bit Hop limit (like IPv4 TTL field) */ + net_ip6addr_t srcipaddr; /* 128-bit Source address */ + net_ip6addr_t destipaddr; /* 128-bit Destination address */ + + /* ICMPv6 header */ + + uint8_t type; /* Defines the format of the ICMP message */ + uint8_t icode; /* Further qualifies the ICMP messsage */ + uint16_t icmpv6chksum; /* Checksum of ICMP header and data */ + + /* Data following the ICMP header contains the data specific to the + * message type indicated by the Type and Code fields. + */ + + /* ICMPv6_ECHO_REQUEST and ICMPv6_ECHO_REPLY data */ + + uint8_t flags; + uint8_t reserved1; + uint8_t reserved2; + uint8_t reserved3; + uint8_t icmpv6data[16]; + uint8_t options[1]; +}; + +/* The structure holding the ICMP statistics that are gathered if + * CONFIG_NET_STATISTICS is defined. + */ + +#ifdef CONFIG_NET_STATISTICS +struct icmpv6_stats_s +{ + net_stats_t drop; /* Number of dropped ICMP packets */ + net_stats_t recv; /* Number of received ICMP packets */ + net_stats_t sent; /* Number of sent ICMP packets */ + net_stats_t typeerr; /* Number of ICMP packets with a wrong type */ +}; +#endif + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: imcp_ping + * + * Description: + * Send a ECHO request and wait for the ECHO response + * + * Parameters: + * addr - The IP address of the peer to send the ICMP ECHO request to + * in network order. + * id - The ID to use in the ICMP ECHO request. This number should be + * unique; only ECHO responses with this matching ID will be + * processed (host order) + * seqno - The sequence number used in the ICMP ECHO request. NOT used + * to match responses (host order) + * dsecs - Wait up to this many deci-seconds for the ECHO response to be + * returned (host order). + * + * Return: + * seqno of received ICMP ECHO with matching ID (may be different + * from the seqno argument (may be a delayed response from an earlier + * ping with the same ID). Or a negated errno on any failure. + * + * Assumptions: + * Called from the user level with interrupts enabled. + * + ****************************************************************************/ + +int icmpv6_ping(net_ipaddr_t addr, uint16_t id, uint16_t seqno, + uint16_t datalen, int dsecs); + +#undef EXTERN +#ifdef __cplusplus +} +#endif +#endif /* __INCLUDE_NUTTX_NET_ICMPv6_H */ diff --git a/nuttx/include/nuttx/net/igmp.h b/nuttx/include/nuttx/net/igmp.h index 4c09cdeef..a6a69c335 100644 --- a/nuttx/include/nuttx/net/igmp.h +++ b/nuttx/include/nuttx/net/igmp.h @@ -83,7 +83,7 @@ */ #define IGMP_HDRLEN 8 -#define IPIGMP_HDRLEN (IGMP_HDRLEN + IP_HDRLEN + 4) +#define IPIGMP_HDRLEN (IGMP_HDRLEN + IPv4_HDRLEN + 4) /* Group flags */ diff --git a/nuttx/include/nuttx/net/ip.h b/nuttx/include/nuttx/net/ip.h index e316b96ad..f3a16efb7 100644 --- a/nuttx/include/nuttx/net/ip.h +++ b/nuttx/include/nuttx/net/ip.h @@ -76,12 +76,14 @@ #define IP_FLAG_DONTFRAG 0x4000 #define IP_FLAG_MOREFRAGS 0x2000 -/* Header sizes */ +/* IP Header sizes */ + +#ifdef CONFIG_NET_IPv4 +# define IPv4_HDRLEN 20 /* Size of IPv4 header */ +#endif #ifdef CONFIG_NET_IPv6 -# define IP_HDRLEN 40 /* Size of IP header */ -#else -# define IP_HDRLEN 20 /* Size of IP header */ +# define IPv6_HDRLEN 40 /* Size of IPv6 header */ #endif /**************************************************************************** @@ -99,7 +101,7 @@ typedef net_ip6addr_t net_ipaddr_t; typedef net_ip4addr_t net_ipaddr_t; #endif -/* The IP header */ +/* The IPv4 header */ struct net_iphdr_s { @@ -218,19 +220,25 @@ struct net_iphdr_s * src The source from where to copy. */ -#ifndef CONFIG_NET_IPv6 -# define net_ipaddr_copy(dest, src) \ +#define net_ipv4addr_copy(dest, src) \ do { \ (dest) = (in_addr_t)(src); \ } while (0) -# define net_ipaddr_hdrcopy(dest, src) \ +#define net_ipv4addr_hdrcopy(dest, src) \ do { \ ((uint16_t*)(dest))[0] = ((uint16_t*)(src))[0]; \ ((uint16_t*)(dest))[1] = ((uint16_t*)(src))[1]; \ } while (0) + +#define net_ipv6addr_copy(dest, src) memcpy(&dest, &src, sizeof(net_ip6addr_t)) +#define net_ipv6addr_hdrcopy(dest, src) net_ipv6addr_copy(dest, src) + +#ifndef CONFIG_NET_IPv6 +# define net_ipaddr_copy(dest, src) net_ipv4addr_copy(dest, src) +# define net_ipaddr_hdrcopy(dest, src) net_ipv4addr_hdrcopy(dest, src) #else /* !CONFIG_NET_IPv6 */ -# define net_ipaddr_copy(dest, src) memcpy(&dest, &src, sizeof(net_ip6addr_t)) -# define net_ipaddr_hdrcopy(dest, src) net_ipaddr_copy(dest, src) +# define net_ipaddr_copy(dest, src) net_ipv6addr_copy(dest, src) +# define net_ipaddr_hdrcopy(dest, src) net_ipv6addr_hdrcopy(dest, src) #endif /* !CONFIG_NET_IPv6 */ /* Compare two IP addresses diff --git a/nuttx/include/nuttx/net/netdev.h b/nuttx/include/nuttx/net/netdev.h index 42fcb2848..56e2686e4 100644 --- a/nuttx/include/nuttx/net/netdev.h +++ b/nuttx/include/nuttx/net/netdev.h @@ -401,22 +401,44 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len); void net_incr32(FAR uint8_t *op32, uint16_t op16); /**************************************************************************** - * Name: ip_chksum + * Name: ipv4_chksum * * Description: - * Calculate the IP header checksum of the packet header in d_buf. + * Calculate the IPv4 header checksum of the packet header in d_buf. * - * The IP header checksum is the Internet checksum of the 20 bytes of - * the IP header. + * The IPv4 header checksum is the Internet checksum of the 20 bytes of + * the IPv4 header. * * If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be * provided by architecture-specific logic. * * Returned Value: - * The IP header checksum of the IP header in the d_buf buffer. + * The IPv4 header checksum of the IPv4 header in the d_buf buffer. * ****************************************************************************/ -uint16_t ip_chksum(FAR struct net_driver_s *dev); +#ifdef CONFIG_NET_IPv4 +uint16_t ipv4_chksum(FAR struct net_driver_s *dev); +#endif + +/**************************************************************************** + * Name: ipv6_chksum + * + * Description: + * Calculate the IPv6 header checksum of the packet header in d_buf. + * + * The IPv6 header checksum is the Internet checksum of the 40 bytes of + * the IPv6 header. + * + * If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be + * provided by architecture-specific logic. + * + * Returned Value: + * The IPv6 header checksum of the IPv6 header in the d_buf buffer. + * + ****************************************************************************/ +#ifdef CONFIG_NET_IPv6 +uint16_t ipv6_chksum(FAR struct net_driver_s *dev); +#endif #endif /* __INCLUDE_NUTTX_NET_NETDEV_H */ diff --git a/nuttx/include/nuttx/net/netstats.h b/nuttx/include/nuttx/net/netstats.h index a624360dc..76f428756 100644 --- a/nuttx/include/nuttx/net/netstats.h +++ b/nuttx/include/nuttx/net/netstats.h @@ -59,6 +59,9 @@ #ifdef CONFIG_NET_ICMP # include <nuttx/net/icmp.h> #endif +#ifdef CONFIG_NET_ICMPv6 +# include <nuttx/net/icmpv6.h> +#endif #ifdef CONFIG_NET_IGMP # include <nuttx/net/igmp.h> #endif @@ -97,22 +100,26 @@ struct ip_stats_s struct net_stats_s { - struct ip_stats_s ip; /* IP statistics */ + struct ip_stats_s ip; /* IP statistics */ #ifdef CONFIG_NET_ICMP - struct icmp_stats_s icmp; /* ICMP statistics */ + struct icmp_stats_s icmp; /* ICMP statistics */ +#endif + +#ifdef CONFIG_NET_ICMPv6 + struct icmpv6_stats_s icmpv6; /* ICMPv6 statistics */ #endif #ifdef CONFIG_NET_IGMP - struct igmp_stats_s igmp; /* IGMP statistics */ + struct igmp_stats_s igmp; /* IGMP statistics */ #endif #ifdef CONFIG_NET_TCP - struct tcp_stats_s tcp; /* TCP statistics */ + struct tcp_stats_s tcp; /* TCP statistics */ #endif #ifdef CONFIG_NET_UDP - struct udp_stats_s udp; /* UDP statistics */ + struct udp_stats_s udp; /* UDP statistics */ #endif }; #endif /* CONFIG_NET_STATISTICS */ diff --git a/nuttx/include/nuttx/net/tcp.h b/nuttx/include/nuttx/net/tcp.h index 53692d389..0384f3bc5 100644 --- a/nuttx/include/nuttx/net/tcp.h +++ b/nuttx/include/nuttx/net/tcp.h @@ -97,8 +97,8 @@ /* TCP header sizes */ -#define TCP_HDRLEN 20 /* Size of TCP header */ -#define IPTCP_HDRLEN (TCP_HDRLEN + IP_HDRLEN) /* Size of IP + TCP header */ +#define TCP_HDRLEN 20 /* Size of TCP header */ +#define IPTCP_HDRLEN (TCP_HDRLEN + IPv4_HDRLEN) /* Size of IPv4 + TCP header */ /* Initial minimum MSS according to RFC 879 * diff --git a/nuttx/include/nuttx/net/udp.h b/nuttx/include/nuttx/net/udp.h index 3ed188cd9..c32120c74 100644 --- a/nuttx/include/nuttx/net/udp.h +++ b/nuttx/include/nuttx/net/udp.h @@ -63,7 +63,7 @@ /* Header sizes */ #define UDP_HDRLEN 8 /* Size of UDP header */ -#define IPUDP_HDRLEN (UDP_HDRLEN + IP_HDRLEN) /* Size of IP + UDP header */ +#define IPUDP_HDRLEN (UDP_HDRLEN + IPv4_HDRLEN) /* Size of IPv4 + UDP header */ /**************************************************************************** * Public Type Definitions diff --git a/nuttx/net/devif/devif_input.c b/nuttx/net/devif/devif_input.c index afdbd52af..b9673057b 100644 --- a/nuttx/net/devif/devif_input.c +++ b/nuttx/net/devif/devif_input.c @@ -167,7 +167,7 @@ static uint8_t devif_reassembly(void) if (!g_reassembly_timer) { - memcpy(g_reassembly_buffer, &pbuf->vhl, IP_HDRLEN); + memcpy(g_reassembly_buffer, &pbuf->vhl, IPv4_HDRLEN); g_reassembly_timer = CONFIG_NET_TCP_REASS_MAXAGE; g_reassembly_flags = 0; @@ -200,7 +200,7 @@ static uint8_t devif_reassembly(void) /* Copy the fragment into the reassembly buffer, at the right offset. */ - memcpy(&g_reassembly_buffer[IP_HDRLEN + offset], (char *)pbuf + (int)((pbuf->vhl & 0x0f) * 4), len); + memcpy(&g_reassembly_buffer[IPv4_HDRLEN + offset], (char *)pbuf + (int)((pbuf->vhl & 0x0f) * 4), len); /* Update the bitmap. */ @@ -282,7 +282,7 @@ static uint8_t devif_reassembly(void) pbuf->len[0] = g_reassembly_len >> 8; pbuf->len[1] = g_reassembly_len & 0xff; pbuf->ipchksum = 0; - pbuf->ipchksum = ~(ip_chksum(dev)); + pbuf->ipchksum = ~(ipv4_chksum(dev)); return g_reassembly_len; } @@ -372,7 +372,7 @@ int devif_input(FAR struct net_driver_s *dev) * the size of the IPv6 header (40 bytes). */ - iplen = (pbuf->len[0] << 8) + pbuf->len[1] + IP_HDRLEN; + iplen = (pbuf->len[0] << 8) + pbuf->len[1] + IPv6_HDRLEN; #else iplen = (pbuf->len[0] << 8) + pbuf->len[1]; #endif /* CONFIG_NET_IPv6 */ @@ -496,7 +496,7 @@ int devif_input(FAR struct net_driver_s *dev) } #ifndef CONFIG_NET_IPv6 - if (ip_chksum(dev) != 0xffff) + if (ipv4_chksum(dev) != 0xffff) { /* Compute and check the IP header checksum. */ diff --git a/nuttx/net/icmp/Make.defs b/nuttx/net/icmp/Make.defs index 4bbe3f391..d5a67c206 100644 --- a/nuttx/net/icmp/Make.defs +++ b/nuttx/net/icmp/Make.defs @@ -33,10 +33,10 @@ # ############################################################################ -# ICMP source files - ifeq ($(CONFIG_NET_ICMP),y) +# ICMP source files + NET_CSRCS += icmp_input.c ifeq ($(CONFIG_NET_ICMP_PING),y) diff --git a/nuttx/net/icmp/icmp_input.c b/nuttx/net/icmp/icmp_input.c index ea3d543c1..4539faf13 100644 --- a/nuttx/net/icmp/icmp_input.c +++ b/nuttx/net/icmp/icmp_input.c @@ -1,6 +1,6 @@ /**************************************************************************** * net/icmp/icmp_input.c - * Handling incoming ICMP/ICMP6 input + * Handling incoming ICMP input * * Copyright (C) 2007-2009, 2012, 2014-2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> @@ -92,10 +92,10 @@ FAR struct devif_callback_s *g_echocallback = NULL; * Name: icmp_input * * Description: - * Handle incoming ICMP/ICMP6 input + * Handle incoming ICMP input * * Parameters: - * dev - The device driver structure containing the received ICMP/ICMP6 + * dev - The device driver structure containing the received ICMP * packet * * Return: @@ -148,7 +148,7 @@ void icmp_input(FAR struct net_driver_s *dev) /* The slow way... sum over the ICMP message */ picmp->icmpchksum = 0; - picmp->icmpchksum = ~icmp_chksum(dev, (((uint16_t)picmp->len[0] << 8) | (uint16_t)picmp->len[1]) - IP_HDRLEN); + picmp->icmpchksum = ~icmp_chksum(dev, (((uint16_t)picmp->len[0] << 8) | (uint16_t)picmp->len[1]) - IPv4_HDRLEN); if (picmp->icmpchksum == 0) { picmp->icmpchksum = 0xffff; diff --git a/nuttx/net/icmp/icmp_send.c b/nuttx/net/icmp/icmp_send.c index e7326e242..2ff347ee7 100644 --- a/nuttx/net/icmp/icmp_send.c +++ b/nuttx/net/icmp/icmp_send.c @@ -130,7 +130,7 @@ void icmp_send(FAR struct net_driver_s *dev, FAR net_ipaddr_t *destaddr) /* Calculate IP checksum. */ picmp->ipchksum = 0; - picmp->ipchksum = ~(ip_chksum(dev)); + picmp->ipchksum = ~(ipv4_chksum(dev)); /* Calculate the ICMP checksum. */ diff --git a/nuttx/net/icmpv6/Make.defs b/nuttx/net/icmpv6/Make.defs index ce83b0713..93c49558e 100644 --- a/nuttx/net/icmpv6/Make.defs +++ b/nuttx/net/icmpv6/Make.defs @@ -35,7 +35,13 @@ ifeq ($(CONFIG_NET_ICMPv6),y) -# Support for ICMPv6 +# ICMPv6 source files + +NET_CSRCS += icmpv6_input.c + +ifeq ($(CONFIG_NET_ICMPv6_PING),y) +NET_CSRCS += icmpv6_ping.c icmpv6_poll.c icmpv6_send.c +endif # Include ICMPv6 build support diff --git a/nuttx/net/icmpv6/icmpv6.h b/nuttx/net/icmpv6/icmpv6.h new file mode 100644 index 000000000..0e872334b --- /dev/null +++ b/nuttx/net/icmpv6/icmpv6.h @@ -0,0 +1,97 @@ +/**************************************************************************** + * net/icmpv6/icmpv6.h + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __NET_ICMPv6_ICMPv6_H +#define __NET_ICMPv6_ICMPv6_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/types.h> + +#include <nuttx/net/ip.h> + +#ifdef CONFIG_NET_ICMPv6 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +# define EXTERN extern "C" +extern "C" +{ +#else +# define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/* Defined in icmpv6_input.c ************************************************/ + +void icmpv6_input(FAR struct net_driver_s *dev); + +/* Defined in icmpv6_poll.c *************************************************/ + +#ifdef CONFIG_NET_ICMPv6_PING +void icmpv6_poll(FAR struct net_driver_s *dev); +#endif /* CONFIG_NET_ICMPv6_PING */ + +/* Defined in icmpv6_send.c *************************************************/ + +#ifdef CONFIG_NET_ICMPv6_PING +void icmpv6_send(FAR struct net_driver_s *dev, FAR net_ipaddr_t *destaddr); +#endif /* CONFIG_NET_ICMPv6_PING */ + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_NET_ICMPv6 */ +#endif /* __NET_ICMPv6_ICMPv6_H */ diff --git a/nuttx/net/icmpv6/icmpv6_input.c b/nuttx/net/icmpv6/icmpv6_input.c new file mode 100644 index 000000000..f93dee7f3 --- /dev/null +++ b/nuttx/net/icmpv6/icmpv6_input.c @@ -0,0 +1,229 @@ +/**************************************************************************** + * net/icmpv6/icmpv6_input.c + * Handling incoming ICMPv6 input + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels <adam@dunkels.com> + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#ifdef CONFIG_NET + +#include <stdint.h> +#include <string.h> +#include <debug.h> + +#include <net/if.h> +#include <arpa/inet.h> + +#include <nuttx/net/netconfig.h> +#include <nuttx/net/netdev.h> +#include <nuttx/net/netstats.h> +#include <nuttx/net/ip.h> + +#include "devif/devif.h" +#include "utils/utils.h" +#include "ipv6/ipv6.h" +#include "icmpv6/icmpv6.h" + +#ifdef CONFIG_NET_ICMPv6 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ICMPv6BUF ((struct icmpv6_iphdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)]) + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +#ifdef CONFIG_NET_ICMPv6v6_PING +FAR struct devif_callback_s *g_echocallback = NULL; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: icmpv6_input + * + * Description: + * Handle incoming ICMPv6 input + * + * Parameters: + * dev - The device driver structure containing the received ICMPv6 + * packet + * + * Return: + * None + * + * Assumptions: + * Called from the interrupt level or with interrupts disabled. + * + ****************************************************************************/ + +void icmpv6_input(FAR struct net_driver_s *dev) +{ + FAR struct icmpv6_iphdr_s *picmp = ICMPv6BUF; + +#ifdef CONFIG_NET_STATISTICS + g_netstats.icmpv6.recv++; +#endif + + /* If we get a neighbor solicitation for our address we should send + * a neighbor advertisement message back. + */ + + if (picmp->type == ICMPv6_NEIGHBOR_SOLICITATION) + { + if (net_ipaddr_cmp(picmp->icmpv6data, dev->d_ipaddr)) + { + if (picmp->options[0] == ICMPv6_OPTION_SOURCE_LINK_ADDRESS) + { + /* Save the sender's address in our neighbor list. */ + + net_neighbor_add(picmp->srcipaddr, &(picmp->options[2])); + } + + /* We should now send a neighbor advertisement back to where the + * neighbor solicitation came from. + */ + + picmp->type = ICMPv6_NEIGHBOR_ADVERTISEMENT; + picmp->flags = ICMPv6_FLAG_S; /* Solicited flag. */ + + picmp->reserved1 = picmp->reserved2 = picmp->reserved3 = 0; + + net_ipv6addr_copy(picmp->destipaddr, picmp->srcipaddr); + net_ipv6addr_copy(picmp->srcipaddr, dev->d_ipaddr); + + picmp->options[0] = ICMPv6_OPTION_TARGET_LINK_ADDRESS; + picmp->options[1] = 1; /* Options length, 1 = 8 bytes. */ + memcpy(&(picmp->options[2]), &dev->d_mac, IFHWADDRLEN); + + picmp->icmpv6chksum = 0; + picmp->icmpv6chksum = ~icmpv6_chksum(dev); + } + else + { + goto drop; + } + } + else if (picmp->type == ICMPv6_ECHO_REQUEST) + { + /* ICMPv6 echo (i.e., ping) processing. This is simple, we only + * change the ICMPv6 type from ECHO to ECHO_REPLY and update the + * ICMPv6 checksum before we return the packet. + */ + + picmp->type = ICMPv6_ECHO_REPLY; + + net_ipv6addr_copy(picmp->destipaddr, picmp->srcipaddr); + net_ipv6addr_copy(picmp->srcipaddr, dev->d_ipaddr); + + picmp->icmpv6chksum = 0; + picmp->icmpv6chksum = ~icmpv6_chksum(dev); + } + + /* If an ICMPv6 echo reply is received then there should also be + * a thread waiting to received the echo response. + */ + +#ifdef CONFIG_NET_ICMPv6v6_PING + else if (picmp->type == ICMPv6_ECHO_REPLY && g_echocallback) + { + uint16_t flags = ICMPv6_ECHOREPLY; + + if (g_echocallback) + { + /* Dispatch the ECHO reply to the waiting thread */ + + flags = devif_callback_execute(dev, picmp, flags, g_echocallback); + } + + /* If the ECHO reply was not handled, then drop the packet */ + + if (flags == ICMPv6_ECHOREPLY) + { + /* The ECHO reply was not handled */ + + goto drop; + } + } +#endif + + else + { + nlldbg("Unknown ICMPv6 cmd: %d\n", picmp->type); + goto typeerr; + } + + nllvdbg("Outgoing ICMPv6 packet length: %d (%d)\n", + dev->d_len, (picmp->len[0] << 8) | picmp->len[1]); + +#ifdef CONFIG_NET_STATISTICS + g_netstats.icmpv6.sent++; + g_netstats.ip.sent++; +#endif + return; + +typeerr: +#ifdef CONFIG_NET_STATISTICS + g_netstats.icmpv6.typeerr++; +#endif + +drop: +#ifdef CONFIG_NET_STATISTICS + g_netstats.icmpv6.drop++; +#endif + dev->d_len = 0; +} + +#endif /* CONFIG_NET_ICMPv6 */ +#endif /* CONFIG_NET */ diff --git a/nuttx/net/icmpv6/icmpv6_ping.c b/nuttx/net/icmpv6/icmpv6_ping.c new file mode 100644 index 000000000..46b71d55f --- /dev/null +++ b/nuttx/net/icmpv6/icmpv6_ping.c @@ -0,0 +1,407 @@ +/**************************************************************************** + * net/icmpv6/icmpv6_ping.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#if defined(CONFIG_NET) && defined(CONFIG_NET_ICMPv6) && \ + defined(CONFIG_NET_ICMPv6v6_PING) + +#include <sys/types.h> +#include <stdint.h> +#include <stdbool.h> +#include <semaphore.h> +#include <debug.h> + +#include <net/if.h> + +#include <nuttx/clock.h> +#include <nuttx/net/netconfig.h> +#include <nuttx/net/net.h> +#include <nuttx/net/netdev.h> +#include <nuttx/net/ip.h> +#include <nuttx/net/icmpv6.h> + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "arp/arp.h" +#include "icmpv6/icmpv6.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ICMPv6BUF ((struct icmpv6_iphdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)]) +#define ICMPv6DAT (&dev->d_buf[NET_LL_HDRLEN(dev) + sizeof(struct icmpv6_iphdr_s)]) + +/* Allocate a new ICMPv6 data callback */ + +#define icmpv6_callback_alloc() devif_callback_alloc(&g_echocallback) +#define icmpv6_callback_free(cb) devif_callback_free(cb, &g_echocallback) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct icmpv6_ping_s +{ + FAR struct devif_callback_s *png_cb; /* Reference to callback instance */ + + sem_t png_sem; /* Use to manage the wait for the response */ + uint32_t png_time; /* Start time for determining timeouts */ + uint32_t png_ticks; /* System clock ticks to wait */ + int png_result; /* 0: success; <0:negated errno on fail */ + net_ipaddr_t png_addr; /* The peer to be ping'ed */ + uint16_t png_id; /* Used to match requests with replies */ + uint16_t png_seqno; /* IN: seqno to send; OUT: seqno recieved */ + uint16_t png_datlen; /* The length of data to send in the ECHO request */ + bool png_sent; /* true... the PING request has been sent */ +}; + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: ping_timeout + * + * Description: + * Check for send timeout. + * + * Parameters: + * pstate - Ping state structure + * + * Returned Value: + * TRUE:timeout FALSE:no timeout + * + * Assumptions: + * Running at the interrupt level + * + ****************************************************************************/ + +static inline int ping_timeout(FAR struct icmpv6_ping_s *pstate) +{ + uint32_t elapsed = clock_systimer() - pstate->png_time; + if (elapsed >= pstate->png_ticks) + { + return TRUE; + } + + return FALSE; +} + +/**************************************************************************** + * Function: ping_interrupt + * + * Description: + * This function is called from the interrupt level to perform the actual + * ECHO request and/or ECHO reply actions when polled by the lower, device + * interfacing layer. + * + * Parameters: + * dev The structure of the network driver that caused the interrupt + * conn The received packet, cast to void * + * pvpriv An instance of struct icmpv6_ping_s cast to void* + * flags Set of events describing why the callback was invoked + * + * Returned Value: + * Modified value of the input flags + * + * Assumptions: + * Running at the interrupt level + * + ****************************************************************************/ + +static uint16_t ping_interrupt(FAR struct net_driver_s *dev, FAR void *conn, + FAR void *pvpriv, uint16_t flags) +{ + FAR struct icmpv6_ping_s *pstate = (struct icmpv6_ping_s *)pvpriv; + FAR uint8_t *ptr; + int i; + + nllvdbg("flags: %04x\n", flags); + if (pstate) + { + /* Check if this is a ICMPv6 ECHO reply. If so, return the sequence + * number to the caller. NOTE: We may not even have sent the + * requested ECHO request; this could have been the delayed ECHO + * response from a previous ping. + */ + + if ((flags & ICMPv6_ECHOREPLY) != 0 && conn != NULL) + { + FAR struct icmpv6_iphdr_s *icmpv6 = (FAR struct icmpv6_iphdr_s *)conn; + + nlldbg("ECHO reply: id=%d seqno=%d\n", + ntohs(icmpv6->id), ntohs(icmpv6->seqno)); + + if (ntohs(icmpv6->id) == pstate->png_id) + { + /* Consume the ECHOREPLY */ + + flags &= ~ICMPv6_ECHOREPLY; + dev->d_len = 0; + + /* Return the result to the caller */ + + pstate->png_result = OK; + pstate->png_seqno = ntohs(icmpv6->seqno); + goto end_wait; + } + } + + /* Check: + * If the outgoing packet is available (it may have been claimed + * by a sendto interrupt serving a different thread) + * -OR- + * If the output buffer currently contains unprocessed incoming + * data. + * -OR- + * If we have already sent the ECHO request + * + * In the first two cases, we will just have to wait for the next + * polling cycle. + */ + + if (dev->d_sndlen <= 0 && /* Packet available */ + (flags & ICMPv6_NEWDATA) == 0 && /* No incoming data */ + !pstate->png_sent) /* Request not sent */ + { + FAR struct icmpv6_iphdr_s *picmpv6 = ICMPv6BUF; + + /* We can send the ECHO request now. + * + * Format the ICMPv6 ECHO request packet + */ + + picmpv6->type = ICMPv6_ECHO_REQUEST; + picmpv6->icode = 0; +# error "IPv6 ECHO Request not implemented" + + /* Add some easily verifiable data */ + + for (i = 0, ptr = ICMPv6DAT; i < pstate->png_datlen; i++) + { + *ptr++ = i; + } + + /* Send the ICMPv6 echo request. Note that d_sndlen is set to + * the size of the ICMPv6 payload and does not include the size + * of the ICMPv6 header. + */ + + nlldbg("Send ECHO request: seqno=%d\n", pstate->png_seqno); + + dev->d_sndlen = pstate->png_datlen + 4; + icmpv6_send(dev, &pstate->png_addr); + pstate->png_sent = true; + return flags; + } + + /* Check if the selected timeout has elapsed */ + + if (ping_timeout(pstate)) + { + int failcode; + + /* Check if this device is on the same network as the destination + * device. + */ + + if (!net_ipaddr_maskcmp(pstate->png_addr, dev->d_ipaddr, dev->d_netmask)) + { + /* Destination address was not on the local network served by this + * device. If a timeout occurs, then the most likely reason is + * that the destination address is not reachable. + */ + + nlldbg("Not reachable\n"); + failcode = -ENETUNREACH; + } + else + { + nlldbg("Ping timeout\n"); + failcode = -ETIMEDOUT; + } + + /* Report the failure */ + + pstate->png_result = failcode; + goto end_wait; + } + + /* Continue waiting */ + } + + return flags; + +end_wait: + nllvdbg("Resuming\n"); + + /* Do not allow any further callbacks */ + + pstate->png_cb->flags = 0; + pstate->png_cb->priv = NULL; + pstate->png_cb->event = NULL; + + /* Wake up the waiting thread */ + + sem_post(&pstate->png_sem); + return flags; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: imcp_ping + * + * Description: + * Send a ECHO request and wait for the ECHO response + * + * Parameters: + * addr - The IP address of the peer to send the ICMPv6 ECHO request to + * in network order. + * id - The ID to use in the ICMPv6 ECHO request. This number should be + * unique; only ECHO responses with this matching ID will be + * processed (host order) + * seqno - The sequence number used in the ICMPv6 ECHO request. NOT used + * to match responses (host order) + * dsecs - Wait up to this many deci-seconds for the ECHO response to be + * returned (host order). + * + * Return: + * seqno of received ICMPv6 ECHO with matching ID (may be different + * from the seqno argument (may be a delayed response from an earlier + * ping with the same ID). Or a negated errno on any failure. + * + * Assumptions: + * Called from the user level with interrupts enabled. + * + ****************************************************************************/ + +int icmpv6_ping(net_ipaddr_t addr, uint16_t id, uint16_t seqno, + uint16_t datalen, int dsecs) +{ + struct icmpv6_ping_s state; + net_lock_t save; +#ifdef CONFIG_NET_ARP_SEND + int ret; + + /* Make sure that the IP address mapping is in the ARP table */ + + ret = arp_send(addr); + if (ret < 0) + { + ndbg("ERROR: Not reachable\n"); + return -ENETUNREACH; + } +#endif + + /* Initialize the state structure */ + + sem_init(&state.png_sem, 0, 0); + state.png_ticks = DSEC2TICK(dsecs); /* System ticks to wait */ + state.png_result = -ENOMEM; /* Assume allocation failure */ + state.png_addr = addr; /* Address of the peer to be ping'ed */ + state.png_id = id; /* The ID to use in the ECHO request */ + state.png_seqno = seqno; /* The seqno to use int the ECHO request */ + state.png_datlen = datalen; /* The length of data to send in the ECHO request */ + state.png_sent = false; /* ECHO request not yet sent */ + + save = net_lock(); + state.png_time = clock_systimer(); + + /* Set up the callback */ + + state.png_cb = icmpv6_callback_alloc(); + if (state.png_cb) + { + state.png_cb->flags = (ICMPv6_POLL | ICMPv6_ECHOREPLY); + state.png_cb->priv = (void*)&state; + state.png_cb->event = ping_interrupt; + state.png_result = -EINTR; /* Assume sem-wait interrupted by signal */ + + /* Notify the device driver of the availability of TX data */ + +#ifdef CONFIG_NET_MULTILINK + netdev_txnotify(g_allzeroaddr, state.png_addr); +#else + netdev_txnotify(state.png_addr); +#endif + + /* Wait for either the full round trip transfer to complete or + * for timeout to occur. (1) net_lockedwait will also terminate if a + * signal is received, (2) interrupts may be disabled! They will + * be re-enabled while the task sleeps and automatically + * re-enabled when the task restarts. + */ + + nlldbg("Start time: 0x%08x seqno: %d\n", state.png_time, seqno); + net_lockedwait(&state.png_sem); + + icmpv6_callback_free(state.png_cb); + } + + net_unlock(save); + + /* Return the negated error number in the event of a failure, or the + * sequence number of the ECHO reply on success. + */ + + if (!state.png_result) + { + nlldbg("Return seqno=%d\n", state.png_seqno); + return (int)state.png_seqno; + } + else + { + nlldbg("Return error=%d\n", -state.png_result); + return state.png_result; + } +} + +#endif /* CONFIG_NET_ICMPv6 && CONFIG_NET_ICMPv6v6_PING ... */ diff --git a/nuttx/net/icmpv6/icmpv6_poll.c b/nuttx/net/icmpv6/icmpv6_poll.c new file mode 100644 index 000000000..8c06b53d2 --- /dev/null +++ b/nuttx/net/icmpv6/icmpv6_poll.c @@ -0,0 +1,104 @@ +/**************************************************************************** + * net/icmpv6/icmpv6_poll.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#if defined(CONFIG_NET) && defined(CONFIG_NET_ICMPv6) && defined(CONFIG_NET_ICMPv6_PING) + +#include <debug.h> + +#include <nuttx/net/netconfig.h> +#include <nuttx/net/netdev.h> +#include <nuttx/net/icmpv6.h> + +#include "devif/devif.h" +#include "icmpv6/icmpv6.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: icmpv6_poll + * + * Description: + * Poll a UDP "connection" structure for availability of TX data + * + * Parameters: + * dev - The device driver structure to use in the send operation + * + * Return: + * None + * + * Assumptions: + * Called from the interrupt level or with interrupts disabled. + * + ****************************************************************************/ + +void icmpv6_poll(FAR struct net_driver_s *dev) +{ + /* Setup for the application callback */ + + dev->d_appdata = &dev->d_buf[NET_LL_HDRLEN(dev) + IPICMPv6_HDRLEN]; + dev->d_snddata = &dev->d_buf[NET_LL_HDRLEN(dev) + IPICMPv6_HDRLEN]; + + dev->d_len = 0; + dev->d_sndlen = 0; + + /* Perform the application callback */ + + (void)devif_callback_execute(dev, NULL, ICMPv6_POLL, g_echocallback); +} + +#endif /* CONFIG_NET && CONFIG_NET_ICMPv6 && CONFIG_NET_ICMPv6_PING */ diff --git a/nuttx/net/icmpv6/icmpv6_send.c b/nuttx/net/icmpv6/icmpv6_send.c new file mode 100644 index 000000000..3c071ed54 --- /dev/null +++ b/nuttx/net/icmpv6/icmpv6_send.c @@ -0,0 +1,147 @@ +/**************************************************************************** + * net/icmpv6/icmpv6_send.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#if defined(CONFIG_NET) && defined(CONFIG_NET_ICMPv6) && defined(CONFIG_NET_ICMPv6_PING) + +#include <debug.h> + +#include <arpa/inet.h> + +#include <nuttx/net/netconfig.h> +#include <nuttx/net/netdev.h> +#include <nuttx/net/netstats.h> +#include <nuttx/net/ip.h> + +#include "devif/devif.h" +#include "utils/utils.h" +#include "icmpv6/icmpv6.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ICMPv6BUF ((struct icmpv6_iphdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)]) + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: icmpv6_send + * + * Description: + * Setup to send an ICMPv6 packet + * + * Parameters: + * dev - The device driver structure to use in the send operation + * + * Return: + * None + * + * Assumptions: + * Called from the interrupt level or with interrupts disabled. + * + ****************************************************************************/ + +void icmpv6_send(FAR struct net_driver_s *dev, FAR net_ipaddr_t *destaddr) +{ + FAR struct icmpv6_iphdr_s *picmpv6 = ICMPv6BUF; + + if (dev->d_sndlen > 0) + { + /* The total length to send is the size of the application data plus + * the IP and ICMPv6 headers (and, eventually, the Ethernet header) + */ + + dev->d_len = dev->d_sndlen + IPICMPv6_HDRLEN; + + /* The total size of the data (for ICMPv6 checksum calculation) includes + * the size of the ICMPv6 header + */ + + dev->d_sndlen += ICMPv6_HDRLEN; + + /* Initialize the IP header. Note that for IPv6, the IP length field + * does not include the IPv6 IP header length. + */ + + picmpv6->vtc = 0x60; + picmpv6->tcf = 0x00; + picmpv6->flow = 0x00; + picmpv6->len[0] = (dev->d_sndlen >> 8); + picmpv6->len[1] = (dev->d_sndlen & 0xff); + picmpv6->nexthdr = IP_PROTO_ICMPv6; + picmpv6->hoplimit = IP_TTL; + + net_ipaddr_copy(picmpv6->srcipaddr, &dev->d_ipaddr); + net_ipaddr_copy(picmpv6->destipaddr, destaddr); + + /* Calculate the ICMPv6 checksum. */ + + picmpv6->icmpv6chksum = 0; + picmpv6->icmpv6chksum = ~(icmpv6_chksum(dev, dev->d_sndlen)); + if (picmpv6->icmpv6chksum == 0) + { + picmpv6->icmpv6chksum = 0xffff; + } + + nllvdbg("Outgoing ICMPv6 packet length: %d (%d)\n", + dev->d_len, (picmpv6->len[0] << 8) | picmpv6->len[1]); + +#ifdef CONFIG_NET_STATISTICS + g_netstats.icmpv6.sent++; + g_netstats.ip.sent++; +#endif + } +} + +#endif /* CONFIG_NET && CONFIG_NET_ICMPv6 && CONFIG_NET_ICMPv6_PING */ diff --git a/nuttx/net/igmp/igmp_send.c b/nuttx/net/igmp/igmp_send.c index 189f9cfc3..87c70b53c 100644 --- a/nuttx/net/igmp/igmp_send.c +++ b/nuttx/net/igmp/igmp_send.c @@ -161,7 +161,7 @@ void igmp_send(FAR struct net_driver_s *dev, FAR struct igmp_group_s *group, /* Calculate IP checksum. */ IGMPBUF->ipchksum = 0; - IGMPBUF->ipchksum = ~igmp_chksum((FAR uint8_t *)IGMPBUF, IP_HDRLEN + RASIZE); + IGMPBUF->ipchksum = ~igmp_chksum((FAR uint8_t *)IGMPBUF, IPv4_HDRLEN + RASIZE); /* Set up the IGMP message */ diff --git a/nuttx/net/tcp/tcp_input.c b/nuttx/net/tcp/tcp_input.c index 0106c390f..c447045ec 100644 --- a/nuttx/net/tcp/tcp_input.c +++ b/nuttx/net/tcp/tcp_input.c @@ -324,7 +324,7 @@ found: * len) and the length of the IP header (20 bytes). */ - dev->d_len -= (len + IP_HDRLEN); + dev->d_len -= (len + IPv4_HDRLEN); /* First, check if the sequence number of the incoming packet is * what we're expecting next. If not, we send out an ACK with the diff --git a/nuttx/net/tcp/tcp_send.c b/nuttx/net/tcp/tcp_send.c index 2e8d23115..7546b2e2b 100644 --- a/nuttx/net/tcp/tcp_send.c +++ b/nuttx/net/tcp/tcp_send.c @@ -106,8 +106,8 @@ static void tcp_sendcomplete(FAR struct net_driver_s *dev) * length. */ - pbuf->len[0] = ((dev->d_len - IP_HDRLEN) >> 8); - pbuf->len[1] = ((dev->d_len - IP_HDRLEN) & 0xff); + pbuf->len[0] = ((dev->d_len - IPv6_HDRLEN) >> 8); + pbuf->len[1] = ((dev->d_len - IPv6_HDRLEN) & 0xff); #else /* CONFIG_NET_IPv6 */ @@ -142,7 +142,7 @@ static void tcp_sendcomplete(FAR struct net_driver_s *dev) /* Calculate IP checksum. */ pbuf->ipchksum = 0; - pbuf->ipchksum = ~(ip_chksum(dev)); + pbuf->ipchksum = ~(ipv4_chksum(dev)); #endif /* CONFIG_NET_IPv6 */ diff --git a/nuttx/net/udp/udp_send.c b/nuttx/net/udp/udp_send.c index 52951d536..acb7bddb6 100644 --- a/nuttx/net/udp/udp_send.c +++ b/nuttx/net/udp/udp_send.c @@ -147,7 +147,7 @@ void udp_send(struct net_driver_s *dev, struct udp_conn_s *conn) /* Calculate IP checksum. */ pudpbuf->ipchksum = 0; - pudpbuf->ipchksum = ~(ip_chksum(dev)); + pudpbuf->ipchksum = ~(ipv4_chksum(dev)); #endif /* CONFIG_NET_IPv6 */ diff --git a/nuttx/net/utils/Kconfig b/nuttx/net/utils/Kconfig index 412ece03e..112b257e2 100644 --- a/nuttx/net/utils/Kconfig +++ b/nuttx/net/utils/Kconfig @@ -20,6 +20,7 @@ config NET_ARCH_CHKSUM functions with the following prototypes: uint16_t net_chksum(FAR uint16_t *data, uint16_t len) - uint16_t ip_chksum(FAR struct net_driver_s *dev) + uint16_t ipv4_chksum(FAR struct net_driver_s *dev) + uint16_t ipv6_chksum(FAR struct net_driver_s *dev) uint16_t tcp_chksum(FAR struct net_driver_s *dev); uint16_t udp_chksum(FAR struct net_driver_s *dev); diff --git a/nuttx/net/utils/net_chksum.c b/nuttx/net/utils/net_chksum.c index 14ecc43a5..f2183665e 100644 --- a/nuttx/net/utils/net_chksum.c +++ b/nuttx/net/utils/net_chksum.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/utils/net_chksum.c * - * Copyright (C) 2007-2010, 2012, 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2010, 2012, 2014-2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Redistribution and use in source and binary forms, with or without @@ -123,7 +123,7 @@ static uint16_t upper_layer_chksum(FAR struct net_driver_s *dev, uint8_t proto) #ifdef CONFIG_NET_IPv6 upper_layer_len = (((uint16_t)(pbuf->len[0]) << 8) + pbuf->len[1]); #else /* CONFIG_NET_IPv6 */ - upper_layer_len = (((uint16_t)(pbuf->len[0]) << 8) + pbuf->len[1]) - IP_HDRLEN; + upper_layer_len = (((uint16_t)(pbuf->len[0]) << 8) + pbuf->len[1]) - IPv4_HDRLEN; #endif /* CONFIG_NET_IPv6 */ /* Verify some minimal assumptions */ @@ -145,7 +145,7 @@ static uint16_t upper_layer_chksum(FAR struct net_driver_s *dev, uint8_t proto) /* Sum TCP header and data. */ - sum = chksum(sum, &dev->d_buf[IP_HDRLEN + NET_LL_HDRLEN(dev)], upper_layer_len); + sum = chksum(sum, &dev->d_buf[IPv4_HDRLEN + NET_LL_HDRLEN(dev)], upper_layer_len); return (sum == 0) ? 0xffff : htons(sum); } @@ -264,28 +264,55 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len) #endif /* CONFIG_NET_ARCH_CHKSUM */ /**************************************************************************** - * Name: ip_chksum + * Name: ipv4_chksum * * Description: - * Calculate the IP header checksum of the packet header in d_buf. + * Calculate the IPv4 header checksum of the packet header in d_buf. * - * The IP header checksum is the Internet checksum of the 20 bytes of - * the IP header. + * The IPv4 header checksum is the Internet checksum of the 20 bytes of + * the IPv4 header. * * If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be * provided by architecture-specific logic. * * Returned Value: - * The IP header checksum of the IP header in the d_buf buffer. + * The IPv4 header checksum of the IPv4 header in the d_buf buffer. * ****************************************************************************/ -#if !CONFIG_NET_ARCH_CHKSUM -uint16_t ip_chksum(FAR struct net_driver_s *dev) +#if defined(CONFIG_NET_IPv4) && !defined(CONFIG_NET_ARCH_CHKSUM) +uint16_t ipv4_chksum(FAR struct net_driver_s *dev) +{ + uint16_t sum; + + sum = chksum(0, &dev->d_buf[NET_LL_HDRLEN(dev)], IPv4_HDRLEN); + return (sum == 0) ? 0xffff : htons(sum); +} +#endif /* CONFIG_NET_ARCH_CHKSUM */ + +/**************************************************************************** + * Name: ipv6_chksum + * + * Description: + * Calculate the IPv6 header checksum of the packet header in d_buf. + * + * The IPv6 header checksum is the Internet checksum of the 40 bytes of + * the IPv6 header. + * + * If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be + * provided by architecture-specific logic. + * + * Returned Value: + * The IPv6 header checksum of the IPv6 header in the d_buf buffer. + * + ****************************************************************************/ + +#if defined(CONFIG_NET_IPv6) && !defined(CONFIG_NET_ARCH_CHKSUM) +uint16_t ipv6_chksum(FAR struct net_driver_s *dev) { uint16_t sum; - sum = chksum(0, &dev->d_buf[NET_LL_HDRLEN(dev)], IP_HDRLEN); + sum = chksum(0, &dev->d_buf[NET_LL_HDRLEN(dev)], IPv6_HDRLEN); return (sum == 0) ? 0xffff : htons(sum); } #endif /* CONFIG_NET_ARCH_CHKSUM */ |