Networking: Update ICMPv6 logic to RFCs

This commit is contained in:
Gregory Nutt 2015-01-20 11:06:51 -06:00
parent 92f440c20c
commit d68cd0d8de
11 changed files with 181 additions and 78 deletions

View File

@ -94,14 +94,25 @@
#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 */
/* Option types */
#define ICMPv6_OPT_SRCLLADDR 1 /* Source Link-Layer Address */
#define ICMPv6_OPT_TGTLLADDR 2 /* Target Link-Layer Address */
#define ICMPv6_OPT_PREFIX 3 /* Prefix Information */
#define ICMPv6_OPT_REDIRECT 4 /* Redirected Header */
#define ICMPv6_OPT_MTU 5 /* MTU */
/* ICMPv6 Neighbor Advertisement message flags */
#define ICMPv6_FLAG_R (1 << 7) /* Router flag */
#define ICMPv6_FLAG_S (1 << 6) /* Solicited flag */
#define ICMPv6_FLAG_O (1 << 5) /* Override flag */
/****************************************************************************
* Public Type Definitions
****************************************************************************/
@ -113,7 +124,7 @@ 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) */
uint8_t tcf; /* Bits 0-3: traffic class (LS), 4-bits: 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) */
@ -124,21 +135,44 @@ struct icmpv6_iphdr_s
/* ICMPv6 header */
uint8_t type; /* Defines the format of the ICMP message */
uint8_t icode; /* Further qualifies the ICMP messages */
uint16_t icmpv6chksum; /* Checksum of ICMP header and data */
uint8_t code; /* Further qualifies the ICMP messages */
uint16_t chksum; /* 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 */
/* This the message format for the ICMPv6 Neighbor Solicitation message */
uint8_t flags;
uint8_t reserved1;
uint8_t reserved2;
uint8_t reserved3;
uint8_t icmpv6data[16];
uint8_t options[1];
struct icmpv6_neighbor_solicit_s
{
uint8_t type; /* Message Type: ICMPv6_NEIGHBOR_SOLICIT */
uint8_t code; /* Further qualifies the ICMP messages */
uint16_t chksum; /* Checksum of ICMP header and data */
uint8_t flags[4]; /* See ICMPv6_FLAG_ definitions */
net_ipv6addr_t tgtaddr; /* 128-bit Target IPv6 address */
uint8_t opttype; /* Option Type: ICMPv6_OPT_SRCLLADDR */
uint8_t optlen; /* Option length: 8 octets */
#ifdef CONFIG_NET_ETHERNET
uint8_t srclladdr[6]; /* Options: Source link layer address */
#endif
};
/* This the message format for the ICMPv6 Neighbor Advertisement message */
struct icmpv6_neighbor_advertise_s
{
uint8_t type; /* Message Type: ICMPv6_NEIGHBOR_ADVERTISE */
uint8_t code; /* Further qualifies the ICMP messages */
uint16_t chksum; /* Checksum of ICMP header and data */
uint8_t flags[4]; /* See ICMPv6_FLAG_ definitions */
net_ipv6addr_t tgtaddr; /* Target IPv6 address */
uint8_t opttype; /* Option Type: ICMPv6_OPT_TGTLLADDR */
uint8_t optlen; /* Option length: 8 octets */
#ifdef CONFIG_NET_ETHERNET
uint8_t tgtlladdr[6]; /* Options: Target link layer address */
#endif
};
/* The structure holding the ICMP statistics that are gathered if

View File

@ -83,7 +83,7 @@
* UDP_POLL periodically from the drivers to support (1) timed
* PKT_POLL operations, and (2) to check if the socket layer has
* ICMP_POLL data that it wants to send
* OUT: Not used
* ICMPv6_POLL OUT: Not used
*
* TCP_BACKLOG IN: There is a new connection in the backlog list set
* up by the listen() command. (TCP only)
@ -126,6 +126,7 @@
#define UDP_POLL TCP_POLL
#define PKT_POLL TCP_POLL
#define ICMP_POLL TCP_POLL
#define ICMPv6_POLL TCP_POLL
#define TCP_BACKLOG (1 << 5)
#define TCP_CLOSE (1 << 6)
#define TCP_ABORT (1 << 7)
@ -178,6 +179,7 @@ struct devif_callback_s
/****************************************************************************
* Public Data
****************************************************************************/
/* Well-known addresses */
#ifdef CONFIG_NET_IPv4
extern const in_addr_t g_ipv4_alloneaddr;
@ -193,16 +195,22 @@ extern const net_ipv6addr_t g_ipv6_allzeroaddr;
extern uint16_t g_ipid;
#if defined(CONFIG_NET_TCP_REASSEMBLY) && !defined(CONFIG_NET_IPv6)
/* Reassembly timer (units: deci-seconds) */
#if defined(CONFIG_NET_TCP_REASSEMBLY) && !defined(CONFIG_NET_IPv6)
extern uint8_t g_reassembly_timer;
#endif
#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING)
/* List of applications waiting for ICMP ECHO REPLY */
#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING)
extern struct devif_callback_s *g_echocallback;
extern struct devif_callback_s *g_icmp_echocallback;
#endif
#if defined(CONFIG_NET_ICMPv6) && defined(CONFIG_NET_ICMPv6_PING)
/* List of applications waiting for ICMPv6 ECHO REPLY */
extern struct devif_callback_s *g_icmpv6_echocallback;
#endif
/****************************************************************************

View File

@ -77,7 +77,7 @@
****************************************************************************/
#ifdef CONFIG_NET_ICMP_PING
FAR struct devif_callback_s *g_echocallback = NULL;
FAR struct devif_callback_s *g_icmp_echocallback = NULL;
#endif
/****************************************************************************
@ -182,9 +182,9 @@ void icmp_input(FAR struct net_driver_s *dev)
*/
#ifdef CONFIG_NET_ICMP_PING
else if (picmp->type == ICMP_ECHO_REPLY && g_echocallback)
else if (picmp->type == ICMP_ECHO_REPLY && g_icmp_echocallback)
{
(void)devif_callback_execute(dev, picmp, ICMP_ECHOREPLY, g_echocallback);
(void)devif_callback_execute(dev, picmp, ICMP_ECHOREPLY, g_icmp_echocallback);
}
#endif

View File

@ -70,8 +70,8 @@
/* Allocate a new ICMP data callback */
#define icmp_callback_alloc() devif_callback_alloc(&g_echocallback)
#define icmp_callback_free(cb) devif_callback_free(cb, &g_echocallback)
#define icmp_callback_alloc() devif_callback_alloc(&g_icmp_echocallback)
#define icmp_callback_free(cb) devif_callback_free(cb, &g_icmp_echocallback)
/****************************************************************************
* Private Types

View File

@ -96,7 +96,7 @@ void icmp_poll(FAR struct net_driver_s *dev)
/* Perform the application callback */
(void)devif_callback_execute(dev, NULL, ICMP_POLL, g_echocallback);
(void)devif_callback_execute(dev, NULL, ICMP_POLL, g_icmp_echocallback);
}
#endif /* CONFIG_NET && CONFIG_NET_ICMP && CONFIG_NET_ICMP_PING */

View File

@ -16,6 +16,13 @@ config NET_ICMPv6
if NET_ICMPv6
config NET_ICMPv6_PING
bool "ICMPv6 ping interfaces"
default n
---help---
Provide interfaces to support application level support for
for sending ECHO (ping) requests and associating ECHO replies.
endif # NET_ICMPv6
endmenu # ICMPv6 Networking Support
endif # NET_IPv6

View File

@ -71,6 +71,11 @@
#define ETHBUF ((struct eth_hdr_s *)&dev->d_buf[0])
#define ICMPv6BUF ((struct icmpv6_iphdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
#define ICMPv6SOLICIT \
((struct icmpv6_neighbor_solicit_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
#define ICMPv6ADVERTISE \
((struct icmpv6_neighbor_advertise_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
/****************************************************************************
* Public Variables
****************************************************************************/
@ -79,8 +84,8 @@
* Private Variables
****************************************************************************/
#ifdef CONFIG_NET_ICMPv6v6_PING
FAR struct devif_callback_s *g_echocallback = NULL;
#ifdef CONFIG_NET_ICMPv6_PING
FAR struct devif_callback_s *g_icmpv6_echocallback = NULL;
#endif
/****************************************************************************
@ -127,42 +132,84 @@ void icmpv6_input(FAR struct net_driver_s *dev)
if (icmp->type == ICMPv6_NEIGHBOR_SOLICIT)
{
FAR struct icmpv6_neighbor_solicit_s *sol;
FAR struct icmpv6_neighbor_advertise_s *adv;
/* If the data matches our address, add the neighbor to the list
* of neighbors.
*
* Missing checks:
* optlen = 1 (8 octets)
*/
if (net_ipv6addr_cmp(icmp->icmpv6data, dev->d_ipv6addr))
sol = ICMPv6SOLICIT;
if (net_ipv6addr_cmp(sol->tgtaddr, dev->d_ipv6addr))
{
if (icmp->options[0] == ICMPv6_OPTION_SOURCE_LINK_ADDRESS)
if (sol->opttype == ICMPv6_OPT_SRCLLADDR)
{
/* Save the sender's address in our neighbor list. */
net_neighbor_add(icmp->srcipaddr,
(FAR struct net_neighbor_addr_s *)&(icmp->options[2]));
(FAR struct net_neighbor_addr_s *)sol->srclladdr);
}
/* We should now send a neighbor advertisement back to where the
* neighbor solicitation came from.
*/
icmp->type = ICMPv6_NEIGHBOR_ADVERTISE;
icmp->flags = ICMPv6_FLAG_S; /* Solicited flag. */
/* Set up the IPv6 header (most is probably already in place) */
icmp->reserved1 = 0;
icmp->reserved2 = 0;
icmp->reserved3 = 0;
icmp->vtc = 0x60; /* Version/traffic class (MS) */
icmp->tcf = 0; /* Traffic class (LS)/Flow label (MS) */
icmp->flow = 0; /* Flow label (LS) */
/* Length excludes the IPv6 header */
icmp->len[0] = (sizeof(struct icmpv6_neighbor_advertise_s) >> 8);
icmp->len[1] = (sizeof(struct icmpv6_neighbor_advertise_s) & 0xff);
icmp->proto = IP_PROTO_ICMP6; /* Next header */
icmp->ttl = IP_TTL; /* Hop limit */
/* Swap source for destination IP address, add our source IP
* address
*/
net_ipv6addr_copy(icmp->destipaddr, icmp->srcipaddr);
net_ipv6addr_copy(icmp->srcipaddr, dev->d_ipv6addr);
icmp->options[0] = ICMPv6_OPTION_TARGET_LINK_ADDRESS;
icmp->options[1] = 1; /* Options length, 1 = 8 bytes. */
memcpy(&(icmp->options[2]), &dev->d_mac, IFHWADDRLEN);
/* Set up the ICMPv6 Neighbor Advertise response */
icmp->icmpv6chksum = 0;
icmp->icmpv6chksum = ~icmpv6_chksum(dev);
adv = ICMPv6ADVERTISE;
adv->type = ICMPv6_NEIGHBOR_ADVERTISE; /* Message type */
adv->code = 0; /* Message qualifier */
adv->flags[0] = ICMPv6_FLAG_S; /* Solicited flag. */
adv->flags[1] = 0;
adv->flags[2] = 0;
adv->flags[3] = 0;
adv->opttype = ICMPv6_OPT_TGTLLADDR; /* Option type */
adv->optlen = 1; /* Option length = 1 octet */
/* Copy our link layer address into the message
* REVISIT: What if the link layer is not Ethernet?
*/
memcpy(&(adv->tgtlladdr), &dev->d_mac, IFHWADDRLEN);
/* Calculate the checksum over both the ICMP header and payload */
icmp->chksum = 0;
icmp->chksum = ~icmpv6_chksum(dev);
/* Set the size to the size of the IPv6 header and the payload size */
dev->d_len = IPv6_HDRLEN + sizeof(struct icmpv6_neighbor_advertise_s);
#ifdef CONFIG_NET_ETHERNET
/* Add the size of the Ethernet header */
dev->d_len += ETH_HDRLEN;
/* Move the source and to the destination addresses in the
* Ethernet header and use our MAC as the new source address.
*/
@ -195,8 +242,8 @@ void icmpv6_input(FAR struct net_driver_s *dev)
net_ipv6addr_copy(icmp->destipaddr, icmp->srcipaddr);
net_ipv6addr_copy(icmp->srcipaddr, dev->d_ipv6addr);
icmp->icmpv6chksum = 0;
icmp->icmpv6chksum = ~icmpv6_chksum(dev);
icmp->chksum = 0;
icmp->chksum = ~icmpv6_chksum(dev);
}
/* If an ICMPv6 echo reply is received then there should also be
@ -204,15 +251,15 @@ void icmpv6_input(FAR struct net_driver_s *dev)
*/
#ifdef CONFIG_NET_ICMPv6v6_PING
else if (icmp->type == ICMPv6_ECHO_REPLY && g_echocallback)
else if (icmp->type == ICMPv6_ECHO_REPLY && g_icmpv6_echocallback)
{
uint16_t flags = ICMPv6_ECHOREPLY;
if (g_echocallback)
if (g_icmpv6_echocallback)
{
/* Dispatch the ECHO reply to the waiting thread */
flags = devif_callback_execute(dev, icmp, flags, g_echocallback);
flags = devif_callback_execute(dev, icmp, flags, g_icmpv6_echocallback);
}
/* If the ECHO reply was not handled, then drop the packet */

View File

@ -70,8 +70,8 @@
/* 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)
#define icmpv6_callback_alloc() devif_callback_alloc(&g_icmpv6_echocallback)
#define icmpv6_callback_free(cb) devif_callback_free(cb, &g_icmpv6_echocallback)
/****************************************************************************
* Private Types

View File

@ -96,7 +96,7 @@ void icmpv6_poll(FAR struct net_driver_s *dev)
/* Perform the application callback */
(void)devif_callback_execute(dev, NULL, ICMPv6_POLL, g_echocallback);
(void)devif_callback_execute(dev, NULL, ICMPv6_POLL, g_icmpv6_echocallback);
}
#endif /* CONFIG_NET && CONFIG_NET_ICMPv6 && CONFIG_NET_ICMPv6_PING */

View File

@ -40,6 +40,7 @@
#include <nuttx/config.h>
#if defined(CONFIG_NET) && defined(CONFIG_NET_ICMPv6) && defined(CONFIG_NET_ICMPv6_PING)
#include <string.h>
#include <debug.h>
#include <arpa/inet.h>
@ -48,6 +49,7 @@
#include <nuttx/net/netdev.h>
#include <nuttx/net/netstats.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/icmpv6.h>
#include "devif/devif.h"
#include "utils/utils.h"
@ -94,7 +96,7 @@
void icmpv6_send(FAR struct net_driver_s *dev, FAR net_ipv6addr_t *destaddr)
{
FAR struct icmpv6_iphdr_s *picmpv6 = ICMPv6BUF;
FAR struct icmpv6_iphdr_s *icmp = ICMPv6BUF;
if (dev->d_sndlen > 0)
{
@ -114,28 +116,24 @@ void icmpv6_send(FAR struct net_driver_s *dev, FAR net_ipv6addr_t *destaddr)
* 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;
icmp->vtc = 0x60;
icmp->tcf = 0x00;
icmp->flow = 0x00;
icmp->len[0] = (dev->d_sndlen >> 8);
icmp->len[1] = (dev->d_sndlen & 0xff);
icmp->proto = IP_PROTO_ICMP6;
icmp->ttl = IP_TTL;
net_ipv6addr_copy(picmpv6->srcipaddr, &dev->d_ipaddr);
net_ipv6addr_copy(picmpv6->destipaddr, destaddr);
net_ipv6addr_copy(icmp->srcipaddr, dev->d_ipv6addr);
net_ipv6addr_copy(icmp->destipaddr, destaddr);
/* Calculate the ICMPv6 checksum. */
picmpv6->icmpv6chksum = 0;
picmpv6->icmpv6chksum = ~(icmpv6_chksum(dev, dev->d_sndlen));
if (picmpv6->icmpv6chksum == 0)
{
picmpv6->icmpv6chksum = 0xffff;
}
icmp->chksum = 0;
icmp->chksum = ~icmpv6_chksum(dev);
nllvdbg("Outgoing ICMPv6 packet length: %d (%d)\n",
dev->d_len, (picmpv6->len[0] << 8) | picmpv6->len[1]);
dev->d_len, (icmp->len[0] << 8) | icmp->len[1]);
#ifdef CONFIG_NET_STATISTICS
g_netstats.icmpv6.sent++;

View File

@ -74,9 +74,9 @@
#if !CONFIG_NET_ARCH_CHKSUM
static uint16_t chksum(uint16_t sum, FAR const uint8_t *data, uint16_t len)
{
FAR const uint8_t *dataptr;
FAR const uint8_t *last_byte;
uint16_t t;
const uint8_t *dataptr;
const uint8_t *last_byte;
dataptr = data;
last_byte = data + len - 1;
@ -85,7 +85,7 @@ static uint16_t chksum(uint16_t sum, FAR const uint8_t *data, uint16_t len)
{
/* At least two more bytes */
t = (dataptr[0] << 8) + dataptr[1];
t = ((uint16_t)dataptr[0] << 8) + dataptr[1];
sum += t;
if (sum < t)
{
@ -119,11 +119,16 @@ static uint16_t chksum(uint16_t sum, FAR const uint8_t *data, uint16_t len)
static uint16_t ipv4_upperlayer_chksum(FAR struct net_driver_s *dev,
uint8_t proto)
{
FAR struct ipv4_hdr_s *pbuf = IPv4BUF;
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
uint16_t upperlen;
uint16_t sum;
upperlen = (((uint16_t)(pbuf->len[0]) << 8) + pbuf->len[1]) - IPv4_HDRLEN;
/* The length reported in the IPv4 header is the length of both the IPv4
* header and the payload that follows the header. We need to subtract
* the size of the IPv4 header to get the size of the payload.
*/
upperlen = (((uint16_t)(ipv4->len[0]) << 8) + ipv4->len[1]) - IPv4_HDRLEN;
/* Verify some minimal assumptions */
@ -139,12 +144,11 @@ static uint16_t ipv4_upperlayer_chksum(FAR struct net_driver_s *dev,
/* Sum IP source and destination addresses. */
sum = chksum(sum, (uint8_t *)&pbuf->srcipaddr, 2 * sizeof(in_addr_t));
sum = chksum(sum, (FAR uint8_t *)&ipv4->srcipaddr, 2 * sizeof(in_addr_t));
/* Sum TCP header and data. */
/* Sum IP payload data. */
sum = chksum(sum, &dev->d_buf[IPv4_HDRLEN + NET_LL_HDRLEN(dev)], upperlen);
return (sum == 0) ? 0xffff : htons(sum);
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
@ -157,11 +161,15 @@ static uint16_t ipv4_upperlayer_chksum(FAR struct net_driver_s *dev,
static uint16_t ipv6_upperlayer_chksum(FAR struct net_driver_s *dev,
uint8_t proto)
{
FAR struct ipv6_hdr_s *pbuf = IPv6BUF;
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
uint16_t upperlen;
uint16_t sum;
upperlen = (((uint16_t)(pbuf->len[0]) << 8) + pbuf->len[1]);
/* The length reported in the IPv6 header is the length of the payload
* that follows the header.
*/
upperlen = ((uint16_t)ipv6->len[0] << 8) + ipv6->len[1];
/* Verify some minimal assumptions */
@ -170,19 +178,20 @@ static uint16_t ipv6_upperlayer_chksum(FAR struct net_driver_s *dev,
return 0;
}
/* First sum pseudo-header. */
/* IP protocol and length fields. This addition cannot carry. */
/* The checksum is calculated starting with a pseudo-header of IPv6 header
* fields according to the IPv6 standard, which consists of the source
* and destination addresses, the packet length and the next header field.
*/
sum = upperlen + proto;
/* Sum IP source and destination addresses. */
sum = chksum(sum, (uint8_t *)&pbuf->srcipaddr, 2 * sizeof(net_ipv6addr_t));
sum = chksum(sum, (FAR uint8_t *)&ipv6->srcipaddr, 2 * sizeof(net_ipv6addr_t));
/* Sum TCP header and data. */
/* Sum IP payload data. */
sum = chksum(sum, &dev->d_buf[IPv6_HDRLEN + NET_LL_HDRLEN(dev)], upperlen);
return (sum == 0) ? 0xffff : htons(sum);
}
#endif /* CONFIG_NET_ARCH_CHKSUM */