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:322
#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:160
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:53
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:505
#define ENOTSUP
Operation not supported.
Definition errno.h:590
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 = iobuf->data;
144 int rc;
145
146 /* Set type */
147 echo->icmp.type = echo_protocol->reply;
148
149 /* Transmit reply */
150 DBGC ( icmpcol ( st_dest ), "ICMP TX echo reply id %04x seq %04x\n",
151 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
152 if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
153 return rc;
154
155 return 0;
156}
uint8_t reply
Reply type.
Definition icmp.h:48

References io_buffer::data, DBGC, echo(), icmp_tx_echo(), icmpcol(), 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 166 of file icmp.c.

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

198 {
199 struct icmp_echo *echo = iobuf->data;
200 int rc;
201
202 /* Sanity check */
203 if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
204 DBGC ( icmpcol ( st_src ), "ICMP RX echo reply too short at "
205 "%zd bytes (min %zd bytes)\n",
206 iob_len ( iobuf ), sizeof ( *echo ) );
207 free_iob ( iobuf );
208 return -EINVAL;
209 }
210 DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
211 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
212
213 /* Deliver to ping protocol */
214 if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
215 return rc;
216
217 return 0;
218}
__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:227

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

228 {
229 free_iob ( iobuf );
230 return 0;
231}

References __unused, __weak, and free_iob().

Referenced by icmp_rx_echo_reply().