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 );
25 #include <ipxe/iobuf.h>
27 #include <ipxe/tcpip.h>
28 #include <ipxe/ping.h>
29 #include <ipxe/crc32.h>
30 #include <ipxe/icmp.h>
39 * Identify ICMP echo protocol
41 * @v st_family Address family
42 * @ret echo_protocol ICMP echo protocol, or NULL
44 static struct icmp_echo_protocol * icmp_echo_protocol ( sa_family_t family ) {
45 struct icmp_echo_protocol *echo_protocol;
47 for_each_table_entry ( echo_protocol, ICMP_ECHO_PROTOCOLS ) {
48 if ( echo_protocol->family == family )
56 * Determine debugging colour for ICMP debug messages
58 * @v st_peer Peer address
59 * @ret col Debugging colour (for DBGC())
61 static uint32_t icmpcol ( struct sockaddr_tcpip *st_peer ) {
63 return crc32_le ( 0, st_peer, sizeof ( *st_peer ) );
67 * Transmit ICMP echo packet
70 * @v st_dest Destination socket address
71 * @v echo_protocol ICMP echo protocol
72 * @ret rc Return status code
74 static int icmp_tx_echo ( struct io_buffer *iobuf,
75 struct sockaddr_tcpip *st_dest,
76 struct icmp_echo_protocol *echo_protocol ) {
77 struct icmp_echo *echo = iobuf->data;
80 /* Set ICMP type and (re)calculate checksum */
81 echo->icmp.chksum = 0;
82 echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );
85 if ( ( rc = tcpip_tx ( iobuf, echo_protocol->tcpip_protocol, NULL,
87 ( echo_protocol->net_checksum ?
88 &echo->icmp.chksum : NULL ) ) ) != 0 )
95 * Transmit ICMP echo request
98 * @v st_dest Destination socket address
99 * @ret rc Return status code
101 int icmp_tx_echo_request ( struct io_buffer *iobuf,
102 struct sockaddr_tcpip *st_dest ) {
103 struct icmp_echo *echo = iobuf->data;
104 struct icmp_echo_protocol *echo_protocol;
107 /* Identify ICMP echo protocol */
108 echo_protocol = icmp_echo_protocol ( st_dest->st_family );
109 if ( ! echo_protocol ) {
110 DBGC ( icmpcol ( st_dest ), "ICMP TX echo request unknown "
111 "address family %d\n", st_dest->st_family );
117 echo->icmp.type = echo_protocol->request;
119 /* Transmit request */
120 DBGC ( icmpcol ( st_dest ), "ICMP TX echo request id %04x seq %04x\n",
121 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
122 if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
129 * Transmit ICMP echo reply
131 * @v iobuf I/O buffer
132 * @v st_dest Destination socket address
133 * @ret rc Return status code
135 static int icmp_tx_echo_reply ( struct io_buffer *iobuf,
136 struct sockaddr_tcpip *st_dest,
137 struct icmp_echo_protocol *echo_protocol ) {
138 struct icmp_echo *echo = iobuf->data;
142 echo->icmp.type = echo_protocol->reply;
145 DBGC ( icmpcol ( st_dest ), "ICMP TX echo reply id %04x seq %04x\n",
146 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
147 if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
154 * Process a received ICMP echo request
156 * @v iobuf I/O buffer
157 * @v st_src Source socket address
158 * @v echo_protocol ICMP echo protocol
159 * @ret rc Return status code
161 int icmp_rx_echo_request ( struct io_buffer *iobuf,
162 struct sockaddr_tcpip *st_src,
163 struct icmp_echo_protocol *echo_protocol ) {
164 struct icmp_echo *echo = iobuf->data;
168 if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
169 DBGC ( icmpcol ( st_src ), "ICMP RX echo request too short at "
170 "%zd bytes (min %zd bytes)\n",
171 iob_len ( iobuf ), sizeof ( *echo ) );
175 DBGC ( icmpcol ( st_src ), "ICMP RX echo request id %04x seq %04x\n",
176 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
178 /* Transmit echo reply */
179 if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
186 * Process a received ICMP echo request
188 * @v iobuf I/O buffer
189 * @v st_src Source socket address
190 * @ret rc Return status code
192 int icmp_rx_echo_reply ( struct io_buffer *iobuf,
193 struct sockaddr_tcpip *st_src ) {
194 struct icmp_echo *echo = iobuf->data;
198 if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
199 DBGC ( icmpcol ( st_src ), "ICMP RX echo reply too short at "
200 "%zd bytes (min %zd bytes)\n",
201 iob_len ( iobuf ), sizeof ( *echo ) );
205 DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
206 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
208 /* Deliver to ping protocol */
209 if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
216 * Receive ping reply (when no ping protocol is present)
218 * @v iobuf I/O buffer
219 * @v st_src Source socket address
220 * @ret rc Return status code
222 __weak int ping_rx ( struct io_buffer *iobuf,
223 struct sockaddr_tcpip *st_src __unused ) {