iPXE
icmp.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 <string.h>
00027 #include <byteswap.h>
00028 #include <errno.h>
00029 #include <ipxe/iobuf.h>
00030 #include <ipxe/in.h>
00031 #include <ipxe/tcpip.h>
00032 #include <ipxe/ping.h>
00033 #include <ipxe/crc32.h>
00034 #include <ipxe/icmp.h>
00035 
00036 /** @file
00037  *
00038  * ICMP protocol
00039  *
00040  */
00041 
00042 /**
00043  * Identify ICMP echo protocol
00044  *
00045  * @v st_family         Address family
00046  * @ret echo_protocol   ICMP echo protocol, or NULL
00047  */
00048 static struct icmp_echo_protocol * icmp_echo_protocol ( sa_family_t family ) {
00049         struct icmp_echo_protocol *echo_protocol;
00050 
00051         for_each_table_entry ( echo_protocol, ICMP_ECHO_PROTOCOLS ) {
00052                 if ( echo_protocol->family == family )
00053                         return echo_protocol;
00054         }
00055         return NULL;
00056 }
00057 
00058 /**
00059  *
00060  * Determine debugging colour for ICMP debug messages
00061  *
00062  * @v st_peer           Peer address
00063  * @ret col             Debugging colour (for DBGC())
00064  */
00065 static uint32_t icmpcol ( struct sockaddr_tcpip *st_peer ) {
00066 
00067         return crc32_le ( 0, st_peer, sizeof ( *st_peer ) );
00068 }
00069 
00070 /**
00071  * Transmit ICMP echo packet
00072  *
00073  * @v iobuf             I/O buffer
00074  * @v st_dest           Destination socket address
00075  * @v echo_protocol     ICMP echo protocol
00076  * @ret rc              Return status code
00077  */
00078 static int icmp_tx_echo ( struct io_buffer *iobuf,
00079                           struct sockaddr_tcpip *st_dest,
00080                           struct icmp_echo_protocol *echo_protocol ) {
00081         struct icmp_echo *echo = iobuf->data;
00082         int rc;
00083 
00084         /* Set ICMP type and (re)calculate checksum */
00085         echo->icmp.chksum = 0;
00086         echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );
00087 
00088         /* Transmit packet */
00089         if ( ( rc = tcpip_tx ( iobuf, echo_protocol->tcpip_protocol, NULL,
00090                                st_dest, NULL,
00091                                ( echo_protocol->net_checksum ?
00092                                  &echo->icmp.chksum : NULL ) ) ) != 0 )
00093                 return rc;
00094 
00095         return 0;
00096 }
00097 
00098 /**
00099  * Transmit ICMP echo request
00100  *
00101  * @v iobuf             I/O buffer
00102  * @v st_dest           Destination socket address
00103  * @ret rc              Return status code
00104  */
00105 int icmp_tx_echo_request ( struct io_buffer *iobuf,
00106                            struct sockaddr_tcpip *st_dest ) {
00107         struct icmp_echo *echo = iobuf->data;
00108         struct icmp_echo_protocol *echo_protocol;
00109         int rc;
00110 
00111         /* Identify ICMP echo protocol */
00112         echo_protocol = icmp_echo_protocol ( st_dest->st_family );
00113         if ( ! echo_protocol ) {
00114                 DBGC ( icmpcol ( st_dest ), "ICMP TX echo request unknown "
00115                        "address family %d\n", st_dest->st_family );
00116                 free_iob ( iobuf );
00117                 return -ENOTSUP;
00118         }
00119 
00120         /* Set type */
00121         echo->icmp.type = echo_protocol->request;
00122 
00123         /* Transmit request */
00124         DBGC ( icmpcol ( st_dest ), "ICMP TX echo request id %04x seq %04x\n",
00125                ntohs ( echo->ident ), ntohs ( echo->sequence ) );
00126         if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
00127                 return rc;
00128 
00129         return 0;
00130 }
00131 
00132 /**
00133  * Transmit ICMP echo reply
00134  *
00135  * @v iobuf             I/O buffer
00136  * @v st_dest           Destination socket address
00137  * @ret rc              Return status code
00138  */
00139 static int icmp_tx_echo_reply ( struct io_buffer *iobuf,
00140                                 struct sockaddr_tcpip *st_dest,
00141                                 struct icmp_echo_protocol *echo_protocol ) {
00142         struct icmp_echo *echo = iobuf->data;
00143         int rc;
00144 
00145         /* Set type */
00146         echo->icmp.type = echo_protocol->reply;
00147 
00148         /* Transmit reply */
00149         DBGC ( icmpcol ( st_dest ), "ICMP TX echo reply id %04x seq %04x\n",
00150                ntohs ( echo->ident ), ntohs ( echo->sequence ) );
00151         if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
00152                 return rc;
00153 
00154         return 0;
00155 }
00156 
00157 /**
00158  * Process a received ICMP echo request
00159  *
00160  * @v iobuf             I/O buffer
00161  * @v st_src            Source socket address
00162  * @v echo_protocol     ICMP echo protocol
00163  * @ret rc              Return status code
00164  */
00165 int icmp_rx_echo_request ( struct io_buffer *iobuf,
00166                            struct sockaddr_tcpip *st_src,
00167                            struct icmp_echo_protocol *echo_protocol ) {
00168         struct icmp_echo *echo = iobuf->data;
00169         int rc;
00170 
00171         /* Sanity check */
00172         if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
00173                 DBGC ( icmpcol ( st_src ), "ICMP RX echo request too short at "
00174                        "%zd bytes (min %zd bytes)\n",
00175                        iob_len ( iobuf ), sizeof ( *echo ) );
00176                 free_iob ( iobuf );
00177                 return -EINVAL;
00178         }
00179         DBGC ( icmpcol ( st_src ), "ICMP RX echo request id %04x seq %04x\n",
00180                ntohs ( echo->ident ), ntohs ( echo->sequence ) );
00181 
00182         /* Transmit echo reply */
00183         if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
00184                 return rc;
00185 
00186         return 0;
00187 }
00188 
00189 /**
00190  * Process a received ICMP echo request
00191  *
00192  * @v iobuf             I/O buffer
00193  * @v st_src            Source socket address
00194  * @ret rc              Return status code
00195  */
00196 int icmp_rx_echo_reply ( struct io_buffer *iobuf,
00197                          struct sockaddr_tcpip *st_src ) {
00198         struct icmp_echo *echo = iobuf->data;
00199         int rc;
00200 
00201         /* Sanity check */
00202         if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
00203                 DBGC ( icmpcol ( st_src ), "ICMP RX echo reply too short at "
00204                        "%zd bytes (min %zd bytes)\n",
00205                        iob_len ( iobuf ), sizeof ( *echo ) );
00206                 free_iob ( iobuf );
00207                 return -EINVAL;
00208         }
00209         DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
00210                ntohs ( echo->ident ), ntohs ( echo->sequence ) );
00211 
00212         /* Deliver to ping protocol */
00213         if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
00214                 return rc;
00215 
00216         return 0;
00217 }
00218 
00219 /**
00220  * Receive ping reply (when no ping protocol is present)
00221  *
00222  * @v iobuf             I/O buffer
00223  * @v st_src            Source socket address
00224  * @ret rc              Return status code
00225  */
00226 __weak int ping_rx ( struct io_buffer *iobuf,
00227                      struct sockaddr_tcpip *st_src __unused ) {
00228         free_iob ( iobuf );
00229         return 0;
00230 }