iPXE
ping.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <byteswap.h>
00030 #include <ipxe/refcnt.h>
00031 #include <ipxe/list.h>
00032 #include <ipxe/iobuf.h>
00033 #include <ipxe/tcpip.h>
00034 #include <ipxe/icmp.h>
00035 #include <ipxe/interface.h>
00036 #include <ipxe/xfer.h>
00037 #include <ipxe/open.h>
00038 #include <ipxe/netdevice.h>
00039 #include <ipxe/ping.h>
00040 
00041 /** @file
00042  *
00043  * ICMP ping protocol
00044  *
00045  */
00046 
00047 /**
00048  * A ping connection
00049  *
00050  */
00051 struct ping_connection {
00052         /** Reference counter */
00053         struct refcnt refcnt;
00054         /** List of ping connections */
00055         struct list_head list;
00056 
00057         /** Remote socket address */
00058         struct sockaddr_tcpip peer;
00059         /** Local port number */
00060         uint16_t port;
00061 
00062         /** Data transfer interface */
00063         struct interface xfer;
00064 };
00065 
00066 /** List of registered ping connections */
00067 static LIST_HEAD ( ping_conns );
00068 
00069 /**
00070  * Identify ping connection by local port number
00071  *
00072  * @v port              Local port number
00073  * @ret ping            Ping connection, or NULL
00074  */
00075 static struct ping_connection * ping_demux ( unsigned int port ) {
00076         struct ping_connection *ping;
00077 
00078         list_for_each_entry ( ping, &ping_conns, list ) {
00079                 if ( ping->port == port )
00080                         return ping;
00081         }
00082         return NULL;
00083 }
00084 
00085 /**
00086  * Check if local port number is available
00087  *
00088  * @v port              Local port number
00089  * @ret port            Local port number, or negative error
00090  */
00091 static int ping_port_available ( int port ) {
00092 
00093         return ( ping_demux ( port ) ? -EADDRINUSE : port );
00094 }
00095 
00096 /**
00097  * Process ICMP ping reply
00098  *
00099  * @v iobuf             I/O buffer
00100  * @v st_src            Source address
00101  * @ret rc              Return status code
00102  */
00103 int ping_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src ) {
00104         struct icmp_echo *echo = iobuf->data;
00105         struct ping_connection *ping;
00106         struct xfer_metadata meta;
00107         int rc;
00108 
00109         /* Sanity check: should already have been checked by ICMP layer */
00110         assert ( iob_len ( iobuf ) >= sizeof ( *echo ) );
00111 
00112         /* Identify connection */
00113         ping = ping_demux ( ntohs ( echo->ident ) );
00114         DBGC ( ping, "PING %p reply id %#04x seq %#04x\n",
00115                ping, ntohs ( echo->ident ), ntohs ( echo->sequence ) );
00116         if ( ! ping ) {
00117                 rc = -ENOTCONN;
00118                 goto discard;
00119         }
00120 
00121         /* Strip header, construct metadata, and pass data to upper layer */
00122         iob_pull ( iobuf, sizeof ( *echo ) );
00123         memset ( &meta, 0, sizeof ( meta ) );
00124         meta.src = ( ( struct sockaddr * ) st_src );
00125         meta.flags = XFER_FL_ABS_OFFSET;
00126         meta.offset = ntohs ( echo->sequence );
00127         return xfer_deliver ( &ping->xfer, iob_disown ( iobuf ), &meta );
00128 
00129  discard:
00130         free_iob ( iobuf );
00131         return rc;
00132 }
00133 
00134 /**
00135  * Allocate I/O buffer for ping
00136  *
00137  * @v ping              Ping connection
00138  * @v len               Payload size
00139  * @ret iobuf           I/O buffer, or NULL
00140  */
00141 static struct io_buffer *
00142 ping_alloc_iob ( struct ping_connection *ping __unused, size_t len ) {
00143         size_t header_len;
00144         struct io_buffer *iobuf;
00145 
00146         header_len = ( MAX_LL_NET_HEADER_LEN + sizeof ( struct icmp_echo ) );
00147         iobuf = alloc_iob ( header_len + len );
00148         if ( iobuf )
00149                 iob_reserve ( iobuf, header_len );
00150         return iobuf;
00151 }
00152 
00153 /**
00154  * Deliver datagram as I/O buffer
00155  *
00156  * @v ping              Ping connection
00157  * @v iobuf             I/O buffer
00158  * @v meta              Data transfer metadata
00159  * @ret rc              Return status code
00160  */
00161 static int ping_deliver ( struct ping_connection *ping, struct io_buffer *iobuf,
00162                           struct xfer_metadata *meta ) {
00163         struct icmp_echo *echo = iob_push ( iobuf, sizeof ( *echo ) );
00164         int rc;
00165 
00166         /* Construct header */
00167         memset ( echo, 0, sizeof ( *echo ) );
00168         echo->ident = htons ( ping->port );
00169         echo->sequence = htons ( meta->offset );
00170 
00171         /* Transmit echo request */
00172         if ( ( rc = icmp_tx_echo_request ( iob_disown ( iobuf ),
00173                                            &ping->peer ) ) != 0 ) {
00174                 DBGC ( ping, "PING %p could not transmit: %s\n",
00175                        ping, strerror ( rc ) );
00176                 return rc;
00177         }
00178 
00179         return 0;
00180 }
00181 
00182 /**
00183  * Close ping connection
00184  *
00185  * @v ping              Ping connection
00186  * @v rc                Reason for close
00187  */
00188 static void ping_close ( struct ping_connection *ping, int rc ) {
00189 
00190         /* Close data transfer interface */
00191         intf_shutdown ( &ping->xfer, rc );
00192 
00193         /* Remove from list of connections and drop list's reference */
00194         list_del ( &ping->list );
00195         ref_put ( &ping->refcnt );
00196 
00197         DBGC ( ping, "PING %p closed\n", ping );
00198 }
00199 
00200 /** Ping data transfer interface operations */
00201 static struct interface_operation ping_xfer_operations[] = {
00202         INTF_OP ( xfer_deliver, struct ping_connection *, ping_deliver ),
00203         INTF_OP ( xfer_alloc_iob, struct ping_connection *, ping_alloc_iob ),
00204         INTF_OP ( intf_close, struct ping_connection *, ping_close ),
00205 };
00206 
00207 /** Ping data transfer interface descriptor */
00208 static struct interface_descriptor ping_xfer_desc =
00209         INTF_DESC ( struct ping_connection, xfer, ping_xfer_operations );
00210 
00211 /**
00212  * Open a ping connection
00213  *
00214  * @v xfer              Data transfer interface
00215  * @v peer              Peer socket address
00216  * @v local             Local socket address, or NULL
00217  * @ret rc              Return status code
00218  */
00219 static int ping_open ( struct interface *xfer, struct sockaddr *peer,
00220                        struct sockaddr *local ) {
00221         struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
00222         struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
00223         struct ping_connection *ping;
00224         int port;
00225         int rc;
00226 
00227         /* Allocate and initialise structure */
00228         ping = zalloc ( sizeof ( *ping ) );
00229         if ( ! ping ) {
00230                 rc = -ENOMEM;
00231                 goto err_alloc;
00232         }
00233         DBGC ( ping, "PING %p allocated\n", ping );
00234         ref_init ( &ping->refcnt, NULL );
00235         intf_init ( &ping->xfer, &ping_xfer_desc, &ping->refcnt );
00236         memcpy ( &ping->peer, st_peer, sizeof ( ping->peer ) );
00237 
00238         /* Bind to local port */
00239         port = tcpip_bind ( st_local, ping_port_available );
00240         if ( port < 0 ) {
00241                 rc = port;
00242                 DBGC ( ping, "PING %p could not bind: %s\n",
00243                        ping, strerror ( rc ) );
00244                 goto err_bind;
00245         }
00246         ping->port = port;
00247         DBGC ( ping, "PING %p bound to id %#04x\n", ping, port );
00248 
00249         /* Attach parent interface, transfer reference to connection
00250          * list, and return
00251          */
00252         intf_plug_plug ( &ping->xfer, xfer );
00253         list_add ( &ping->list, &ping_conns );
00254         return 0;
00255 
00256  err_bind:
00257         ref_put ( &ping->refcnt );
00258  err_alloc:
00259         return rc;
00260 }
00261 
00262 /** Ping IPv4 socket opener */
00263 struct socket_opener ping_ipv4_socket_opener __socket_opener = {
00264         .semantics      = PING_SOCK_ECHO,
00265         .family         = AF_INET,
00266         .open           = ping_open,
00267 };
00268 
00269 /** Ping IPv6 socket opener */
00270 struct socket_opener ping_ipv6_socket_opener __socket_opener = {
00271         .semantics      = PING_SOCK_ECHO,
00272         .family         = AF_INET6,
00273         .open           = ping_open,
00274 };
00275 
00276 /** Linkage hack */
00277 int ping_sock_echo = PING_SOCK_ECHO;