iPXE
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)
 FILE_SECBOOT (PERMITTED)
static struct icmp_echo_protocol * icmp_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()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ icmp_echo_protocol()

struct icmp_echo_protocol * icmp_echo_protocol ( sa_family_t family)
static

Identify ICMP echo protocol.

Parameters
st_familyAddress family
Return values
echo_protocolICMP echo protocol, or NULL

Definition at line 49 of file icmp.c.

49 {
50 struct icmp_echo_protocol *echo_protocol;
51
52 for_each_table_entry ( echo_protocol, ICMP_ECHO_PROTOCOLS ) {
53 if ( echo_protocol->family == family )
54 return echo_protocol;
55 }
56 return NULL;
57}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
#define ICMP_ECHO_PROTOCOLS
ICMP echo protocol table.
Definition icmp.h:56
An ICMP echo protocol.
Definition icmp.h:42
sa_family_t family
Address family.
Definition icmp.h:44
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition tables.h:386

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

Referenced by icmp_tx_echo_request().

◆ icmpcol()

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 66 of file icmp.c.

66 {
67
68 return crc32_le ( 0, st_peer, sizeof ( *st_peer ) );
69}
u32 crc32_le(u32 seed, const void *data, size_t len)
Calculate 32-bit little-endian CRC checksum.
Definition crc32.c:40

References crc32_le().

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

◆ icmp_tx_echo()

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 79 of file icmp.c.

81 {
82 struct icmp_echo *echo = iobuf->data;
83 int rc;
84
85 /* Set ICMP type and (re)calculate checksum */
86 echo->icmp.chksum = 0;
87 echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );
88
89 /* Transmit packet */
90 if ( ( rc = tcpip_tx ( iobuf, echo_protocol->tcpip_protocol, NULL,
91 st_dest, NULL,
92 ( echo_protocol->net_checksum ?
93 &echo->icmp.chksum : NULL ) ) ) != 0 )
94 return rc;
95
96 return 0;
97}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:220
int echo(void)
Definition kb.c:133
int net_checksum
Include network-layer checksum within packet.
Definition icmp.h:52
struct tcpip_protocol * tcpip_protocol
TCP/IP protocol.
Definition icmp.h:50
An ICMP echo request/reply.
Definition icmp.h:30
void * data
Start of data.
Definition iobuf.h:113
int tcpip_tx(struct io_buffer *iobuf, struct tcpip_protocol *tcpip_protocol, struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest, struct net_device *netdev, uint16_t *trans_csum)
Transmit a TCP/IP packet.
Definition tcpip.c:92
uint16_t tcpip_chksum(const void *data, size_t len)
Calculate TCP/IP checkum.
Definition tcpip.c:204

References io_buffer::data, echo(), 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().

◆ icmp_tx_echo_request()

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 106 of file icmp.c.

107 {
108 struct icmp_echo *echo = iobuf->data;
109 struct icmp_echo_protocol *echo_protocol;
110 int rc;
111
112 /* Identify ICMP echo protocol */
113 echo_protocol = icmp_echo_protocol ( st_dest->st_family );
114 if ( ! echo_protocol ) {
115 DBGC ( icmpcol ( st_dest ), "ICMP TX echo request unknown "
116 "address family %d\n", st_dest->st_family );
117 free_iob ( iobuf );
118 return -ENOTSUP;
119 }
120
121 /* Set type */
122 echo->icmp.type = echo_protocol->request;
123
124 /* Transmit request */
125 DBGC ( icmpcol ( st_dest ), "ICMP TX echo request id %04x seq %04x\n",
126 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
127 if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
128 return rc;
129
130 return 0;
131}
#define DBGC(...)
Definition compiler.h:530
#define ENOTSUP
Operation not supported.
Definition errno.h:633
static struct icmp_echo_protocol * icmp_echo_protocol(sa_family_t family)
Identify ICMP echo protocol.
Definition icmp.c:49
static int icmp_tx_echo(struct io_buffer *iobuf, struct sockaddr_tcpip *st_dest, struct icmp_echo_protocol *echo_protocol)
Transmit ICMP echo packet.
Definition icmp.c:79
static uint32_t icmpcol(struct sockaddr_tcpip *st_peer)
Determine debugging colour for ICMP debug messages.
Definition icmp.c:66
#define ntohs(value)
Definition byteswap.h:137
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition iobuf.c:153
uint8_t request
Request type.
Definition icmp.h:46
sa_family_t st_family
Socket address family (part of struct sockaddr).
Definition tcpip.h:78

References io_buffer::data, DBGC, echo(), ENOTSUP, free_iob(), icmp_echo_protocol(), icmp_tx_echo(), icmpcol(), ntohs, rc, icmp_echo_protocol::request, and sockaddr_tcpip::st_family.

Referenced by ping_deliver().

◆ icmp_tx_echo_reply()

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 140 of file icmp.c.

142 {
143 struct icmp_echo *echo;
144 int rc;
145
146 /* Sanity check: should have already been checked by receiver */
147 assert ( iob_len ( iobuf ) >= sizeof ( *echo ) );
148 echo = iobuf->data;
149
150 /* Set type */
151 echo->icmp.type = echo_protocol->reply;
152
153 /* Transmit reply */
154 DBGC ( icmpcol ( st_dest ), "ICMP TX echo reply id %04x seq %04x\n",
155 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
156 if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
157 return rc;
158
159 return 0;
160}
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
uint8_t reply
Reply type.
Definition icmp.h:48

References assert, io_buffer::data, DBGC, echo(), icmp_tx_echo(), icmpcol(), iob_len(), ntohs, rc, and icmp_echo_protocol::reply.

Referenced by icmp_rx_echo_request().

◆ icmp_rx_echo_request()

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 170 of file icmp.c.

172 {
173 struct icmp_echo *echo;
174 int rc;
175
176 /* Sanity check */
177 if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
178 DBGC ( icmpcol ( st_src ), "ICMP RX echo request too short at "
179 "%zd bytes (min %zd bytes)\n",
180 iob_len ( iobuf ), sizeof ( *echo ) );
181 free_iob ( iobuf );
182 return -EINVAL;
183 }
184 echo = iobuf->data;
185 DBGC ( icmpcol ( st_src ), "ICMP RX echo request id %04x seq %04x\n",
186 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
187
188 /* Transmit echo reply */
189 if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
190 return rc;
191
192 return 0;
193}
#define EINVAL
Invalid argument.
Definition errno.h:472
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.
Definition icmp.c:140

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

Referenced by icmpv4_rx(), and icmpv6_rx_echo_request().

◆ icmp_rx_echo_reply()

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 202 of file icmp.c.

203 {
204 struct icmp_echo *echo;
205 int rc;
206
207 /* Sanity check */
208 if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
209 DBGC ( icmpcol ( st_src ), "ICMP RX echo reply too short at "
210 "%zd bytes (min %zd bytes)\n",
211 iob_len ( iobuf ), sizeof ( *echo ) );
212 free_iob ( iobuf );
213 return -EINVAL;
214 }
215 echo = iobuf->data;
216 DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
217 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
218
219 /* Deliver to ping protocol */
220 if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
221 return rc;
222
223 return 0;
224}
__weak int ping_rx(struct io_buffer *iobuf, struct sockaddr_tcpip *st_src __unused)
Receive ping reply (when no ping protocol is present).
Definition icmp.c:233

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

Referenced by icmpv4_rx(), and icmpv6_rx_echo_reply().

◆ ping_rx()

__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 233 of file icmp.c.

234 {
235 free_iob ( iobuf );
236 return 0;
237}

References __unused, __weak, and free_iob().

Referenced by icmp_rx_echo_reply().