Преглед изворни кода

[icmp] Add support for sending ICMP echo requests

Merge common functionality between IPv4 and IPv6 ICMP echo handling,
and add support for transmitting ICMP echo requests and delivering
ICMP echo replies to a (not yet implemented) ping_rx() function.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown пре 10 година
родитељ
комит
5c2ffc26cc
9 измењених фајлова са 402 додато и 121 уклоњено
  1. 2
    1
      src/include/ipxe/errfile.h
  2. 49
    1
      src/include/ipxe/icmp.h
  3. 1
    22
      src/include/ipxe/icmpv6.h
  4. 1
    1
      src/include/ipxe/ndp.h
  5. 18
    0
      src/include/ipxe/ping.h
  6. 174
    55
      src/net/icmp.c
  7. 109
    0
      src/net/icmpv4.c
  8. 46
    39
      src/net/icmpv6.c
  9. 2
    2
      src/net/ipv4.c

+ 2
- 1
src/include/ipxe/errfile.h Прегледај датотеку

@@ -184,7 +184,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
184 184
 #define ERRFILE_slam			( ERRFILE_NET | 0x00160000 )
185 185
 #define ERRFILE_ib_sma			( ERRFILE_NET | 0x00170000 )
186 186
 #define ERRFILE_ib_packet		( ERRFILE_NET | 0x00180000 )
187
-#define ERRFILE_icmp			( ERRFILE_NET | 0x00190000 )
187
+#define ERRFILE_icmpv4			( ERRFILE_NET | 0x00190000 )
188 188
 #define ERRFILE_ib_qset			( ERRFILE_NET | 0x001a0000 )
189 189
 #define ERRFILE_ib_gma			( ERRFILE_NET | 0x001b0000 )
190 190
 #define ERRFILE_ib_pathrec		( ERRFILE_NET | 0x001c0000 )
@@ -216,6 +216,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
216 216
 #define ERRFILE_oncrpc_iob		( ERRFILE_NET | 0x00360000 )
217 217
 #define ERRFILE_neighbour		( ERRFILE_NET | 0x00370000 )
218 218
 #define ERRFILE_socket			( ERRFILE_NET | 0x00380000 )
219
+#define ERRFILE_icmp			( ERRFILE_NET | 0x00390000 )
219 220
 
220 221
 #define ERRFILE_image		      ( ERRFILE_IMAGE | 0x00000000 )
221 222
 #define ERRFILE_elf		      ( ERRFILE_IMAGE | 0x00010000 )

+ 49
- 1
src/include/ipxe/icmp.h Прегледај датотеку

@@ -9,6 +9,12 @@
9 9
 
10 10
 FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12
+#include <stdint.h>
13
+#include <ipxe/iobuf.h>
14
+#include <ipxe/socket.h>
15
+#include <ipxe/tcpip.h>
16
+#include <ipxe/tables.h>
17
+
12 18
 /** An ICMP header */
13 19
 struct icmp_header {
14 20
 	/** Type */
@@ -19,7 +25,49 @@ struct icmp_header {
19 25
 	uint16_t chksum;
20 26
 } __attribute__ (( packed ));
21 27
 
22
-#define ICMP_ECHO_RESPONSE 0
28
+/** An ICMP echo request/reply */
29
+struct icmp_echo {
30
+	/** ICMPv6 header */
31
+	struct icmp_header icmp;
32
+	/** Identifier */
33
+	uint16_t ident;
34
+	/** Sequence number */
35
+	uint16_t sequence;
36
+	/** Data */
37
+	uint8_t data[0];
38
+} __attribute__ (( packed ));
39
+
40
+/** An ICMP echo protocol */
41
+struct icmp_echo_protocol {
42
+	/** Address family */
43
+	sa_family_t family;
44
+	/** Request type */
45
+	uint8_t request;
46
+	/** Reply type */
47
+	uint8_t reply;
48
+	/** TCP/IP protocol */
49
+	struct tcpip_protocol *tcpip_protocol;
50
+	/** Include network-layer checksum within packet */
51
+	int net_checksum;
52
+};
53
+
54
+/** ICMP echo protocol table */
55
+#define ICMP_ECHO_PROTOCOLS \
56
+	__table ( struct icmp_echo_protocol, "icmp_echo_protocols" )
57
+
58
+/** Declare an ICMP echo protocol */
59
+#define __icmp_echo_protocol __table_entry ( ICMP_ECHO_PROTOCOLS, 01 )
60
+
61
+#define ICMP_ECHO_REPLY 0
23 62
 #define ICMP_ECHO_REQUEST 8
24 63
 
64
+extern int icmp_tx_echo_request ( struct io_buffer *iobuf,
65
+				  struct sockaddr_tcpip *st_dest );
66
+
67
+extern int icmp_rx_echo_request ( struct io_buffer *iobuf,
68
+				  struct sockaddr_tcpip *st_src,
69
+				  struct icmp_echo_protocol *echo_protocol );
70
+extern int icmp_rx_echo_reply ( struct io_buffer *iobuf,
71
+				struct sockaddr_tcpip *st_src );
72
+
25 73
 #endif /* _IPXE_ICMP_H */

+ 1
- 22
src/include/ipxe/icmpv6.h Прегледај датотеку

@@ -13,28 +13,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
13 13
 #include <ipxe/tables.h>
14 14
 #include <ipxe/iobuf.h>
15 15
 #include <ipxe/netdevice.h>
16
-
17
-/** An ICMPv6 header */
18
-struct icmpv6_header {
19
-	/** Type */
20
-	uint8_t type;
21
-	/** Code */
22
-	uint8_t code;
23
-	/** Checksum */
24
-	uint16_t chksum;
25
-} __attribute__ (( packed ));
26
-
27
-/** An ICMPv6 echo request/reply */
28
-struct icmpv6_echo {
29
-	/** ICMPv6 header */
30
-	struct icmpv6_header icmp;
31
-	/** Identifier */
32
-	uint16_t ident;
33
-	/** Sequence number */
34
-	uint16_t sequence;
35
-	/** Data */
36
-	uint8_t data[0];
37
-} __attribute__ (( packed ));
16
+#include <ipxe/icmp.h>
38 17
 
39 18
 /** An ICMPv6 handler */
40 19
 struct icmpv6_handler {

+ 1
- 1
src/include/ipxe/ndp.h Прегледај датотеку

@@ -31,7 +31,7 @@ struct ndp_option {
31 31
 /** An NDP header */
32 32
 struct ndp_header {
33 33
 	/** ICMPv6 header */
34
-	struct icmpv6_header icmp;
34
+	struct icmp_header icmp;
35 35
 	/** Flags */
36 36
 	uint8_t flags;
37 37
 	/** Reserved */

+ 18
- 0
src/include/ipxe/ping.h Прегледај датотеку

@@ -0,0 +1,18 @@
1
+#ifndef _IPXE_PING_H
2
+#define _IPXE_PING_H
3
+
4
+/** @file
5
+ *
6
+ * ICMP ping protocol
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER );
11
+
12
+#include <ipxe/iobuf.h>
13
+#include <ipxe/tcpip.h>
14
+
15
+extern int ping_rx ( struct io_buffer *iobuf,
16
+		     struct sockaddr_tcpip *st_src );
17
+
18
+#endif /* _IPXE_PING_H */

+ 174
- 55
src/net/icmp.c Прегледај датотеку

@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
2
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3 3
  *
4 4
  * This program is free software; you can redistribute it and/or
5 5
  * modify it under the terms of the GNU General Public License as
@@ -20,10 +20,13 @@
20 20
 FILE_LICENCE ( GPL2_OR_LATER );
21 21
 
22 22
 #include <string.h>
23
+#include <byteswap.h>
23 24
 #include <errno.h>
24 25
 #include <ipxe/iobuf.h>
25 26
 #include <ipxe/in.h>
26 27
 #include <ipxe/tcpip.h>
28
+#include <ipxe/ping.h>
29
+#include <ipxe/crc32.h>
27 30
 #include <ipxe/icmp.h>
28 31
 
29 32
 /** @file
@@ -32,76 +35,192 @@ FILE_LICENCE ( GPL2_OR_LATER );
32 35
  *
33 36
  */
34 37
 
35
-struct tcpip_protocol icmp_protocol __tcpip_protocol;
38
+/**
39
+ * Identify ICMP echo protocol
40
+ *
41
+ * @v st_family		Address family
42
+ * @ret echo_protocol	ICMP echo protocol, or NULL
43
+ */
44
+static struct icmp_echo_protocol * icmp_echo_protocol ( sa_family_t family ) {
45
+	struct icmp_echo_protocol *echo_protocol;
46
+
47
+	for_each_table_entry ( echo_protocol, ICMP_ECHO_PROTOCOLS ) {
48
+		if ( echo_protocol->family == family )
49
+			return echo_protocol;
50
+	}
51
+	return NULL;
52
+}
36 53
 
37 54
 /**
38
- * Process a received packet
55
+ *
56
+ * Determine debugging colour for ICMP debug messages
57
+ *
58
+ * @v st_peer		Peer address
59
+ * @ret col		Debugging colour (for DBGC())
60
+ */
61
+static uint32_t icmpcol ( struct sockaddr_tcpip *st_peer ) {
62
+
63
+	return crc32_le ( 0, st_peer, sizeof ( *st_peer ) );
64
+}
65
+
66
+/**
67
+ * Transmit ICMP echo packet
39 68
  *
40 69
  * @v iobuf		I/O buffer
41
- * @v netdev		Network device
42
- * @v st_src		Partially-filled source address
43
- * @v st_dest		Partially-filled destination address
44
- * @v pshdr_csum	Pseudo-header checksum
70
+ * @v st_dest		Destination socket address
71
+ * @v echo_protocol	ICMP echo protocol
45 72
  * @ret rc		Return status code
46 73
  */
47
-static int icmp_rx ( struct io_buffer *iobuf,
48
-		     struct net_device *netdev __unused,
49
-		     struct sockaddr_tcpip *st_src,
50
-		     struct sockaddr_tcpip *st_dest,
51
-		     uint16_t pshdr_csum __unused ) {
52
-	struct icmp_header *icmp = iobuf->data;
53
-	size_t len = iob_len ( iobuf );
54
-	unsigned int csum;
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;
55 78
 	int rc;
56 79
 
57
-	/* Sanity check */
58
-	if ( len < sizeof ( *icmp ) ) {
59
-		DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
60
-		      len, sizeof ( *icmp ) );
61
-		rc = -EINVAL;
62
-		goto done;
63
-	}
80
+	/* Set ICMP type and (re)calculate checksum */
81
+	echo->icmp.chksum = 0;
82
+	echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );
64 83
 
65
-	/* Verify checksum */
66
-	csum = tcpip_chksum ( icmp, len );
67
-	if ( csum != 0 ) {
68
-		DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
69
-		      csum );
70
-		DBG_HD ( icmp, len );
71
-		rc = -EINVAL;
72
-		goto done;
84
+	/* Transmit packet */
85
+	if ( ( rc = tcpip_tx ( iobuf, echo_protocol->tcpip_protocol, NULL,
86
+			       st_dest, NULL,
87
+			       ( echo_protocol->net_checksum ?
88
+				 &echo->icmp.chksum : NULL ) ) ) != 0 )
89
+		return rc;
90
+
91
+	return 0;
92
+}
93
+
94
+/**
95
+ * Transmit ICMP echo request
96
+ *
97
+ * @v iobuf		I/O buffer
98
+ * @v st_dest		Destination socket address
99
+ * @ret rc		Return status code
100
+ */
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;
105
+	int rc;
106
+
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 );
112
+		free_iob ( iobuf );
113
+		return -ENOTSUP;
73 114
 	}
74 115
 
75
-	/* We respond only to pings */
76
-	if ( icmp->type != ICMP_ECHO_REQUEST ) {
77
-		DBG ( "ICMP ignoring type %d\n", icmp->type );
78
-		rc = 0;
79
-		goto done;
116
+	/* Set type */
117
+	echo->icmp.type = echo_protocol->request;
118
+
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 )
123
+		return rc;
124
+
125
+	return 0;
126
+}
127
+
128
+/**
129
+ * Transmit ICMP echo reply
130
+ *
131
+ * @v iobuf		I/O buffer
132
+ * @v st_dest		Destination socket address
133
+ * @ret rc		Return status code
134
+ */
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;
139
+	int rc;
140
+
141
+	/* Set type */
142
+	echo->icmp.type = echo_protocol->reply;
143
+
144
+	/* Transmit 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 )
148
+		return rc;
149
+
150
+	return 0;
151
+}
152
+
153
+/**
154
+ * Process a received ICMP echo request
155
+ *
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
160
+ */
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;
165
+	int rc;
166
+
167
+	/* Sanity check */
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 ) );
172
+		free_iob ( iobuf );
173
+		return -EINVAL;
80 174
 	}
175
+	DBGC ( icmpcol ( st_src ), "ICMP RX echo request id %04x seq %04x\n",
176
+	       ntohs ( echo->ident ), ntohs ( echo->sequence ) );
81 177
 
82
-	DBG ( "ICMP responding to ping\n" );
178
+	/* Transmit echo reply */
179
+	if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
180
+		return rc;
83 181
 
84
-	/* Change type to response and recalculate checksum */
85
-	icmp->type = ICMP_ECHO_RESPONSE;
86
-	icmp->chksum = 0;
87
-	icmp->chksum = tcpip_chksum ( icmp, len );
182
+	return 0;
183
+}
88 184
 
89
-	/* Transmit the response */
90
-	if ( ( rc = tcpip_tx ( iob_disown ( iobuf ), &icmp_protocol, st_dest,
91
-			       st_src, NULL, NULL ) ) != 0 ) {
92
-		DBG ( "ICMP could not transmit ping response: %s\n",
93
-		      strerror ( rc ) );
94
-		goto done;
185
+/**
186
+ * Process a received ICMP echo request
187
+ *
188
+ * @v iobuf		I/O buffer
189
+ * @v st_src		Source socket address
190
+ * @ret rc		Return status code
191
+ */
192
+int icmp_rx_echo_reply ( struct io_buffer *iobuf,
193
+			 struct sockaddr_tcpip *st_src ) {
194
+	struct icmp_echo *echo = iobuf->data;
195
+	int rc;
196
+
197
+	/* Sanity check */
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 ) );
202
+		free_iob ( iobuf );
203
+		return -EINVAL;
95 204
 	}
205
+	DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
206
+	       ntohs ( echo->ident ), ntohs ( echo->sequence ) );
96 207
 
97
- done:
98
-	free_iob ( iobuf );
99
-	return rc;
208
+	/* Deliver to ping protocol */
209
+	if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
210
+		return rc;
211
+
212
+	return 0;
100 213
 }
101 214
 
102
-/** ICMP TCP/IP protocol */
103
-struct tcpip_protocol icmp_protocol __tcpip_protocol = {
104
-	.name = "ICMP",
105
-	.rx = icmp_rx,
106
-	.tcpip_proto = IP_ICMP,
107
-};
215
+/**
216
+ * Receive ping reply (when no ping protocol is present)
217
+ *
218
+ * @v iobuf		I/O buffer
219
+ * @v st_src		Source socket address
220
+ * @ret rc		Return status code
221
+ */
222
+__weak int ping_rx ( struct io_buffer *iobuf,
223
+		     struct sockaddr_tcpip *st_src __unused ) {
224
+	free_iob ( iobuf );
225
+	return 0;
226
+}

+ 109
- 0
src/net/icmpv4.c Прегледај датотеку

@@ -0,0 +1,109 @@
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
+
20
+FILE_LICENCE ( GPL2_OR_LATER );
21
+
22
+#include <string.h>
23
+#include <errno.h>
24
+#include <ipxe/iobuf.h>
25
+#include <ipxe/in.h>
26
+#include <ipxe/tcpip.h>
27
+#include <ipxe/icmp.h>
28
+
29
+/** @file
30
+ *
31
+ * ICMPv4 protocol
32
+ *
33
+ */
34
+
35
+struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol;
36
+
37
+/**
38
+ * Process a received packet
39
+ *
40
+ * @v iobuf		I/O buffer
41
+ * @v netdev		Network device
42
+ * @v st_src		Partially-filled source address
43
+ * @v st_dest		Partially-filled destination address
44
+ * @v pshdr_csum	Pseudo-header checksum
45
+ * @ret rc		Return status code
46
+ */
47
+static int icmpv4_rx ( struct io_buffer *iobuf,
48
+		       struct net_device *netdev __unused,
49
+		       struct sockaddr_tcpip *st_src,
50
+		       struct sockaddr_tcpip *st_dest __unused,
51
+		       uint16_t pshdr_csum __unused ) {
52
+	struct icmp_header *icmp = iobuf->data;
53
+	size_t len = iob_len ( iobuf );
54
+	unsigned int csum;
55
+	unsigned int type;
56
+	int rc;
57
+
58
+	/* Sanity check */
59
+	if ( len < sizeof ( *icmp ) ) {
60
+		DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
61
+		      len, sizeof ( *icmp ) );
62
+		rc = -EINVAL;
63
+		goto discard;
64
+	}
65
+
66
+	/* Verify checksum */
67
+	csum = tcpip_chksum ( icmp, len );
68
+	if ( csum != 0 ) {
69
+		DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
70
+		      csum );
71
+		DBG_HD ( icmp, len );
72
+		rc = -EINVAL;
73
+		goto discard;
74
+	}
75
+
76
+	/* Handle ICMP packet */
77
+	type = icmp->type;
78
+	switch ( type ) {
79
+	case ICMP_ECHO_REQUEST:
80
+		return icmp_rx_echo_request ( iobuf, st_src,
81
+					      &icmpv4_echo_protocol );
82
+	case ICMP_ECHO_REPLY:
83
+		return icmp_rx_echo_reply ( iobuf, st_src );
84
+	default:
85
+		DBG ( "ICMP ignoring type %d\n", type );
86
+		rc = 0;
87
+		break;
88
+	}
89
+
90
+ discard:
91
+	free_iob ( iobuf );
92
+	return rc;
93
+}
94
+
95
+/** ICMPv4 TCP/IP protocol */
96
+struct tcpip_protocol icmpv4_protocol __tcpip_protocol = {
97
+	.name = "ICMPv4",
98
+	.rx = icmpv4_rx,
99
+	.tcpip_proto = IP_ICMP,
100
+};
101
+
102
+/** ICMPv4 echo protocol */
103
+struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol = {
104
+	.family = AF_INET,
105
+	.request = ICMP_ECHO_REQUEST,
106
+	.reply = ICMP_ECHO_REPLY,
107
+	.tcpip_protocol = &icmpv4_protocol,
108
+	.net_checksum = 0,
109
+};

+ 46
- 39
src/net/icmpv6.c Прегледај датотеку

@@ -25,6 +25,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
25 25
 #include <ipxe/in.h>
26 26
 #include <ipxe/iobuf.h>
27 27
 #include <ipxe/tcpip.h>
28
+#include <ipxe/ping.h>
28 29
 #include <ipxe/icmpv6.h>
29 30
 
30 31
 /** @file
@@ -33,6 +34,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
33 34
  *
34 35
  */
35 36
 
37
+struct icmp_echo_protocol icmpv6_echo_protocol __icmp_echo_protocol;
38
+
36 39
 /**
37 40
  * Process received ICMPv6 echo request packet
38 41
  *
@@ -42,50 +45,45 @@ FILE_LICENCE ( GPL2_OR_LATER );
42 45
  * @v sin6_dest		Destination socket address
43 46
  * @ret rc		Return status code
44 47
  */
45
-static int icmpv6_rx_echo ( struct io_buffer *iobuf,
46
-			    struct net_device *netdev,
47
-			    struct sockaddr_in6 *sin6_src,
48
-			    struct sockaddr_in6 *sin6_dest __unused ) {
48
+static int icmpv6_rx_echo_request ( struct io_buffer *iobuf,
49
+				    struct net_device *netdev __unused,
50
+				    struct sockaddr_in6 *sin6_src,
51
+				    struct sockaddr_in6 *sin6_dest __unused ) {
49 52
 	struct sockaddr_tcpip *st_src =
50 53
 		( ( struct sockaddr_tcpip * ) sin6_src );
51
-	struct icmpv6_echo *echo = iobuf->data;
52
-	size_t len = iob_len ( iobuf );
53
-	int rc;
54 54
 
55
-	/* Sanity check */
56
-	if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
57
-		DBGC ( netdev, "ICMPv6 echo request too short at %zd bytes "
58
-		       "(min %zd bytes)\n", iob_len ( iobuf ),
59
-		       sizeof ( *echo ) );
60
-		rc = -EINVAL;
61
-		goto done;
62
-	}
63
-	DBGC ( netdev, "ICMPv6 echo request from %s (id %#04x seq %#04x)\n",
64
-	       inet6_ntoa ( &sin6_dest->sin6_addr ), ntohs ( echo->ident ),
65
-	       ntohs ( echo->sequence ) );
66
-
67
-	/* Convert echo request to echo reply and recalculate checksum */
68
-	echo->icmp.type = ICMPV6_ECHO_REPLY;
69
-	echo->icmp.chksum = 0;
70
-	echo->icmp.chksum = tcpip_chksum ( echo, len );
71
-
72
-	/* Transmit echo reply */
73
-	if ( ( rc = tcpip_tx ( iob_disown ( iobuf ), &icmpv6_protocol, NULL,
74
-			       st_src, netdev, &echo->icmp.chksum ) ) != 0 ) {
75
-		DBGC ( netdev, "ICMPv6 could not transmit reply: %s\n",
76
-		       strerror ( rc ) );
77
-		goto done;
78
-	}
79
-
80
- done:
81
-	free_iob ( iobuf );
82
-	return rc;
55
+	return icmp_rx_echo_request ( iobuf, st_src, &icmpv6_echo_protocol );
83 56
 }
84 57
 
85
-/** ICMPv6 echo request handlers */
86
-struct icmpv6_handler icmpv6_echo_handler __icmpv6_handler = {
58
+/** ICMPv6 echo request handler */
59
+struct icmpv6_handler icmpv6_echo_request_handler __icmpv6_handler = {
87 60
 	.type = ICMPV6_ECHO_REQUEST,
88
-	.rx = icmpv6_rx_echo,
61
+	.rx = icmpv6_rx_echo_request,
62
+};
63
+
64
+/**
65
+ * Process received ICMPv6 echo reply packet
66
+ *
67
+ * @v iobuf		I/O buffer
68
+ * @v netdev		Network device
69
+ * @v sin6_src		Source socket address
70
+ * @v sin6_dest		Destination socket address
71
+ * @ret rc		Return status code
72
+ */
73
+static int icmpv6_rx_echo_reply ( struct io_buffer *iobuf,
74
+				  struct net_device *netdev __unused,
75
+				  struct sockaddr_in6 *sin6_src,
76
+				  struct sockaddr_in6 *sin6_dest __unused ) {
77
+	struct sockaddr_tcpip *st_src =
78
+		( ( struct sockaddr_tcpip * ) sin6_src );
79
+
80
+	return icmp_rx_echo_reply ( iobuf, st_src );
81
+}
82
+
83
+/** ICMPv6 echo reply handler */
84
+struct icmpv6_handler icmpv6_echo_reply_handler __icmpv6_handler = {
85
+	.type = ICMPV6_ECHO_REPLY,
86
+	.rx = icmpv6_rx_echo_reply,
89 87
 };
90 88
 
91 89
 /**
@@ -119,7 +117,7 @@ static int icmpv6_rx ( struct io_buffer *iobuf, struct net_device *netdev,
119 117
 		       struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum ) {
120 118
 	struct sockaddr_in6 *sin6_src = ( ( struct sockaddr_in6 * ) st_src );
121 119
 	struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
122
-	struct icmpv6_header *icmp = iobuf->data;
120
+	struct icmp_header *icmp = iobuf->data;
123 121
 	size_t len = iob_len ( iobuf );
124 122
 	struct icmpv6_handler *handler;
125 123
 	unsigned int csum;
@@ -170,3 +168,12 @@ struct tcpip_protocol icmpv6_protocol __tcpip_protocol = {
170 168
 	.rx = icmpv6_rx,
171 169
 	.tcpip_proto = IP_ICMP6,
172 170
 };
171
+
172
+/** ICMPv6 echo protocol */
173
+struct icmp_echo_protocol icmpv6_echo_protocol __icmp_echo_protocol = {
174
+	.family = AF_INET6,
175
+	.request = ICMPV6_ECHO_REQUEST,
176
+	.reply = ICMPV6_ECHO_REPLY,
177
+	.tcpip_protocol = &icmpv6_protocol,
178
+	.net_checksum = 1,
179
+};

+ 2
- 2
src/net/ipv4.c Прегледај датотеку

@@ -669,5 +669,5 @@ struct settings_applicator ipv4_settings_applicator __settings_applicator = {
669 669
 	.apply = ipv4_create_routes,
670 670
 };
671 671
 
672
-/* Drag in ICMP */
673
-REQUIRE_OBJECT ( icmp );
672
+/* Drag in ICMPv4 */
673
+REQUIRE_OBJECT ( icmpv4 );

Loading…
Откажи
Сачувај