GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / net / net_icmp.c
blobe98bc87ee8cceb08f97d123d66fb6ab0c40cc146
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * ICMP Protocol File: net_icmp.c
5 *
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.
9 *
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"
58 #include "net_ebuf.h"
59 #include "net_ether.h"
61 #include "net_ip.h"
63 /* *********************************************************************
64 * Constants
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 /* *********************************************************************
74 * Structures
75 ********************************************************************* */
77 struct icmp_info_s {
78 ip_info_t *icmp_ipinfo;
79 queue_t icmp_echoreplies;
80 int icmp_maxreplies;
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.
89 * Input parameters:
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
95 * Return value:
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;
103 uint16_t imsg;
104 uint16_t cksum;
105 ebuf_t *txbuf;
106 uint8_t *icmphdr;
107 int res;
109 imsg = ICMPMSG(buf->eb_ptr[0],buf->eb_ptr[1]);
111 res = ETH_DROP; /* assume we're dropping the pkt */
113 switch (imsg) {
114 case ICMPMSG(ICMP_TYPE_ECHOREQ,ICMP_CODE_ECHO):
115 txbuf = _ip_alloc(ipi);
116 if (txbuf) {
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) {
127 _ip_free(ipi,txbuf);
130 break;
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);
137 res = ETH_KEEP;
139 break;
141 default:
142 res = ETH_DROP;
145 return res;
149 /* *********************************************************************
150 * _ICMP_INIT(ipi)
152 * Initialize the ICMP layer.
154 * Input parameters:
155 * ipi - ipinfo structure of IP layer to attach to
157 * Return value:
158 * icmp_info_t structure or NULL if error occurs
159 ********************************************************************* */
161 icmp_info_t *_icmp_init(ip_info_t *ipi)
163 icmp_info_t *icmp;
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);
174 return icmp;
177 /* *********************************************************************
178 * _ICMP_UNINIT(icmp)
180 * Un-initialize the ICMP layer.
182 * Input parameters:
183 * icmp - icmp_info_t structure
185 * Return value:
186 * nothing
187 ********************************************************************* */
189 void _icmp_uninit(icmp_info_t *icmp)
191 _ip_deregister(icmp->icmp_ipinfo,IPPROTO_ICMP);
193 KFREE(icmp);
197 /* *********************************************************************
198 * _ICMP_PING(icmp,dest,seq,len)
200 * Transmit an ICMP echo request and wait for a reply.
202 * Input parameters:
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
208 * Return value:
209 * <0 = error
210 * 0 = timeout
211 * >0 = reply received
212 ********************************************************************* */
214 int _icmp_ping(icmp_info_t *icmp,uint8_t *dest,int seq,int len)
216 ebuf_t *buf;
217 int64_t timer;
218 uint16_t cksum;
219 uint8_t *icmphdr;
220 uint16_t id;
221 int idx;
222 int result = 0;
225 * Get an ebuf
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);
268 buf = NULL;
271 * Wait for a reply
274 TIMER_SET(timer,2*CFE_HZ);
276 while (!TIMER_EXPIRED(timer)) {
278 POLL();
279 buf = (ebuf_t *) q_deqnext(&(icmp->icmp_echoreplies));
281 /* If we get a packet, make sure it matches. */
283 if (buf) {
284 uint16_t rxid,rxseq;
286 cksum = ip_chksum(0,buf->eb_ptr,ebuf_length(buf));
287 if (cksum == 0xFFFF) {
288 ebuf_skip(buf,2);
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)) {
294 result = 1;
295 break;
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);
310 return result;