|
@@ -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
|
+}
|