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
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/refcnt.h>
27 #include <ipxe/list.h>
28 #include <ipxe/iobuf.h>
29 #include <ipxe/tcpip.h>
30 #include <ipxe/icmp.h>
31 #include <ipxe/interface.h>
32 #include <ipxe/xfer.h>
33 #include <ipxe/open.h>
34 #include <ipxe/netdevice.h>
35 #include <ipxe/ping.h>
47 struct ping_connection {
48 /** Reference counter */
50 /** List of ping connections */
51 struct list_head list;
53 /** Remote socket address */
54 struct sockaddr_tcpip peer;
55 /** Local port number */
58 /** Data transfer interface */
59 struct interface xfer;
62 /** List of registered ping connections */
63 static LIST_HEAD ( ping_conns );
66 * Identify ping connection by local port number
68 * @v port Local port number
69 * @ret ping Ping connection, or NULL
71 static struct ping_connection * ping_demux ( unsigned int port ) {
72 struct ping_connection *ping;
74 list_for_each_entry ( ping, &ping_conns, list ) {
75 if ( ping->port == port )
82 * Check if local port number is available
84 * @v port Local port number
85 * @ret port Local port number, or negative error
87 static int ping_port_available ( int port ) {
89 return ( ping_demux ( port ) ? -EADDRINUSE : port );
93 * Process ICMP ping reply
96 * @v st_src Source address
97 * @ret rc Return status code
99 int ping_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src ) {
100 struct icmp_echo *echo = iobuf->data;
101 struct ping_connection *ping;
102 struct xfer_metadata meta;
105 /* Sanity check: should already have been checked by ICMP layer */
106 assert ( iob_len ( iobuf ) >= sizeof ( *echo ) );
108 /* Identify connection */
109 ping = ping_demux ( ntohs ( echo->ident ) );
110 DBGC ( ping, "PING %p reply id %#04x seq %#04x\n",
111 ping, ntohs ( echo->ident ), ntohs ( echo->sequence ) );
117 /* Strip header, construct metadata, and pass data to upper layer */
118 iob_pull ( iobuf, sizeof ( *echo ) );
119 memset ( &meta, 0, sizeof ( meta ) );
120 meta.src = ( ( struct sockaddr * ) st_src );
121 meta.flags = XFER_FL_ABS_OFFSET;
122 meta.offset = ntohs ( echo->sequence );
123 return xfer_deliver ( &ping->xfer, iob_disown ( iobuf ), &meta );
131 * Allocate I/O buffer for ping
133 * @v ping Ping connection
134 * @v len Payload size
135 * @ret iobuf I/O buffer, or NULL
137 static struct io_buffer *
138 ping_alloc_iob ( struct ping_connection *ping __unused, size_t len ) {
140 struct io_buffer *iobuf;
142 header_len = ( MAX_LL_NET_HEADER_LEN + sizeof ( struct icmp_echo ) );
143 iobuf = alloc_iob ( header_len + len );
145 iob_reserve ( iobuf, header_len );
150 * Deliver datagram as I/O buffer
152 * @v ping Ping connection
153 * @v iobuf I/O buffer
154 * @v meta Data transfer metadata
155 * @ret rc Return status code
157 static int ping_deliver ( struct ping_connection *ping, struct io_buffer *iobuf,
158 struct xfer_metadata *meta ) {
159 struct icmp_echo *echo = iob_push ( iobuf, sizeof ( *echo ) );
162 /* Construct header */
163 memset ( echo, 0, sizeof ( *echo ) );
164 echo->ident = htons ( ping->port );
165 echo->sequence = htons ( meta->offset );
167 /* Transmit echo request */
168 if ( ( rc = icmp_tx_echo_request ( iob_disown ( iobuf ),
169 &ping->peer ) ) != 0 ) {
170 DBGC ( ping, "PING %p could not transmit: %s\n",
171 ping, strerror ( rc ) );
179 * Close ping connection
181 * @v ping Ping connection
182 * @v rc Reason for close
184 static void ping_close ( struct ping_connection *ping, int rc ) {
186 /* Close data transfer interface */
187 intf_shutdown ( &ping->xfer, rc );
189 /* Remove from list of connections and drop list's reference */
190 list_del ( &ping->list );
191 ref_put ( &ping->refcnt );
193 DBGC ( ping, "PING %p closed\n", ping );
196 /** Ping data transfer interface operations */
197 static struct interface_operation ping_xfer_operations[] = {
198 INTF_OP ( xfer_deliver, struct ping_connection *, ping_deliver ),
199 INTF_OP ( xfer_alloc_iob, struct ping_connection *, ping_alloc_iob ),
200 INTF_OP ( intf_close, struct ping_connection *, ping_close ),
203 /** Ping data transfer interface descriptor */
204 static struct interface_descriptor ping_xfer_desc =
205 INTF_DESC ( struct ping_connection, xfer, ping_xfer_operations );
208 * Open a ping connection
210 * @v xfer Data transfer interface
211 * @v peer Peer socket address
212 * @v local Local socket address, or NULL
213 * @ret rc Return status code
215 static int ping_open ( struct interface *xfer, struct sockaddr *peer,
216 struct sockaddr *local ) {
217 struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
218 struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
219 struct ping_connection *ping;
223 /* Allocate and initialise structure */
224 ping = zalloc ( sizeof ( *ping ) );
229 DBGC ( ping, "PING %p allocated\n", ping );
230 ref_init ( &ping->refcnt, NULL );
231 intf_init ( &ping->xfer, &ping_xfer_desc, &ping->refcnt );
232 memcpy ( &ping->peer, st_peer, sizeof ( ping->peer ) );
234 /* Bind to local port */
235 port = tcpip_bind ( st_local, ping_port_available );
238 DBGC ( ping, "PING %p could not bind: %s\n",
239 ping, strerror ( rc ) );
243 DBGC ( ping, "PING %p bound to id %#04x\n", ping, port );
245 /* Attach parent interface, transfer reference to connection
248 intf_plug_plug ( &ping->xfer, xfer );
249 list_add ( &ping->list, &ping_conns );
253 ref_put ( &ping->refcnt );
258 /** Ping IPv4 socket opener */
259 struct socket_opener ping_ipv4_socket_opener __socket_opener = {
260 .semantics = PING_SOCK_ECHO,
265 /** Ping IPv6 socket opener */
266 struct socket_opener ping_ipv6_socket_opener __socket_opener = {
267 .semantics = PING_SOCK_ECHO,
273 int ping_sock_echo = PING_SOCK_ECHO;