iPXE
Data Structures | Functions | Variables
ping.c File Reference

ICMP ping protocol. More...

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/refcnt.h>
#include <ipxe/list.h>
#include <ipxe/iobuf.h>
#include <ipxe/tcpip.h>
#include <ipxe/icmp.h>
#include <ipxe/interface.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/netdevice.h>
#include <ipxe/ping.h>

Go to the source code of this file.

Data Structures

struct  ping_connection
 A ping connection. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FILE_SECBOOT (PERMITTED)
 
static LIST_HEAD (ping_conns)
 List of registered ping connections. More...
 
static struct ping_connectionping_demux (unsigned int port)
 Identify ping connection by local port number. More...
 
static int ping_port_available (int port)
 Check if local port number is available. More...
 
int ping_rx (struct io_buffer *iobuf, struct sockaddr_tcpip *st_src)
 Process ICMP ping reply. More...
 
static struct io_bufferping_alloc_iob (struct ping_connection *ping __unused, size_t len)
 Allocate I/O buffer for ping. More...
 
static int ping_deliver (struct ping_connection *ping, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Deliver datagram as I/O buffer. More...
 
static void ping_close (struct ping_connection *ping, int rc)
 Close ping connection. More...
 
static int ping_open (struct interface *xfer, struct sockaddr *peer, struct sockaddr *local)
 Open a ping connection. More...
 

Variables

static struct interface_operation ping_xfer_operations []
 Ping data transfer interface operations. More...
 
static struct interface_descriptor ping_xfer_desc
 Ping data transfer interface descriptor. More...
 
struct socket_opener ping_socket_opener __socket_opener
 Ping socket opener. More...
 
int ping_sock_echo = PING_SOCK_ECHO
 Linkage hack. More...
 

Detailed Description

ICMP ping protocol.

Definition in file ping.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ LIST_HEAD()

static LIST_HEAD ( ping_conns  )
static

List of registered ping connections.

◆ ping_demux()

static struct ping_connection* ping_demux ( unsigned int  port)
static

Identify ping connection by local port number.

Parameters
portLocal port number
Return values
pingPing connection, or NULL

Definition at line 76 of file ping.c.

76  {
77  struct ping_connection *ping;
78 
79  list_for_each_entry ( ping, &ping_conns, list ) {
80  if ( ping->port == port )
81  return ping;
82  }
83  return NULL;
84 }
struct list_head list
List of ping connections.
Definition: ping.c:56
A ping connection.
Definition: ping.c:52
u8 port
Port number.
Definition: CIB_PRM.h:31
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:432
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
int ping(const char *hostname, unsigned long timeout, size_t len, unsigned int count, int quiet)
Ping a host.
Definition: pingmgmt.c:70

References ping_connection::list, list_for_each_entry, NULL, ping(), and port.

Referenced by ping_port_available(), and ping_rx().

◆ ping_port_available()

static int ping_port_available ( int  port)
static

Check if local port number is available.

Parameters
portLocal port number
Return values
portLocal port number, or negative error

Definition at line 92 of file ping.c.

92  {
93 
94  return ( ping_demux ( port ) ? -EADDRINUSE : port );
95 }
#define EADDRINUSE
Address already in use.
Definition: errno.h:304
static struct ping_connection * ping_demux(unsigned int port)
Identify ping connection by local port number.
Definition: ping.c:76
u8 port
Port number.
Definition: CIB_PRM.h:31

References EADDRINUSE, ping_demux(), and port.

Referenced by ping_open().

◆ ping_rx()

int ping_rx ( struct io_buffer iobuf,
struct sockaddr_tcpip st_src 
)

Process ICMP ping reply.

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

Definition at line 104 of file ping.c.

104  {
105  struct icmp_echo *echo = iobuf->data;
106  struct ping_connection *ping;
107  struct xfer_metadata meta;
108  int rc;
109 
110  /* Sanity check: should already have been checked by ICMP layer */
111  assert ( iob_len ( iobuf ) >= sizeof ( *echo ) );
112 
113  /* Identify connection */
114  ping = ping_demux ( ntohs ( echo->ident ) );
115  DBGC ( ping, "PING %p reply id %#04x seq %#04x\n",
116  ping, ntohs ( echo->ident ), ntohs ( echo->sequence ) );
117  if ( ! ping ) {
118  rc = -ENOTCONN;
119  goto discard;
120  }
121 
122  /* Strip header, construct metadata, and pass data to upper layer */
123  iob_pull ( iobuf, sizeof ( *echo ) );
124  memset ( &meta, 0, sizeof ( meta ) );
125  meta.src = ( ( struct sockaddr * ) st_src );
126  meta.flags = XFER_FL_ABS_OFFSET;
127  meta.offset = ntohs ( echo->sequence );
128  return xfer_deliver ( &ping->xfer, iob_disown ( iobuf ), &meta );
129 
130  discard:
131  free_iob ( iobuf );
132  return rc;
133 }
#define iob_pull(iobuf, len)
Definition: iobuf.h:107
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Data transfer metadata.
Definition: xfer.h:23
An ICMP echo request/reply.
Definition: icmp.h:30
#define XFER_FL_ABS_OFFSET
Offset is absolute.
Definition: xfer.h:48
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:153
#define DBGC(...)
Definition: compiler.h:505
A ping connection.
Definition: ping.c:52
#define ntohs(value)
Definition: byteswap.h:137
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:217
static struct ping_connection * ping_demux(unsigned int port)
Identify ping connection by local port number.
Definition: ping.c:76
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define ENOTCONN
The socket is not connected.
Definition: errno.h:570
Generalized socket address structure.
Definition: socket.h:97
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:160
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:195
void * data
Start of data.
Definition: iobuf.h:53
uint8_t meta
Metadata flags.
Definition: ena.h:14
int ping(const char *hostname, unsigned long timeout, size_t len, unsigned int count, int quiet)
Ping a host.
Definition: pingmgmt.c:70
void * memset(void *dest, int character, size_t len) __nonnull
int echo(void)
Definition: kb.c:133

References assert(), io_buffer::data, DBGC, echo(), ENOTCONN, free_iob(), iob_disown, iob_len(), iob_pull, memset(), meta, ntohs, ping(), ping_demux(), rc, xfer_deliver(), and XFER_FL_ABS_OFFSET.

◆ ping_alloc_iob()

static struct io_buffer* ping_alloc_iob ( struct ping_connection *ping  __unused,
size_t  len 
)
static

Allocate I/O buffer for ping.

Parameters
pingPing connection
lenPayload size
Return values
iobufI/O buffer, or NULL

Definition at line 143 of file ping.c.

143  {
144  size_t header_len;
145  struct io_buffer *iobuf;
146 
147  header_len = ( MAX_LL_NET_HEADER_LEN + sizeof ( struct icmp_echo ) );
148  iobuf = alloc_iob ( header_len + len );
149  if ( iobuf )
150  iob_reserve ( iobuf, header_len );
151  return iobuf;
152 }
An ICMP echo request/reply.
Definition: icmp.h:30
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:131
#define MAX_LL_NET_HEADER_LEN
Maximum combined length of a link-layer and network-layer header.
Definition: netdevice.h:59
ring len
Length.
Definition: dwmac.h:231
#define iob_reserve(iobuf, len)
Definition: iobuf.h:72
A persistent I/O buffer.
Definition: iobuf.h:38

References alloc_iob(), iob_reserve, len, and MAX_LL_NET_HEADER_LEN.

◆ ping_deliver()

static int ping_deliver ( struct ping_connection ping,
struct io_buffer iobuf,
struct xfer_metadata meta 
)
static

Deliver datagram as I/O buffer.

Parameters
pingPing connection
iobufI/O buffer
metaData transfer metadata
Return values
rcReturn status code

Definition at line 162 of file ping.c.

163  {
164  struct icmp_echo *echo = iob_push ( iobuf, sizeof ( *echo ) );
165  int rc;
166 
167  /* Construct header */
168  memset ( echo, 0, sizeof ( *echo ) );
169  echo->ident = htons ( ping->port );
170  echo->sequence = htons ( meta->offset );
171 
172  /* Transmit echo request */
173  if ( ( rc = icmp_tx_echo_request ( iob_disown ( iobuf ),
174  &ping->peer ) ) != 0 ) {
175  DBGC ( ping, "PING %p could not transmit: %s\n",
176  ping, strerror ( rc ) );
177  return rc;
178  }
179 
180  return 0;
181 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int icmp_tx_echo_request(struct io_buffer *iobuf, struct sockaddr_tcpip *st_dest)
Transmit ICMP echo request.
Definition: icmp.c:106
An ICMP echo request/reply.
Definition: icmp.h:30
#define iob_push(iobuf, len)
Definition: iobuf.h:89
#define DBGC(...)
Definition: compiler.h:505
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:217
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
uint8_t meta
Metadata flags.
Definition: ena.h:14
#define htons(value)
Definition: byteswap.h:136
int ping(const char *hostname, unsigned long timeout, size_t len, unsigned int count, int quiet)
Ping a host.
Definition: pingmgmt.c:70
void * memset(void *dest, int character, size_t len) __nonnull
int echo(void)
Definition: kb.c:133

References DBGC, echo(), htons, icmp_tx_echo_request(), iob_disown, iob_push, memset(), meta, ping(), rc, and strerror().

◆ ping_close()

static void ping_close ( struct ping_connection ping,
int  rc 
)
static

Close ping connection.

Parameters
pingPing connection
rcReason for close

Definition at line 189 of file ping.c.

189  {
190 
191  /* Close data transfer interface */
192  intf_shutdown ( &ping->xfer, rc );
193 
194  /* Remove from list of connections and drop list's reference */
195  list_del ( &ping->list );
196  ref_put ( &ping->refcnt );
197 
198  DBGC ( ping, "PING %p closed\n", ping );
199 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:279
#define DBGC(...)
Definition: compiler.h:505
#define list_del(list)
Delete an entry from a list.
Definition: list.h:120
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107
int ping(const char *hostname, unsigned long timeout, size_t len, unsigned int count, int quiet)
Ping a host.
Definition: pingmgmt.c:70

References DBGC, intf_shutdown(), list_del, ping(), rc, and ref_put.

◆ ping_open()

static int ping_open ( struct interface xfer,
struct sockaddr peer,
struct sockaddr local 
)
static

Open a ping connection.

Parameters
xferData transfer interface
peerPeer socket address
localLocal socket address, or NULL
Return values
rcReturn status code

Definition at line 220 of file ping.c.

221  {
222  struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
223  struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
224  struct ping_connection *ping;
225  int port;
226  int rc;
227 
228  /* Allocate and initialise structure */
229  ping = zalloc ( sizeof ( *ping ) );
230  if ( ! ping ) {
231  rc = -ENOMEM;
232  goto err_alloc;
233  }
234  DBGC ( ping, "PING %p allocated\n", ping );
235  ref_init ( &ping->refcnt, NULL );
236  intf_init ( &ping->xfer, &ping_xfer_desc, &ping->refcnt );
237  memcpy ( &ping->peer, st_peer, sizeof ( ping->peer ) );
238 
239  /* Bind to local port */
240  port = tcpip_bind ( st_local, ping_port_available );
241  if ( port < 0 ) {
242  rc = port;
243  DBGC ( ping, "PING %p could not bind: %s\n",
244  ping, strerror ( rc ) );
245  goto err_bind;
246  }
247  ping->port = port;
248  DBGC ( ping, "PING %p bound to id %#04x\n", ping, port );
249 
250  /* Attach parent interface, transfer reference to connection
251  * list, and return
252  */
253  intf_plug_plug ( &ping->xfer, xfer );
254  list_add ( &ping->list, &ping_conns );
255  return 0;
256 
257  err_bind:
258  ref_put ( &ping->refcnt );
259  err_alloc:
260  return rc;
261 }
TCP/IP socket address.
Definition: tcpip.h:76
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static struct interface_descriptor ping_xfer_desc
Ping data transfer interface descriptor.
Definition: ping.c:209
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:70
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:65
#define DBGC(...)
Definition: compiler.h:505
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:108
A ping connection.
Definition: ping.c:52
int tcpip_bind(struct sockaddr_tcpip *st_local, int(*available)(int port))
Bind to local TCP/IP port.
Definition: tcpip.c:215
struct interface xfer
Data transfer interface.
Definition: ping.c:64
#define ENOMEM
Not enough space.
Definition: errno.h:535
void * memcpy(void *dest, const void *src, size_t len) __nonnull
u8 port
Port number.
Definition: CIB_PRM.h:31
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
static int ping_port_available(int port)
Check if local port number is available.
Definition: ping.c:92
struct mschapv2_challenge peer
Peer challenge.
Definition: mschapv2.h:12
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:204
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107
int ping(const char *hostname, unsigned long timeout, size_t len, unsigned int count, int quiet)
Ping a host.
Definition: pingmgmt.c:70

References DBGC, ENOMEM, intf_init(), intf_plug_plug(), list_add, memcpy(), NULL, peer, ping(), ping_port_available(), ping_xfer_desc, port, rc, ref_init, ref_put, strerror(), tcpip_bind(), ping_connection::xfer, and zalloc().

Variable Documentation

◆ ping_xfer_operations

struct interface_operation ping_xfer_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:250
A ping connection.
Definition: ping.c:52
struct io_buffer * xfer_alloc_iob(struct interface *intf, size_t len)
Allocate I/O buffer.
Definition: xfer.c:159
static int ping_deliver(struct ping_connection *ping, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram as I/O buffer.
Definition: ping.c:162
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:195
static struct io_buffer * ping_alloc_iob(struct ping_connection *ping __unused, size_t len)
Allocate I/O buffer for ping.
Definition: ping.c:143
static void ping_close(struct ping_connection *ping, int rc)
Close ping connection.
Definition: ping.c:189

Ping data transfer interface operations.

Definition at line 202 of file ping.c.

◆ ping_xfer_desc

struct interface_descriptor ping_xfer_desc
static
Initial value:
=
A ping connection.
Definition: ping.c:52
static struct interface_operation ping_xfer_operations[]
Ping data transfer interface operations.
Definition: ping.c:202
struct interface xfer
Data transfer interface.
Definition: ping.c:64
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:81

Ping data transfer interface descriptor.

Definition at line 209 of file ping.c.

Referenced by ping_open().

◆ __socket_opener

struct socket_opener ping_socket_opener __socket_opener
Initial value:
= {
.semantics = PING_SOCK_ECHO,
.open = ping_open,
}
static int ping_open(struct interface *xfer, struct sockaddr *peer, struct sockaddr *local)
Open a ping connection.
Definition: ping.c:220
#define PING_SOCK_ECHO
Definition: socket.h:34

Ping socket opener.

Definition at line 264 of file ping.c.