summaryrefslogtreecommitdiffstats
path: root/src/net/icmp.c
diff options
context:
space:
mode:
authorMichael Brown2013-10-21 15:10:07 +0200
committerMichael Brown2013-10-21 16:08:12 +0200
commit5c2ffc26cc9b73ef60cbd123cd90f499dc01bbc2 (patch)
tree5dc77a8f4b54549e1468ecf93d1cd5cc454f6e60 /src/net/icmp.c
parent[resolv] Use sock_aton() to allow parsing of arbitrary numeric addresses (diff)
downloadipxe-5c2ffc26cc9b73ef60cbd123cd90f499dc01bbc2.tar.gz
ipxe-5c2ffc26cc9b73ef60cbd123cd90f499dc01bbc2.tar.xz
ipxe-5c2ffc26cc9b73ef60cbd123cd90f499dc01bbc2.zip
[icmp] Add support for sending ICMP echo requests
Merge common functionality between IPv4 and IPv6 ICMP echo handling, and add support for transmitting ICMP echo requests and delivering ICMP echo replies to a (not yet implemented) ping_rx() function. Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/net/icmp.c')
-rw-r--r--src/net/icmp.c229
1 files changed, 174 insertions, 55 deletions
diff --git a/src/net/icmp.c b/src/net/icmp.c
index 6142b748..1bbf8bd3 100644
--- a/src/net/icmp.c
+++ b/src/net/icmp.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -20,10 +20,13 @@
FILE_LICENCE ( GPL2_OR_LATER );
#include <string.h>
+#include <byteswap.h>
#include <errno.h>
#include <ipxe/iobuf.h>
#include <ipxe/in.h>
#include <ipxe/tcpip.h>
+#include <ipxe/ping.h>
+#include <ipxe/crc32.h>
#include <ipxe/icmp.h>
/** @file
@@ -32,76 +35,192 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/
-struct tcpip_protocol icmp_protocol __tcpip_protocol;
+/**
+ * Identify ICMP echo protocol
+ *
+ * @v st_family Address family
+ * @ret echo_protocol ICMP echo protocol, or NULL
+ */
+static struct icmp_echo_protocol * icmp_echo_protocol ( sa_family_t family ) {
+ struct icmp_echo_protocol *echo_protocol;
+
+ for_each_table_entry ( echo_protocol, ICMP_ECHO_PROTOCOLS ) {
+ if ( echo_protocol->family == family )
+ return echo_protocol;
+ }
+ return NULL;
+}
/**
- * Process a received packet
+ *
+ * Determine debugging colour for ICMP debug messages
+ *
+ * @v st_peer Peer address
+ * @ret col Debugging colour (for DBGC())
+ */
+static uint32_t icmpcol ( struct sockaddr_tcpip *st_peer ) {
+
+ return crc32_le ( 0, st_peer, sizeof ( *st_peer ) );
+}
+
+/**
+ * Transmit ICMP echo packet
*
* @v iobuf I/O buffer
- * @v netdev Network device
- * @v st_src Partially-filled source address
- * @v st_dest Partially-filled destination address
- * @v pshdr_csum Pseudo-header checksum
+ * @v st_dest Destination socket address
+ * @v echo_protocol ICMP echo protocol
* @ret rc Return status code
*/
-static int icmp_rx ( struct io_buffer *iobuf,
- struct net_device *netdev __unused,
- struct sockaddr_tcpip *st_src,
- struct sockaddr_tcpip *st_dest,
- uint16_t pshdr_csum __unused ) {
- struct icmp_header *icmp = iobuf->data;
- size_t len = iob_len ( iobuf );
- unsigned int csum;
+static int icmp_tx_echo ( struct io_buffer *iobuf,
+ struct sockaddr_tcpip *st_dest,
+ struct icmp_echo_protocol *echo_protocol ) {
+ struct icmp_echo *echo = iobuf->data;
int rc;
- /* Sanity check */
- if ( len < sizeof ( *icmp ) ) {
- DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
- len, sizeof ( *icmp ) );
- rc = -EINVAL;
- goto done;
- }
+ /* Set ICMP type and (re)calculate checksum */
+ echo->icmp.chksum = 0;
+ echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );
- /* Verify checksum */
- csum = tcpip_chksum ( icmp, len );
- if ( csum != 0 ) {
- DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
- csum );
- DBG_HD ( icmp, len );
- rc = -EINVAL;
- goto done;
+ /* Transmit packet */
+ if ( ( rc = tcpip_tx ( iobuf, echo_protocol->tcpip_protocol, NULL,
+ st_dest, NULL,
+ ( echo_protocol->net_checksum ?
+ &echo->icmp.chksum : NULL ) ) ) != 0 )
+ return rc;
+
+ return 0;
+}
+
+/**
+ * Transmit ICMP echo request
+ *
+ * @v iobuf I/O buffer
+ * @v st_dest Destination socket address
+ * @ret rc Return status code
+ */
+int icmp_tx_echo_request ( struct io_buffer *iobuf,
+ struct sockaddr_tcpip *st_dest ) {
+ struct icmp_echo *echo = iobuf->data;
+ struct icmp_echo_protocol *echo_protocol;
+ int rc;
+
+ /* Identify ICMP echo protocol */
+ echo_protocol = icmp_echo_protocol ( st_dest->st_family );
+ if ( ! echo_protocol ) {
+ DBGC ( icmpcol ( st_dest ), "ICMP TX echo request unknown "
+ "address family %d\n", st_dest->st_family );
+ free_iob ( iobuf );
+ return -ENOTSUP;
}
- /* We respond only to pings */
- if ( icmp->type != ICMP_ECHO_REQUEST ) {
- DBG ( "ICMP ignoring type %d\n", icmp->type );
- rc = 0;
- goto done;
+ /* Set type */
+ echo->icmp.type = echo_protocol->request;
+
+ /* Transmit request */
+ DBGC ( icmpcol ( st_dest ), "ICMP TX echo request id %04x seq %04x\n",
+ ntohs ( echo->ident ), ntohs ( echo->sequence ) );
+ if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
+ return rc;
+
+ return 0;
+}
+
+/**
+ * Transmit ICMP echo reply
+ *
+ * @v iobuf I/O buffer
+ * @v st_dest Destination socket address
+ * @ret rc Return status code
+ */
+static int icmp_tx_echo_reply ( struct io_buffer *iobuf,
+ struct sockaddr_tcpip *st_dest,
+ struct icmp_echo_protocol *echo_protocol ) {
+ struct icmp_echo *echo = iobuf->data;
+ int rc;
+
+ /* Set type */
+ echo->icmp.type = echo_protocol->reply;
+
+ /* Transmit reply */
+ DBGC ( icmpcol ( st_dest ), "ICMP TX echo reply id %04x seq %04x\n",
+ ntohs ( echo->ident ), ntohs ( echo->sequence ) );
+ if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
+ return rc;
+
+ return 0;
+}
+
+/**
+ * Process a received ICMP echo request
+ *
+ * @v iobuf I/O buffer
+ * @v st_src Source socket address
+ * @v echo_protocol ICMP echo protocol
+ * @ret rc Return status code
+ */
+int icmp_rx_echo_request ( struct io_buffer *iobuf,
+ struct sockaddr_tcpip *st_src,
+ struct icmp_echo_protocol *echo_protocol ) {
+ struct icmp_echo *echo = iobuf->data;
+ int rc;
+
+ /* Sanity check */
+ if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
+ DBGC ( icmpcol ( st_src ), "ICMP RX echo request too short at "
+ "%zd bytes (min %zd bytes)\n",
+ iob_len ( iobuf ), sizeof ( *echo ) );
+ free_iob ( iobuf );
+ return -EINVAL;
}
+ DBGC ( icmpcol ( st_src ), "ICMP RX echo request id %04x seq %04x\n",
+ ntohs ( echo->ident ), ntohs ( echo->sequence ) );
- DBG ( "ICMP responding to ping\n" );
+ /* Transmit echo reply */
+ if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
+ return rc;
- /* Change type to response and recalculate checksum */
- icmp->type = ICMP_ECHO_RESPONSE;
- icmp->chksum = 0;
- icmp->chksum = tcpip_chksum ( icmp, len );
+ return 0;
+}
- /* Transmit the response */
- if ( ( rc = tcpip_tx ( iob_disown ( iobuf ), &icmp_protocol, st_dest,
- st_src, NULL, NULL ) ) != 0 ) {
- DBG ( "ICMP could not transmit ping response: %s\n",
- strerror ( rc ) );
- goto done;
+/**
+ * Process a received ICMP echo request
+ *
+ * @v iobuf I/O buffer
+ * @v st_src Source socket address
+ * @ret rc Return status code
+ */
+int icmp_rx_echo_reply ( struct io_buffer *iobuf,
+ struct sockaddr_tcpip *st_src ) {
+ struct icmp_echo *echo = iobuf->data;
+ int rc;
+
+ /* Sanity check */
+ if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
+ DBGC ( icmpcol ( st_src ), "ICMP RX echo reply too short at "
+ "%zd bytes (min %zd bytes)\n",
+ iob_len ( iobuf ), sizeof ( *echo ) );
+ free_iob ( iobuf );
+ return -EINVAL;
}
+ DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
+ ntohs ( echo->ident ), ntohs ( echo->sequence ) );
- done:
- free_iob ( iobuf );
- return rc;
+ /* Deliver to ping protocol */
+ if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
+ return rc;
+
+ return 0;
}
-/** ICMP TCP/IP protocol */
-struct tcpip_protocol icmp_protocol __tcpip_protocol = {
- .name = "ICMP",
- .rx = icmp_rx,
- .tcpip_proto = IP_ICMP,
-};
+/**
+ * Receive ping reply (when no ping protocol is present)
+ *
+ * @v iobuf I/O buffer
+ * @v st_src Source socket address
+ * @ret rc Return status code
+ */
+__weak int ping_rx ( struct io_buffer *iobuf,
+ struct sockaddr_tcpip *st_src __unused ) {
+ free_iob ( iobuf );
+ return 0;
+}