Przeglądaj źródła

[icmp] Add support for responding to pings

tags/v0.9.7
Michael Brown 15 lat temu
rodzic
commit
06b5132fdc
4 zmienionych plików z 128 dodań i 0 usunięć
  1. 1
    0
      src/include/gpxe/errfile.h
  2. 23
    0
      src/include/gpxe/icmp.h
  3. 101
    0
      src/net/icmp.c
  4. 3
    0
      src/net/ipv4.c

+ 1
- 0
src/include/gpxe/errfile.h Wyświetl plik

@@ -139,6 +139,7 @@
139 139
 #define ERRFILE_slam			( ERRFILE_NET | 0x00160000 )
140 140
 #define ERRFILE_ib_sma			( ERRFILE_NET | 0x00170000 )
141 141
 #define ERRFILE_ib_packet		( ERRFILE_NET | 0x00180000 )
142
+#define ERRFILE_icmp			( ERRFILE_NET | 0x00190000 )
142 143
 
143 144
 #define ERRFILE_image		      ( ERRFILE_IMAGE | 0x00000000 )
144 145
 #define ERRFILE_elf		      ( ERRFILE_IMAGE | 0x00010000 )

+ 23
- 0
src/include/gpxe/icmp.h Wyświetl plik

@@ -0,0 +1,23 @@
1
+#ifndef _GPXE_ICMP_H
2
+#define _GPXE_ICMP_H
3
+
4
+/** @file
5
+ *
6
+ * ICMP protocol
7
+ *
8
+ */
9
+
10
+/** An ICMP header */
11
+struct icmp_header {
12
+	/** Type */
13
+	uint8_t type;
14
+	/** Code */
15
+	uint8_t code;
16
+	/** Checksum */
17
+	uint16_t chksum;
18
+} __attribute__ (( packed ));
19
+
20
+#define ICMP_ECHO_RESPONSE 0
21
+#define ICMP_ECHO_REQUEST 8
22
+
23
+#endif /* _GPXE_ICMP_H */

+ 101
- 0
src/net/icmp.c Wyświetl plik

@@ -0,0 +1,101 @@
1
+/*
2
+ * Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <string.h>
20
+#include <errno.h>
21
+#include <gpxe/iobuf.h>
22
+#include <gpxe/in.h>
23
+#include <gpxe/tcpip.h>
24
+#include <gpxe/icmp.h>
25
+
26
+/** @file
27
+ *
28
+ * ICMP protocol
29
+ *
30
+ */
31
+
32
+struct tcpip_protocol icmp_protocol __tcpip_protocol;
33
+
34
+/**
35
+ * Process a received packet
36
+ *
37
+ * @v iobuf		I/O buffer
38
+ * @v st_src		Partially-filled source address
39
+ * @v st_dest		Partially-filled destination address
40
+ * @v pshdr_csum	Pseudo-header checksum
41
+ * @ret rc		Return status code
42
+ */
43
+static int icmp_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src,
44
+		     struct sockaddr_tcpip *st_dest,
45
+		     uint16_t pshdr_csum __unused ) {
46
+	struct icmp_header *icmp = iobuf->data;
47
+	size_t len = iob_len ( iobuf );
48
+	unsigned int csum;
49
+	int rc;
50
+
51
+	/* Sanity check */
52
+	if ( len < sizeof ( *icmp ) ) {
53
+		DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
54
+		      len, sizeof ( *icmp ) );
55
+		rc = -EINVAL;
56
+		goto done;
57
+	}
58
+
59
+	/* Verify checksum */
60
+	csum = tcpip_chksum ( icmp, len );
61
+	if ( csum != 0 ) {
62
+		DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
63
+		      csum );
64
+		DBG_HD ( icmp, len );
65
+		rc = -EINVAL;
66
+		goto done;
67
+	}
68
+
69
+	/* We respond only to pings */
70
+	if ( icmp->type != ICMP_ECHO_REQUEST ) {
71
+		DBG ( "ICMP ignoring type %d\n", icmp->type );
72
+		rc = 0;
73
+		goto done;
74
+	}
75
+
76
+	DBG ( "ICMP responding to ping\n" );
77
+
78
+	/* Change type to response and recalculate checksum */
79
+	icmp->type = ICMP_ECHO_RESPONSE;
80
+	icmp->chksum = 0;
81
+	icmp->chksum = tcpip_chksum ( icmp, len );
82
+
83
+	/* Transmit the response */
84
+	if ( ( rc = tcpip_tx ( iob_disown ( iobuf ), &icmp_protocol, st_dest,
85
+			       st_src, NULL, NULL ) ) != 0 ) {
86
+		DBG ( "ICMP could not transmit ping response: %s\n",
87
+		      strerror ( rc ) );
88
+		goto done;
89
+	}
90
+
91
+ done:
92
+	free_iob ( iobuf );
93
+	return rc;
94
+}
95
+
96
+/** ICMP TCP/IP protocol */
97
+struct tcpip_protocol icmp_protocol __tcpip_protocol = {
98
+	.name = "ICMP",
99
+	.rx = icmp_rx,
100
+	.tcpip_proto = IP_ICMP,
101
+};

+ 3
- 0
src/net/ipv4.c Wyświetl plik

@@ -627,3 +627,6 @@ static int ipv4_create_routes ( void ) {
627 627
 struct settings_applicator ipv4_settings_applicator __settings_applicator = {
628 628
 	.apply = ipv4_create_routes,
629 629
 };
630
+
631
+/* Drag in ICMP */
632
+REQUIRE_OBJECT ( icmp );

Ładowanie…
Anuluj
Zapisz