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. More...
 
static uint32_t icmpcol (struct sockaddr_tcpip *st_peer)
 Determine debugging colour for ICMP debug messages. More...
 
static int icmp_tx_echo (struct io_buffer *iobuf, struct sockaddr_tcpip *st_dest, struct icmp_echo_protocol *echo_protocol)
 Transmit ICMP echo packet. More...
 
int icmp_tx_echo_request (struct io_buffer *iobuf, struct sockaddr_tcpip *st_dest)
 Transmit ICMP echo request. More...
 
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. More...
 
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. More...
 
int icmp_rx_echo_reply (struct io_buffer *iobuf, struct sockaddr_tcpip *st_src)
 Process a received ICMP echo request. More...
 
__weak int ping_rx (struct io_buffer *iobuf, struct sockaddr_tcpip *st_src __unused)
 Receive ping reply (when no ping protocol is present) More...
 

Detailed Description

ICMP protocol.

Definition in file icmp.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ icmp_echo_protocol()

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

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

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

Referenced by icmp_tx_echo_request().

◆ icmpcol()

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.

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

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()

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.

80  {
81  struct icmp_echo *echo = iobuf->data;
82  int rc;
83 
84  /* Set ICMP type and (re)calculate checksum */
85  echo->icmp.chksum = 0;
86  echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );
87 
88  /* Transmit packet */
89  if ( ( rc = tcpip_tx ( iobuf, echo_protocol->tcpip_protocol, NULL,
90  st_dest, NULL,
91  ( echo_protocol->net_checksum ?
92  &echo->icmp.chksum : NULL ) ) ) != 0 )
93  return rc;
94 
95  return 0;
96 }
int net_checksum
Include network-layer checksum within packet.
Definition: icmp.h:51
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An ICMP echo request/reply.
Definition: icmp.h:29
struct tcpip_protocol * tcpip_protocol
TCP/IP protocol.
Definition: icmp.h:49
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
void * data
Start of data.
Definition: iobuf.h:44
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:91
uint16_t tcpip_chksum(const void *data, size_t len)
Calculate TCP/IP checkum.
Definition: tcpip.c:203
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
int echo(void)
Definition: kb.c:133

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

106  {
107  struct icmp_echo *echo = iobuf->data;
108  struct icmp_echo_protocol *echo_protocol;
109  int rc;
110 
111  /* Identify ICMP echo protocol */
112  echo_protocol = icmp_echo_protocol ( st_dest->st_family );
113  if ( ! echo_protocol ) {
114  DBGC ( icmpcol ( st_dest ), "ICMP TX echo request unknown "
115  "address family %d\n", st_dest->st_family );
116  free_iob ( iobuf );
117  return -ENOTSUP;
118  }
119 
120  /* Set type */
121  echo->icmp.type = echo_protocol->request;
122 
123  /* Transmit request */
124  DBGC ( icmpcol ( st_dest ), "ICMP TX echo request id %04x seq %04x\n",
125  ntohs ( echo->ident ), ntohs ( echo->sequence ) );
126  if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
127  return rc;
128 
129  return 0;
130 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An ICMP echo request/reply.
Definition: icmp.h:29
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
sa_family_t st_family
Socket address family (part of struct sockaddr)
Definition: tcpip.h:77
#define DBGC(...)
Definition: compiler.h:505
#define ntohs(value)
Definition: byteswap.h:136
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
static struct icmp_echo_protocol * icmp_echo_protocol(sa_family_t family)
Identify ICMP echo protocol.
Definition: icmp.c:48
uint8_t request
Request type.
Definition: icmp.h:45
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:78
static uint32_t icmpcol(struct sockaddr_tcpip *st_peer)
Determine debugging colour for ICMP debug messages.
Definition: icmp.c:65
An ICMP echo protocol.
Definition: icmp.h:41
void * data
Start of data.
Definition: iobuf.h:44
int echo(void)
Definition: kb.c:133

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()

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.

141  {
142  struct icmp_echo *echo = iobuf->data;
143  int rc;
144 
145  /* Set type */
146  echo->icmp.type = echo_protocol->reply;
147 
148  /* Transmit reply */
149  DBGC ( icmpcol ( st_dest ), "ICMP TX echo reply id %04x seq %04x\n",
150  ntohs ( echo->ident ), ntohs ( echo->sequence ) );
151  if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
152  return rc;
153 
154  return 0;
155 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An ICMP echo request/reply.
Definition: icmp.h:29
#define DBGC(...)
Definition: compiler.h:505
#define ntohs(value)
Definition: byteswap.h:136
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:78
static uint32_t icmpcol(struct sockaddr_tcpip *st_peer)
Determine debugging colour for ICMP debug messages.
Definition: icmp.c:65
void * data
Start of data.
Definition: iobuf.h:44
uint8_t reply
Reply type.
Definition: icmp.h:47
int echo(void)
Definition: kb.c:133

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

167  {
168  struct icmp_echo *echo = iobuf->data;
169  int rc;
170 
171  /* Sanity check */
172  if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
173  DBGC ( icmpcol ( st_src ), "ICMP RX echo request too short at "
174  "%zd bytes (min %zd bytes)\n",
175  iob_len ( iobuf ), sizeof ( *echo ) );
176  free_iob ( iobuf );
177  return -EINVAL;
178  }
179  DBGC ( icmpcol ( st_src ), "ICMP RX echo request id %04x seq %04x\n",
180  ntohs ( echo->ident ), ntohs ( echo->sequence ) );
181 
182  /* Transmit echo reply */
183  if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
184  return rc;
185 
186  return 0;
187 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An ICMP echo request/reply.
Definition: icmp.h:29
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
#define DBGC(...)
Definition: compiler.h:505
#define ntohs(value)
Definition: byteswap.h:136
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:139
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
static uint32_t icmpcol(struct sockaddr_tcpip *st_peer)
Determine debugging colour for ICMP debug messages.
Definition: icmp.c:65
void * data
Start of data.
Definition: iobuf.h:44
int echo(void)
Definition: kb.c:133

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

197  {
198  struct icmp_echo *echo = iobuf->data;
199  int rc;
200 
201  /* Sanity check */
202  if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
203  DBGC ( icmpcol ( st_src ), "ICMP RX echo reply too short at "
204  "%zd bytes (min %zd bytes)\n",
205  iob_len ( iobuf ), sizeof ( *echo ) );
206  free_iob ( iobuf );
207  return -EINVAL;
208  }
209  DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
210  ntohs ( echo->ident ), ntohs ( echo->sequence ) );
211 
212  /* Deliver to ping protocol */
213  if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
214  return rc;
215 
216  return 0;
217 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An ICMP echo request/reply.
Definition: icmp.h:29
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
#define DBGC(...)
Definition: compiler.h:505
#define ntohs(value)
Definition: byteswap.h:136
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
static uint32_t icmpcol(struct sockaddr_tcpip *st_peer)
Determine debugging colour for ICMP debug messages.
Definition: icmp.c:65
void * data
Start of data.
Definition: iobuf.h:44
__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:226
int echo(void)
Definition: kb.c:133

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

227  {
228  free_iob ( iobuf );
229  return 0;
230 }
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145

References free_iob().

Referenced by icmp_rx_echo_reply().