1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * ICMP Protocol File: net_icmp.c
6 * This module implements portions of the ICMP protocol. Note
7 * that it is not a complete implementation, just enough to
8 * generate and respond to ICMP echo requests.
10 * Author: Mitch Lichtenberg (mpl@broadcom.com)
12 *********************************************************************
14 * Copyright 2000,2001,2002,2003
15 * Broadcom Corporation. All rights reserved.
17 * This software is furnished under license and may be used and
18 * copied only in accordance with the following terms and
19 * conditions. Subject to these conditions, you may download,
20 * copy, install, use, modify and distribute modified or unmodified
21 * copies of this software in source and/or binary form. No title
22 * or ownership is transferred hereby.
24 * 1) Any source code used, modified or distributed must reproduce
25 * and retain this copyright notice and list of conditions
26 * as they appear in the source file.
28 * 2) No right is granted to use any trade name, trademark, or
29 * logo of Broadcom Corporation. The "Broadcom Corporation"
30 * name may not be used to endorse or promote products derived
31 * from this software without the prior written permission of
32 * Broadcom Corporation.
34 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
35 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
36 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
37 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
38 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
39 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
42 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
44 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
45 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
46 * THE POSSIBILITY OF SUCH DAMAGE.
47 ********************************************************************* */
50 #include "lib_types.h"
51 #include "lib_string.h"
52 #include "lib_queue.h"
53 #include "lib_malloc.h"
54 #include "lib_printf.h"
56 #include "cfe_timer.h"
59 #include "net_ether.h"
63 /* *********************************************************************
65 ********************************************************************* */
67 #define ICMP_CODE_ECHO 0
68 #define ICMP_TYPE_ECHOREPLY 0
69 #define ICMP_TYPE_ECHOREQ 8
71 #define ICMPMSG(type,code) (((type)<<8)|(code))
73 /* *********************************************************************
75 ********************************************************************* */
78 ip_info_t
*icmp_ipinfo
;
79 queue_t icmp_echoreplies
;
83 /* *********************************************************************
84 * ICMP_RX_CALLBACK(ref,buf,dst,src)
86 * This routine is called by the IP layer when we receive
87 * ICMP protocol messages.
90 * ref - reference data (an icmp_info_t)
91 * buf - the ebuf containing the buffer
92 * dst - destination IP address (us, usually)
93 * src - source IP address
96 * ETH_KEEP to keep packet, ETH_DROP to cause packet to be freed
97 ********************************************************************* */
99 static int icmp_rx_callback(void *ref
,ebuf_t
*buf
,uint8_t *dst
,uint8_t *src
)
101 icmp_info_t
*icmp
= (icmp_info_t
*)ref
;
102 ip_info_t
*ipi
= icmp
->icmp_ipinfo
;
109 imsg
= ICMPMSG(buf
->eb_ptr
[0],buf
->eb_ptr
[1]);
111 res
= ETH_DROP
; /* assume we're dropping the pkt */
114 case ICMPMSG(ICMP_TYPE_ECHOREQ
,ICMP_CODE_ECHO
):
115 txbuf
= _ip_alloc(ipi
);
117 /* Construct reply from the original packet. */
118 icmphdr
= txbuf
->eb_ptr
;
119 ebuf_append_bytes(txbuf
,buf
->eb_ptr
,buf
->eb_length
);
120 icmphdr
[0] = ICMP_TYPE_ECHOREPLY
;
121 icmphdr
[1] = ICMP_CODE_ECHO
;
122 icmphdr
[2] = 0; icmphdr
[3] = 0;
123 cksum
= ~ip_chksum(0,icmphdr
,ebuf_length(txbuf
));
124 icmphdr
[2] = (cksum
>> 8) & 0xFF;
125 icmphdr
[3] = (cksum
& 0xFF);
126 if (_ip_send(ipi
,txbuf
,src
,IPPROTO_ICMP
) < 0) {
132 case ICMPMSG(ICMP_TYPE_ECHOREPLY
,ICMP_CODE_ECHO
):
133 if (q_count(&(icmp
->icmp_echoreplies
)) < icmp
->icmp_maxreplies
) {
134 /* We're keeping this packet, put it on the queue and don't
135 free it in the driver. */
136 q_enqueue(&(icmp
->icmp_echoreplies
),(queue_t
*) buf
);
149 /* *********************************************************************
152 * Initialize the ICMP layer.
155 * ipi - ipinfo structure of IP layer to attach to
158 * icmp_info_t structure or NULL if error occurs
159 ********************************************************************* */
161 icmp_info_t
*_icmp_init(ip_info_t
*ipi
)
165 icmp
= (icmp_info_t
*) KMALLOC(sizeof(icmp_info_t
),0);
166 if (!icmp
) return NULL
;
168 icmp
->icmp_ipinfo
= ipi
;
169 q_init(&(icmp
->icmp_echoreplies
));
170 icmp
->icmp_maxreplies
= 0;
172 _ip_register(ipi
,IPPROTO_ICMP
,icmp_rx_callback
,icmp
);
177 /* *********************************************************************
180 * Un-initialize the ICMP layer.
183 * icmp - icmp_info_t structure
187 ********************************************************************* */
189 void _icmp_uninit(icmp_info_t
*icmp
)
191 _ip_deregister(icmp
->icmp_ipinfo
,IPPROTO_ICMP
);
197 /* *********************************************************************
198 * _ICMP_PING(icmp,dest,seq,len)
200 * Transmit an ICMP echo request and wait for a reply.
203 * icmp - icmp_info_t structure
204 * dest - destination IP address
205 * seq - sequence number for ICMP packet
206 * len - length of data portion of ICMP packet
211 * >0 = reply received
212 ********************************************************************* */
214 int _icmp_ping(icmp_info_t
*icmp
,uint8_t *dest
,int seq
,int len
)
228 buf
= _ip_alloc(icmp
->icmp_ipinfo
);
229 if (buf
== NULL
) return -1;
232 * Remember where the ICMP header is going to be so we can
233 * calculate the checksum later.
236 icmphdr
= buf
->eb_ptr
;
238 id
= (uint16_t) cfe_ticks
;
241 * Construct the ICMP header and data portion.
244 ebuf_append_u8(buf
,8); /* echo message */
245 ebuf_append_u8(buf
,0); /* code = 0 */
246 ebuf_append_u16_be(buf
,0); /* empty checksum for now */
247 ebuf_append_u16_be(buf
,id
); /* packet ID */
248 ebuf_append_u16_be(buf
,((uint16_t)seq
)); /* sequence # */
250 for (idx
= 0; idx
< len
; idx
++) {
251 ebuf_append_u8(buf
,((idx
+0x40)&0xFF)); /* data */
255 * Calculate and install the checksum
258 cksum
= ~ip_chksum(0,icmphdr
,ebuf_length(buf
));
259 icmphdr
[2] = (cksum
>> 8) & 0xFF;
260 icmphdr
[3] = (cksum
& 0xFF);
263 * Transmit the ICMP echo
266 icmp
->icmp_maxreplies
= 1; /* allow ICMP replies */
267 _ip_send(icmp
->icmp_ipinfo
,buf
,dest
,IPPROTO_ICMP
);
274 TIMER_SET(timer
,2*CFE_HZ
);
276 while (!TIMER_EXPIRED(timer
)) {
279 buf
= (ebuf_t
*) q_deqnext(&(icmp
->icmp_echoreplies
));
281 /* If we get a packet, make sure it matches. */
286 cksum
= ip_chksum(0,buf
->eb_ptr
,ebuf_length(buf
));
287 if (cksum
== 0xFFFF) {
289 ebuf_skip(buf
,2); /* skip checksum */
290 ebuf_get_u16_be(buf
,rxid
);
291 ebuf_get_u16_be(buf
,rxseq
);
293 if ((id
== rxid
) && ((uint16_t) seq
== rxseq
)) {
298 _ip_free(icmp
->icmp_ipinfo
,buf
);
303 * Don't accept any more replies.
306 icmp
->icmp_maxreplies
= 0; /* allow ICMP replies */
308 if (buf
) _ip_free(icmp
->icmp_ipinfo
,buf
);