2 * Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
23 #include <gpxe/iobuf.h>
25 #include <gpxe/tcpip.h>
26 #include <gpxe/icmp.h>
34 struct tcpip_protocol icmp_protocol __tcpip_protocol
;
37 * Process a received packet
40 * @v st_src Partially-filled source address
41 * @v st_dest Partially-filled destination address
42 * @v pshdr_csum Pseudo-header checksum
43 * @ret rc Return status code
45 static int icmp_rx ( struct io_buffer
*iobuf
, struct sockaddr_tcpip
*st_src
,
46 struct sockaddr_tcpip
*st_dest
,
47 uint16_t pshdr_csum __unused
) {
48 struct icmp_header
*icmp
= iobuf
->data
;
49 size_t len
= iob_len ( iobuf
);
54 if ( len
< sizeof ( *icmp
) ) {
55 DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
56 len
, sizeof ( *icmp
) );
62 csum
= tcpip_chksum ( icmp
, len
);
64 DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
71 /* We respond only to pings */
72 if ( icmp
->type
!= ICMP_ECHO_REQUEST
) {
73 DBG ( "ICMP ignoring type %d\n", icmp
->type
);
78 DBG ( "ICMP responding to ping\n" );
80 /* Change type to response and recalculate checksum */
81 icmp
->type
= ICMP_ECHO_RESPONSE
;
83 icmp
->chksum
= tcpip_chksum ( icmp
, len
);
85 /* Transmit the response */
86 if ( ( rc
= tcpip_tx ( iob_disown ( iobuf
), &icmp_protocol
, st_dest
,
87 st_src
, NULL
, NULL
) ) != 0 ) {
88 DBG ( "ICMP could not transmit ping response: %s\n",
98 /** ICMP TCP/IP protocol */
99 struct tcpip_protocol icmp_protocol __tcpip_protocol
= {
102 .tcpip_proto
= IP_ICMP
,