2
* Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; either version 2 of the
7
* License, or any later version.
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
* You can also choose to distribute this program under the terms of
20
* the Unmodified Binary Distribution Licence (as given in the file
21
* COPYING.UBDL), provided that you have satisfied its requirements.
24
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
30
#include <ipxe/refcnt.h>
31
#include <ipxe/list.h>
32
#include <ipxe/iobuf.h>
33
#include <ipxe/tcpip.h>
34
#include <ipxe/icmp.h>
35
#include <ipxe/interface.h>
36
#include <ipxe/xfer.h>
37
#include <ipxe/open.h>
38
#include <ipxe/netdevice.h>
39
#include <ipxe/ping.h>
51
struct ping_connection {
52
/** Reference counter */
54
/** List of ping connections */
55
struct list_head list;
57
/** Remote socket address */
58
struct sockaddr_tcpip peer;
59
/** Local port number */
62
/** Data transfer interface */
63
struct interface xfer;
66
/** List of registered ping connections */
67
static LIST_HEAD ( ping_conns );
70
* Identify ping connection by local port number
72
* @v port Local port number
73
* @ret ping Ping connection, or NULL
75
static struct ping_connection * ping_demux ( unsigned int port ) {
76
struct ping_connection *ping;
78
list_for_each_entry ( ping, &ping_conns, list ) {
79
if ( ping->port == port )
86
* Check if local port number is available
88
* @v port Local port number
89
* @ret port Local port number, or negative error
91
static int ping_port_available ( int port ) {
93
return ( ping_demux ( port ) ? -EADDRINUSE : port );
97
* Process ICMP ping reply
100
* @v st_src Source address
101
* @ret rc Return status code
103
int ping_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src ) {
104
struct icmp_echo *echo = iobuf->data;
105
struct ping_connection *ping;
106
struct xfer_metadata meta;
109
/* Sanity check: should already have been checked by ICMP layer */
110
assert ( iob_len ( iobuf ) >= sizeof ( *echo ) );
112
/* Identify connection */
113
ping = ping_demux ( ntohs ( echo->ident ) );
114
DBGC ( ping, "PING %p reply id %#04x seq %#04x\n",
115
ping, ntohs ( echo->ident ), ntohs ( echo->sequence ) );
121
/* Strip header, construct metadata, and pass data to upper layer */
122
iob_pull ( iobuf, sizeof ( *echo ) );
123
memset ( &meta, 0, sizeof ( meta ) );
124
meta.src = ( ( struct sockaddr * ) st_src );
125
meta.flags = XFER_FL_ABS_OFFSET;
126
meta.offset = ntohs ( echo->sequence );
127
return xfer_deliver ( &ping->xfer, iob_disown ( iobuf ), &meta );
135
* Allocate I/O buffer for ping
137
* @v ping Ping connection
138
* @v len Payload size
139
* @ret iobuf I/O buffer, or NULL
141
static struct io_buffer *
142
ping_alloc_iob ( struct ping_connection *ping __unused, size_t len ) {
144
struct io_buffer *iobuf;
146
header_len = ( MAX_LL_NET_HEADER_LEN + sizeof ( struct icmp_echo ) );
147
iobuf = alloc_iob ( header_len + len );
149
iob_reserve ( iobuf, header_len );
154
* Deliver datagram as I/O buffer
156
* @v ping Ping connection
157
* @v iobuf I/O buffer
158
* @v meta Data transfer metadata
159
* @ret rc Return status code
161
static int ping_deliver ( struct ping_connection *ping, struct io_buffer *iobuf,
162
struct xfer_metadata *meta ) {
163
struct icmp_echo *echo = iob_push ( iobuf, sizeof ( *echo ) );
166
/* Construct header */
167
memset ( echo, 0, sizeof ( *echo ) );
168
echo->ident = htons ( ping->port );
169
echo->sequence = htons ( meta->offset );
171
/* Transmit echo request */
172
if ( ( rc = icmp_tx_echo_request ( iob_disown ( iobuf ),
173
&ping->peer ) ) != 0 ) {
174
DBGC ( ping, "PING %p could not transmit: %s\n",
175
ping, strerror ( rc ) );
183
* Close ping connection
185
* @v ping Ping connection
186
* @v rc Reason for close
188
static void ping_close ( struct ping_connection *ping, int rc ) {
190
/* Close data transfer interface */
191
intf_shutdown ( &ping->xfer, rc );
193
/* Remove from list of connections and drop list's reference */
194
list_del ( &ping->list );
195
ref_put ( &ping->refcnt );
197
DBGC ( ping, "PING %p closed\n", ping );
200
/** Ping data transfer interface operations */
201
static struct interface_operation ping_xfer_operations[] = {
202
INTF_OP ( xfer_deliver, struct ping_connection *, ping_deliver ),
203
INTF_OP ( xfer_alloc_iob, struct ping_connection *, ping_alloc_iob ),
204
INTF_OP ( intf_close, struct ping_connection *, ping_close ),
207
/** Ping data transfer interface descriptor */
208
static struct interface_descriptor ping_xfer_desc =
209
INTF_DESC ( struct ping_connection, xfer, ping_xfer_operations );
212
* Open a ping connection
214
* @v xfer Data transfer interface
215
* @v peer Peer socket address
216
* @v local Local socket address, or NULL
217
* @ret rc Return status code
219
static int ping_open ( struct interface *xfer, struct sockaddr *peer,
220
struct sockaddr *local ) {
221
struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
222
struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
223
struct ping_connection *ping;
227
/* Allocate and initialise structure */
228
ping = zalloc ( sizeof ( *ping ) );
233
DBGC ( ping, "PING %p allocated\n", ping );
234
ref_init ( &ping->refcnt, NULL );
235
intf_init ( &ping->xfer, &ping_xfer_desc, &ping->refcnt );
236
memcpy ( &ping->peer, st_peer, sizeof ( ping->peer ) );
238
/* Bind to local port */
239
port = tcpip_bind ( st_local, ping_port_available );
242
DBGC ( ping, "PING %p could not bind: %s\n",
243
ping, strerror ( rc ) );
247
DBGC ( ping, "PING %p bound to id %#04x\n", ping, port );
249
/* Attach parent interface, transfer reference to connection
252
intf_plug_plug ( &ping->xfer, xfer );
253
list_add ( &ping->list, &ping_conns );
257
ref_put ( &ping->refcnt );
262
/** Ping IPv4 socket opener */
263
struct socket_opener ping_ipv4_socket_opener __socket_opener = {
264
.semantics = PING_SOCK_ECHO,
269
/** Ping IPv6 socket opener */
270
struct socket_opener ping_ipv6_socket_opener __socket_opener = {
271
.semantics = PING_SOCK_ECHO,
277
int ping_sock_echo = PING_SOCK_ECHO;