iPXE
icmpv4.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <string.h>
28#include <errno.h>
29#include <ipxe/iobuf.h>
30#include <ipxe/in.h>
31#include <ipxe/tcpip.h>
32#include <ipxe/icmp.h>
33
34/** @file
35 *
36 * ICMPv4 protocol
37 *
38 */
39
40struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol;
41
42/**
43 * Process a received packet
44 *
45 * @v iobuf I/O buffer
46 * @v netdev Network device
47 * @v st_src Partially-filled source address
48 * @v st_dest Partially-filled destination address
49 * @v pshdr_csum Pseudo-header checksum
50 * @ret rc Return status code
51 */
52static int icmpv4_rx ( struct io_buffer *iobuf,
54 struct sockaddr_tcpip *st_src,
55 struct sockaddr_tcpip *st_dest __unused,
56 uint16_t pshdr_csum __unused ) {
57 struct icmp_header *icmp;
58 size_t len = iob_len ( iobuf );
59 unsigned int csum;
60 unsigned int type;
61 int rc;
62
63 /* Sanity check */
64 if ( len < sizeof ( *icmp ) ) {
65 DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
66 len, sizeof ( *icmp ) );
67 rc = -EINVAL;
68 goto discard;
69 }
70 icmp = iobuf->data;
71
72 /* Verify checksum */
73 csum = tcpip_chksum ( icmp, len );
74 if ( csum != 0 ) {
75 DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
76 csum );
77 DBG_HD ( icmp, len );
78 rc = -EINVAL;
79 goto discard;
80 }
81
82 /* Handle ICMP packet */
83 type = icmp->type;
84 switch ( type ) {
86 return icmp_rx_echo_request ( iobuf, st_src,
87 &icmpv4_echo_protocol );
88 case ICMP_ECHO_REPLY:
89 return icmp_rx_echo_reply ( iobuf, st_src );
90 default:
91 DBG ( "ICMP ignoring type %d\n", type );
92 rc = 0;
93 break;
94 }
95
96 discard:
97 free_iob ( iobuf );
98 return rc;
99}
100
101/** ICMPv4 TCP/IP protocol */
102struct tcpip_protocol icmpv4_protocol __tcpip_protocol = {
103 .name = "ICMPv4",
104 .rx = icmpv4_rx,
105 .tcpip_proto = IP_ICMP,
106};
107
108/** ICMPv4 echo protocol */
109struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol = {
110 .family = AF_INET,
111 .request = ICMP_ECHO_REQUEST,
112 .reply = ICMP_ECHO_REPLY,
113 .tcpip_protocol = &icmpv4_protocol,
114 .net_checksum = 0,
115};
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned short uint16_t
Definition stdint.h:11
ring len
Length.
Definition dwmac.h:226
uint32_t type
Operating system type.
Definition ena.h:1
Error codes.
static struct net_device * netdev
Definition gdbudp.c:53
#define AF_INET
IPv4 Internet addresses.
Definition socket.h:64
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:598
#define DBG_HD(...)
Definition compiler.h:525
#define DBG(...)
Print a debugging message.
Definition compiler.h:523
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define EINVAL
Invalid argument.
Definition errno.h:472
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
int icmp_rx_echo_request(struct io_buffer *iobuf, struct sockaddr_tcpip *st_src, struct icmp_echo_protocol *echo_protocol)
Process a received ICMP echo request.
Definition icmp.c:170
int icmp_rx_echo_reply(struct io_buffer *iobuf, struct sockaddr_tcpip *st_src)
Process a received ICMP echo request.
Definition icmp.c:202
ICMP protocol.
#define ICMP_ECHO_REPLY
Definition icmp.h:62
#define ICMP_ECHO_REQUEST
Definition icmp.h:63
#define __icmp_echo_protocol
Declare an ICMP echo protocol.
Definition icmp.h:60
static int icmpv4_rx(struct io_buffer *iobuf, struct net_device *netdev __unused, struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest __unused, uint16_t pshdr_csum __unused)
Process a received packet.
Definition icmpv4.c:52
#define IP_ICMP
Definition in.h:13
Transport-network layer interface.
#define __tcpip_protocol
Declare a TCP/IP transport-layer protocol.
Definition tcpip.h:182
String functions.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition iobuf.c:153
I/O buffers.
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:220
An ICMP echo protocol.
Definition icmp.h:42
An ICMP header.
Definition icmp.h:20
uint8_t type
Type.
Definition icmp.h:22
A persistent I/O buffer.
Definition iobuf.h:98
void * data
Start of data.
Definition iobuf.h:113
A network device.
Definition netdevice.h:353
TCP/IP socket address.
Definition tcpip.h:76
A transport-layer protocol of the TCP/IP stack (eg.
Definition tcpip.h:105
uint16_t tcpip_chksum(const void *data, size_t len)
Calculate TCP/IP checkum.
Definition tcpip.c:204