[zbin] Fix check for existence of most recent output byte
[ipxe.git] / src / net / icmp.c
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 <byteswap.h>
24 #include <errno.h>
25 #include <ipxe/iobuf.h>
26 #include <ipxe/in.h>
27 #include <ipxe/tcpip.h>
28 #include <ipxe/ping.h>
29 #include <ipxe/crc32.h>
30 #include <ipxe/icmp.h>
31
32 /** @file
33 *
34 * ICMP protocol
35 *
36 */
37
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 }
53
54 /**
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
68 *
69 * @v iobuf I/O buffer
70 * @v st_dest Destination socket address
71 * @v echo_protocol ICMP echo protocol
72 * @ret rc Return status code
73 */
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;
78 int rc;
79
80 /* Set ICMP type and (re)calculate checksum */
81 echo->icmp.chksum = 0;
82 echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );
83
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;
114 }
115
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;
174 }
175 DBGC ( icmpcol ( st_src ), "ICMP RX echo request id %04x seq %04x\n",
176 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
177
178 /* Transmit echo reply */
179 if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
180 return rc;
181
182 return 0;
183 }
184
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;
204 }
205 DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
206 ntohs ( echo->ident ), ntohs ( echo->sequence ) );
207
208 /* Deliver to ping protocol */
209 if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
210 return rc;
211
212 return 0;
213 }
214
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 }