iPXE
Functions
icmp.c File Reference

ICMP protocol. More...

#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>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static struct icmp_echo_protocolicmp_echo_protocol (sa_family_t family)
 Identify ICMP echo protocol.
static uint32_t icmpcol (struct sockaddr_tcpip *st_peer)
 Determine debugging colour for ICMP debug messages.
static int icmp_tx_echo (struct io_buffer *iobuf, struct sockaddr_tcpip *st_dest, struct icmp_echo_protocol *echo_protocol)
 Transmit ICMP echo packet.
int icmp_tx_echo_request (struct io_buffer *iobuf, struct sockaddr_tcpip *st_dest)
 Transmit ICMP echo request.
static int icmp_tx_echo_reply (struct io_buffer *iobuf, struct sockaddr_tcpip *st_dest, struct icmp_echo_protocol *echo_protocol)
 Transmit ICMP echo reply.
int icmp_rx_echo_request (struct io_buffer *iobuf, struct sockaddr_tcpip *st_src, struct icmp_echo_protocol *echo_protocol)
 Process a received ICMP echo request.
int icmp_rx_echo_reply (struct io_buffer *iobuf, struct sockaddr_tcpip *st_src)
 Process a received ICMP echo request.
__weak int ping_rx (struct io_buffer *iobuf, struct sockaddr_tcpip *st_src __unused)
 Receive ping reply (when no ping protocol is present)

Detailed Description

ICMP protocol.

Definition in file icmp.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static struct icmp_echo_protocol* icmp_echo_protocol ( sa_family_t  family) [static, read]

Identify ICMP echo protocol.

Parameters:
st_familyAddress family
Return values:
echo_protocolICMP echo protocol, or NULL

Definition at line 48 of file icmp.c.

References icmp_echo_protocol::family, for_each_table_entry, ICMP_ECHO_PROTOCOLS, and NULL.

Referenced by icmp_tx_echo_request().

                                                                             {
        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;
}
static uint32_t icmpcol ( struct sockaddr_tcpip st_peer) [static]

Determine debugging colour for ICMP debug messages.

Parameters:
st_peerPeer address
Return values:
colDebugging colour (for DBGC())

Definition at line 65 of file icmp.c.

References crc32_le().

Referenced by icmp_rx_echo_reply(), icmp_rx_echo_request(), icmp_tx_echo_reply(), and icmp_tx_echo_request().

                                                           {

        return crc32_le ( 0, st_peer, sizeof ( *st_peer ) );
}
static int icmp_tx_echo ( struct io_buffer iobuf,
struct sockaddr_tcpip st_dest,
struct icmp_echo_protocol echo_protocol 
) [static]

Transmit ICMP echo packet.

Parameters:
iobufI/O buffer
st_destDestination socket address
echo_protocolICMP echo protocol
Return values:
rcReturn status code

Definition at line 78 of file icmp.c.

References icmp_header::chksum, io_buffer::data, echo(), icmp_echo::icmp, iob_len(), icmp_echo_protocol::net_checksum, NULL, rc, tcpip_chksum(), icmp_echo_protocol::tcpip_protocol, and tcpip_tx().

Referenced by icmp_tx_echo_reply(), and icmp_tx_echo_request().

                                                                     {
        struct icmp_echo *echo = iobuf->data;
        int rc;

        /* Set ICMP type and (re)calculate checksum */
        echo->icmp.chksum = 0;
        echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );

        /* 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;
}
int icmp_tx_echo_request ( struct io_buffer iobuf,
struct sockaddr_tcpip st_dest 
)

Transmit ICMP echo request.

Parameters:
iobufI/O buffer
st_destDestination socket address
Return values:
rcReturn status code

Definition at line 105 of file icmp.c.

References io_buffer::data, DBGC, echo(), ENOTSUP, free_iob(), icmp_echo::icmp, icmp_echo_protocol(), icmp_tx_echo(), icmpcol(), icmp_echo::ident, ntohs, rc, icmp_echo_protocol::request, icmp_echo::sequence, sockaddr_tcpip::st_family, and icmp_header::type.

Referenced by ping_deliver().

                                                            {
        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;
        }

        /* 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;
}
static int icmp_tx_echo_reply ( struct io_buffer iobuf,
struct sockaddr_tcpip st_dest,
struct icmp_echo_protocol echo_protocol 
) [static]

Transmit ICMP echo reply.

Parameters:
iobufI/O buffer
st_destDestination socket address
Return values:
rcReturn status code

Definition at line 139 of file icmp.c.

References io_buffer::data, DBGC, echo(), icmp_echo::icmp, icmp_tx_echo(), icmpcol(), icmp_echo::ident, ntohs, rc, icmp_echo_protocol::reply, icmp_echo::sequence, and icmp_header::type.

Referenced by icmp_rx_echo_request().

                                                                           {
        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;
}
int icmp_rx_echo_request ( struct io_buffer iobuf,
struct sockaddr_tcpip st_src,
struct icmp_echo_protocol echo_protocol 
)

Process a received ICMP echo request.

Parameters:
iobufI/O buffer
st_srcSource socket address
echo_protocolICMP echo protocol
Return values:
rcReturn status code

Definition at line 165 of file icmp.c.

References io_buffer::data, DBGC, echo(), EINVAL, free_iob(), icmp_tx_echo_reply(), icmpcol(), icmp_echo::ident, iob_len(), ntohs, rc, and icmp_echo::sequence.

Referenced by icmpv4_rx(), and icmpv6_rx_echo_request().

                                                                      {
        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 ) );

        /* Transmit echo reply */
        if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
                return rc;

        return 0;
}
int icmp_rx_echo_reply ( struct io_buffer iobuf,
struct sockaddr_tcpip st_src 
)

Process a received ICMP echo request.

Parameters:
iobufI/O buffer
st_srcSource socket address
Return values:
rcReturn status code

Definition at line 196 of file icmp.c.

References io_buffer::data, DBGC, echo(), EINVAL, free_iob(), icmpcol(), icmp_echo::ident, iob_len(), ntohs, ping_rx(), rc, and icmp_echo::sequence.

Referenced by icmpv4_rx(), and icmpv6_rx_echo_reply().

                                                         {
        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 ) );

        /* Deliver to ping protocol */
        if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
                return rc;

        return 0;
}
__weak int ping_rx ( struct io_buffer iobuf,
struct sockaddr_tcpip *st_src  __unused 
)

Receive ping reply (when no ping protocol is present)

Parameters:
iobufI/O buffer
st_srcSource socket address
Return values:
rcReturn status code

Definition at line 226 of file icmp.c.

References free_iob().

Referenced by icmp_rx_echo_reply().

                                                              {
        free_iob ( iobuf );
        return 0;
}