diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2014-05-30 13:32:10 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2014-05-30 13:32:10 -0600 |
commit | 5e4a993391733fbaf705b7e4b01e9a6822efb165 (patch) | |
tree | 59fb622a57c6eae7bec65564bfb3e699b6915e5f | |
parent | 8a451ae8460919136103be464e363545ccc199fe (diff) | |
download | nuttx-5e4a993391733fbaf705b7e4b01e9a6822efb165.tar.gz nuttx-5e4a993391733fbaf705b7e4b01e9a6822efb165.tar.bz2 nuttx-5e4a993391733fbaf705b7e4b01e9a6822efb165.zip |
Move ICMP files from net/uip to net/icmp
-rw-r--r-- | nuttx/net/Makefile | 1 | ||||
-rw-r--r-- | nuttx/net/icmp/Make.defs | 59 | ||||
-rw-r--r-- | nuttx/net/icmp/icmp_input.c | 317 | ||||
-rw-r--r-- | nuttx/net/icmp/icmp_ping.c | 388 | ||||
-rw-r--r-- | nuttx/net/icmp/icmp_poll.c | 103 | ||||
-rw-r--r-- | nuttx/net/icmp/icmp_send.c | 168 | ||||
-rw-r--r-- | nuttx/net/uip/Make.defs | 13 |
7 files changed, 1036 insertions, 13 deletions
diff --git a/nuttx/net/Makefile b/nuttx/net/Makefile index f3fda059a..fa0ea5753 100644 --- a/nuttx/net/Makefile +++ b/nuttx/net/Makefile @@ -100,6 +100,7 @@ NETDEV_CSRCS += netdev_rxnotify.c endif include arp/Make.defs +include icmp/Make.defs include igmp/Make.defs include uip/Make.defs endif diff --git a/nuttx/net/icmp/Make.defs b/nuttx/net/icmp/Make.defs new file mode 100644 index 000000000..59ea9bb0b --- /dev/null +++ b/nuttx/net/icmp/Make.defs @@ -0,0 +1,59 @@ +############################################################################ +# net/icmp/Make.defs +# +# Copyright (C) 2014 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. +# +############################################################################ + +ifeq ($(CONFIG_NET),y) + +# ICMP source files + +ifeq ($(CONFIG_NET_ICMP),y) + +NET_CSRCS += icmp_input.c + +ifeq ($(CONFIG_NET_ICMP_PING),y) +ifneq ($(CONFIG_DISABLE_CLOCK),y) + +NET_CSRCS += icmp_ping.c icmp_poll.c icmp_send.c + +endif +endif +endif + +# Include uip build support + +DEPPATH += --dep-path icmp +VPATH += :icmp +CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)net$(DELIM)icmp} + +endif diff --git a/nuttx/net/icmp/icmp_input.c b/nuttx/net/icmp/icmp_input.c new file mode 100644 index 000000000..1e2626f3b --- /dev/null +++ b/nuttx/net/icmp/icmp_input.c @@ -0,0 +1,317 @@ +/**************************************************************************** + * net/icmp/icmp_input.c + * Handling incoming ICMP/ICMP6 input + * + * Copyright (C) 2007-2009, 2012 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 <debug.h> + +#include <net/if.h> +#include <nuttx/net/uip/uipopt.h> +#include <nuttx/net/uip/uip.h> +#include <nuttx/net/uip/uip-arch.h> + +#include "uip/uip_internal.h" + +#ifdef CONFIG_NET_ICMP + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ICMPBUF ((struct uip_icmpip_hdr *)&dev->d_buf[UIP_LLH_LEN]) + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +#ifdef CONFIG_NET_ICMP_PING +struct uip_callback_s *g_echocallback = NULL; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: uip_icmpinput + * + * Description: + * Handle incoming ICMP/ICMP6 input + * + * Parameters: + * dev - The device driver structure containing the received ICMP/ICMP6 + * packet + * + * Return: + * None + * + * Assumptions: + * Called from the interrupt level or with interrupts disabled. + * + ****************************************************************************/ + +void uip_icmpinput(struct uip_driver_s *dev) +{ + struct uip_icmpip_hdr *picmp = ICMPBUF; + +#ifdef CONFIG_NET_STATISTICS + uip_stat.icmp.recv++; +#endif + +#ifndef CONFIG_NET_IPv6 + /* ICMPv4 processing code follows. */ + + /* ICMP echo (i.e., ping) processing. This is simple, we only change the + * ICMP type from ECHO to ECHO_REPLY and adjust the ICMP checksum before + * we return the packet. + */ + + if (picmp->type == ICMP_ECHO_REQUEST) + { + /* If we are configured to use ping IP address assignment, we use + * the destination IP address of this ping packet and assign it to + * ourself. + */ + +#ifdef CONFIG_NET_PINGADDRCONF + if (dev->d_ipaddr == 0) + { + dev->d_ipaddr = picmp->destipaddr; + } +#endif + + /* Change the ICMP type */ + + picmp->type = ICMP_ECHO_REPLY; + + /* Swap IP addresses. */ + + uiphdr_ipaddr_copy(picmp->destipaddr, picmp->srcipaddr); + uiphdr_ipaddr_copy(picmp->srcipaddr, &dev->d_ipaddr); + + /* Recalculate the ICMP checksum */ + +#if 0 + /* The slow way... sum over the ICMP message */ + + picmp->icmpchksum = 0; + picmp->icmpchksum = ~uip_icmpchksum(dev, (((uint16_t)picmp->len[0] << 8) | (uint16_t)picmp->len[1]) - UIP_IPH_LEN); + if (picmp->icmpchksum == 0) + { + picmp->icmpchksum = 0xffff; + } +#else + /* The quick way -- Since only the type has changed, just adjust the + * checksum for the change of type + */ + + if (picmp->icmpchksum >= HTONS(0xffff - (ICMP_ECHO_REQUEST << 8))) + { + picmp->icmpchksum += HTONS(ICMP_ECHO_REQUEST << 8) + 1; + } + else + { + picmp->icmpchksum += HTONS(ICMP_ECHO_REQUEST << 8); + } +#endif + + nllvdbg("Outgoing ICMP packet length: %d (%d)\n", + dev->d_len, (picmp->len[0] << 8) | picmp->len[1]); + +#ifdef CONFIG_NET_STATISTICS + uip_stat.icmp.sent++; + uip_stat.ip.sent++; +#endif + } + + /* If an ICMP echo reply is received then there should also be + * a thread waiting to received the echo response. + */ + +#ifdef CONFIG_NET_ICMP_PING + else if (picmp->type == ICMP_ECHO_REPLY && g_echocallback) + { + (void)uip_callbackexecute(dev, picmp, UIP_ECHOREPLY, g_echocallback); + } +#endif + + /* Otherwise the ICMP input was not processed */ + + else + { + nlldbg("Unknown ICMP cmd: %d\n", picmp->type); + goto typeerr; + } + + return; + +typeerr: +#ifdef CONFIG_NET_STATISTICS + uip_stat.icmp.typeerr++; + uip_stat.icmp.drop++; +#endif + dev->d_len = 0; + +#else /* !CONFIG_NET_IPv6 */ + + /* If we get a neighbor solicitation for our address we should send + * a neighbor advertisement message back. + */ + + if (picmp->type == ICMP6_NEIGHBOR_SOLICITATION) + { + if (uip_ipaddr_cmp(picmp->icmp6data, dev->d_ipaddr)) + { + if (picmp->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) + { + /* Save the sender's address in our neighbor list. */ + + uiphdr_neighbor_add(picmp->srcipaddr, &(picmp->options[2])); + } + + /* We should now send a neighbor advertisement back to where the + * neighbor solicitation came from. + */ + + picmp->type = ICMP6_NEIGHBOR_ADVERTISEMENT; + picmp->flags = ICMP6_FLAG_S; /* Solicited flag. */ + + picmp->reserved1 = picmp->reserved2 = picmp->reserved3 = 0; + + uiphdr_ipaddr_copy(picmp->destipaddr, picmp->srcipaddr); + uiphdr_ipaddr_copy(picmp->srcipaddr, &dev->d_ipaddr); + picmp->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS; + picmp->options[1] = 1; /* Options length, 1 = 8 bytes. */ + memcpy(&(picmp->options[2]), &dev->d_mac, IFHWADDRLEN); + picmp->icmpchksum = 0; + picmp->icmpchksum = ~uip_icmp6chksum(dev); + } + else + { + goto drop; + } + } + else if (picmp->type == ICMP6_ECHO_REQUEST) + { + /* ICMP echo (i.e., ping) processing. This is simple, we only + * change the ICMP type from ECHO to ECHO_REPLY and update the + * ICMP checksum before we return the packet. + */ + + picmp->type = ICMP6_ECHO_REPLY; + + uiphdr_ipaddr_copy(picmp->destipaddr, picmp->srcipaddr); + uiphdr_ipaddr_copy(picmp->srcipaddr, &dev->d_ipaddr); + picmp->icmpchksum = 0; + picmp->icmpchksum = ~uip_icmp6chksum(dev); + } + + /* If an ICMP echo reply is received then there should also be + * a thread waiting to received the echo response. + */ + +#ifdef CONFIG_NET_ICMP_PING + else if (picmp->type == ICMP6_ECHO_REPLY && g_echocallback) + { + uint16_t flags = UIP_ECHOREPLY; + + if (g_echocallback) + { + /* Dispatch the ECHO reply to the waiting thread */ + + flags = uip_callbackexecute(dev, picmp, flags, g_echocallback); + } + + /* If the ECHO reply was not handled, then drop the packet */ + + if (flags == UIP_ECHOREPLY) + { + /* The ECHO reply was not handled */ + + goto drop; + } + } +#endif + + else + { + nlldbg("Unknown ICMP6 cmd: %d\n", picmp->type); + goto typeerr; + } + + nllvdbg("Outgoing ICMP6 packet length: %d (%d)\n", + dev->d_len, (picmp->len[0] << 8) | picmp->len[1]); + +#ifdef CONFIG_NET_STATISTICS + uip_stat.icmp.sent++; + uip_stat.ip.sent++; +#endif + return; + +typeerr: +#ifdef CONFIG_NET_STATISTICS + uip_stat.icmp.typeerr++; +#endif + +drop: +#ifdef CONFIG_NET_STATISTICS + uip_stat.icmp.drop++; +#endif + dev->d_len = 0; + +#endif /* !CONFIG_NET_IPv6 */ +} + +#endif /* CONFIG_NET_ICMP */ +#endif /* CONFIG_NET */ diff --git a/nuttx/net/icmp/icmp_ping.c b/nuttx/net/icmp/icmp_ping.c new file mode 100644 index 000000000..32c859ca0 --- /dev/null +++ b/nuttx/net/icmp/icmp_ping.c @@ -0,0 +1,388 @@ +/**************************************************************************** + * net/icmp/icmp_ping.c + * + * Copyright (C) 2008-2012 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_ICMP) && \ + defined(CONFIG_NET_ICMP_PING) && !defined(CONFIG_DISABLE_CLOCK) + +#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/uip/uipopt.h> +#include <nuttx/net/uip/uip.h> +#include <nuttx/net/uip/uip-arch.h> + +#include "uip/uip_internal.h" +#include "../net_internal.h" /* Should not include this! */ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ICMPBUF ((struct uip_icmpip_hdr *)&dev->d_buf[UIP_LLH_LEN]) +#define ICMPDAT (&dev->d_buf[UIP_LLH_LEN + sizeof(struct uip_icmpip_hdr)]) + +/* Allocate a new ICMP data callback */ + +#define uip_icmpcallbackalloc() uip_callbackalloc(&g_echocallback) +#define uip_icmpcallbackfree(cb) uip_callbackfree(cb, &g_echocallback) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct icmp_ping_s +{ + FAR struct uip_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 */ + uip_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(struct icmp_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 uIP 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 icmp_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(struct uip_driver_s *dev, void *conn, + void *pvpriv, uint16_t flags) +{ + struct icmp_ping_s *pstate = (struct icmp_ping_s *)pvpriv; + uint8_t *ptr; + int i; + + nllvdbg("flags: %04x\n", flags); + if (pstate) + { + /* Check if this is a ICMP 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 & UIP_ECHOREPLY) != 0 && conn != NULL) + { + struct uip_icmpip_hdr *icmp = (struct uip_icmpip_hdr *)conn; + nlldbg("ECHO reply: id=%d seqno=%d\n", + ntohs(icmp->id), ntohs(icmp->seqno)); + + if (ntohs(icmp->id) == pstate->png_id) + { + /* Consume the ECHOREPLY */ + + flags &= ~UIP_ECHOREPLY; + dev->d_len = 0; + + /* Return the result to the caller */ + + pstate->png_result = OK; + pstate->png_seqno = ntohs(icmp->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 & UIP_NEWDATA) == 0 && /* No incoming data */ + !pstate->png_sent) /* Request not sent */ + { + struct uip_icmpip_hdr *picmp = ICMPBUF; + + /* We can send the ECHO request now. + * + * Format the ICMP ECHO request packet + */ + + picmp->type = ICMP_ECHO_REQUEST; + picmp->icode = 0; +#ifndef CONFIG_NET_IPv6 + picmp->id = htons(pstate->png_id); + picmp->seqno = htons(pstate->png_seqno); +#else +# error "IPv6 ECHO Request not implemented" +#endif + /* Add some easily verifiable data */ + + for (i = 0, ptr = ICMPDAT; i < pstate->png_datlen; i++) + { + *ptr++ = i; + } + + /* Send the ICMP echo request. Note that d_sndlen is set to + * the size of the ICMP payload and does not include the size + * of the ICMP header. + */ + + nlldbg("Send ECHO request: seqno=%d\n", pstate->png_seqno); + + dev->d_sndlen = pstate->png_datlen + 4; + uip_icmpsend(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 (!uip_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: uip_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 uip_ping(uip_ipaddr_t addr, uint16_t id, uint16_t seqno, + uint16_t datalen, int dsecs) +{ + struct icmp_ping_s state; + uip_lock_t save; + + /* 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 = uip_lock(); + state.png_time = clock_systimer(); + + /* Set up the callback */ + + state.png_cb = uip_icmpcallbackalloc(); + if (state.png_cb) + { + state.png_cb->flags = UIP_POLL|UIP_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 */ + + netdev_txnotify(state.png_addr); + + /* Wait for either the full round trip transfer to complete or + * for timeout to occur. (1) uip_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); + uip_lockedwait(&state.png_sem); + + uip_icmpcallbackfree(state.png_cb); + } + + uip_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_ICMP && CONFIG_NET_ICMP_PING ... */ diff --git a/nuttx/net/icmp/icmp_poll.c b/nuttx/net/icmp/icmp_poll.c new file mode 100644 index 000000000..747b8ae05 --- /dev/null +++ b/nuttx/net/icmp/icmp_poll.c @@ -0,0 +1,103 @@ +/**************************************************************************** + * net/icmp/icmp_poll.c + * + * Copyright (C) 2008-2009 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_ICMP) && defined(CONFIG_NET_ICMP_PING) + +#include <debug.h> + +#include <nuttx/net/uip/uipopt.h> +#include <nuttx/net/uip/uip.h> +#include <nuttx/net/uip/uip-arch.h> + +#include "uip/uip_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: uip_icmppoll + * + * 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 uip_icmppoll(struct uip_driver_s *dev) +{ + /* Setup for the application callback */ + + dev->d_appdata = &dev->d_buf[UIP_LLH_LEN + UIP_IPICMPH_LEN]; + dev->d_snddata = &dev->d_buf[UIP_LLH_LEN + UIP_IPICMPH_LEN]; + + dev->d_len = 0; + dev->d_sndlen = 0; + + /* Perform the application callback */ + + (void)uip_callbackexecute(dev, NULL, UIP_POLL, g_echocallback); +} + +#endif /* CONFIG_NET && CONFIG_NET_ICMP && CONFIG_NET_ICMP_PING */ diff --git a/nuttx/net/icmp/icmp_send.c b/nuttx/net/icmp/icmp_send.c new file mode 100644 index 000000000..b46701ed4 --- /dev/null +++ b/nuttx/net/icmp/icmp_send.c @@ -0,0 +1,168 @@ +/**************************************************************************** + * net/icmp/icmp_send.c + * + * Copyright (C) 2008-2010, 2012 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_ICMP) && defined(CONFIG_NET_ICMP_PING) + +#include <debug.h> + +#include <nuttx/net/uip/uipopt.h> +#include <nuttx/net/uip/uip.h> +#include <nuttx/net/uip/uip-arch.h> + +#include "uip/uip_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ICMPBUF ((struct uip_icmpip_hdr *)&dev->d_buf[UIP_LLH_LEN]) + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: uip_icmpsend + * + * Description: + * Setup to send an ICMP 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 uip_icmpsend(struct uip_driver_s *dev, uip_ipaddr_t *destaddr) +{ + struct uip_icmpip_hdr *picmp = ICMPBUF; + + if (dev->d_sndlen > 0) + { + /* The total length to send is the size of the application data plus + * the IP and ICMP headers (and, eventually, the Ethernet header) + */ + + dev->d_len = dev->d_sndlen + UIP_IPICMPH_LEN; + + /* The total size of the data (for ICMP checksum calculation) includes + * the size of the ICMP header + */ + + dev->d_sndlen += UIP_ICMPH_LEN; + + /* Initialize the IP header. Note that for IPv6, the IP length field + * does not include the IPv6 IP header length. + */ + +#ifdef CONFIG_NET_IPv6 + + picmp->vtc = 0x60; + picmp->tcf = 0x00; + picmp->flow = 0x00; + picmp->len[0] = (dev->d_sndlen >> 8); + picmp->len[1] = (dev->d_sndlen & 0xff); + picmp->nexthdr = UIP_PROTO_ICMP; + picmp->hoplimit = UIP_TTL; + + uip_ipaddr_copy(picmp->srcipaddr, &dev->d_ipaddr); + uip_ipaddr_copy(picmp->destipaddr, destaddr); + +#else /* CONFIG_NET_IPv6 */ + + picmp->vhl = 0x45; + picmp->tos = 0; + picmp->len[0] = (dev->d_len >> 8); + picmp->len[1] = (dev->d_len & 0xff); + ++g_ipid; + picmp->ipid[0] = g_ipid >> 8; + picmp->ipid[1] = g_ipid & 0xff; + picmp->ipoffset[0] = UIP_TCPFLAG_DONTFRAG >> 8; + picmp->ipoffset[1] = UIP_TCPFLAG_DONTFRAG & 0xff; + picmp->ttl = UIP_TTL; + picmp->proto = UIP_PROTO_ICMP; + + uiphdr_ipaddr_copy(picmp->srcipaddr, &dev->d_ipaddr); + uiphdr_ipaddr_copy(picmp->destipaddr, destaddr); + + /* Calculate IP checksum. */ + + picmp->ipchksum = 0; + picmp->ipchksum = ~(uip_ipchksum(dev)); + +#endif /* CONFIG_NET_IPv6 */ + + /* Calculate the ICMP checksum. */ + + picmp->icmpchksum = 0; + picmp->icmpchksum = ~(uip_icmpchksum(dev, dev->d_sndlen)); + if (picmp->icmpchksum == 0) + { + picmp->icmpchksum = 0xffff; + } + + nllvdbg("Outgoing ICMP packet length: %d (%d)\n", + dev->d_len, (picmp->len[0] << 8) | picmp->len[1]); + +#ifdef CONFIG_NET_STATISTICS + uip_stat.icmp.sent++; + uip_stat.ip.sent++; +#endif + } +} + +#endif /* CONFIG_NET && CONFIG_NET_ICMP && CONFIG_NET_ICMP_PING */ diff --git a/nuttx/net/uip/Make.defs b/nuttx/net/uip/Make.defs index 24cb9ae97..df31f7eae 100644 --- a/nuttx/net/uip/Make.defs +++ b/nuttx/net/uip/Make.defs @@ -81,19 +81,6 @@ NET_CSRCS += uip_udpcallback.c endif -# ICMP source files - -ifeq ($(CONFIG_NET_ICMP),y) - -NET_CSRCS += uip_icmpinput.c - -ifeq ($(CONFIG_NET_ICMP_PING),y) -ifneq ($(CONFIG_DISABLE_CLOCK),y) -NET_CSRCS += uip_icmpping.c uip_icmppoll.c uip_icmpsend.c -endif -endif -endif - # Include uip build support DEPPATH += --dep-path uip |