summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/arch/mips/board/bcm63xx_ram/src/bcm63xx_net_icmp.c
blob: 89802f8c4bccf673d061cdfb7adb49e2bc0a238b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  ICMP Protocol				File: net_icmp.c
    *  
    *  This module implements portions of the ICMP protocol.  Note
    *  that it is not a complete implementation, just enough to 
    *  generate and respond to ICMP echo requests.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_string.h"
#include "lib_queue.h"
#include "lib_malloc.h"
#include "lib_printf.h"

#include "cfe_timer.h"

#include "net_ebuf.h"
#include "net_ether.h"

#include "net_ip.h"

/*  *********************************************************************
    *  Constants
    ********************************************************************* */

#define ICMP_CODE_ECHO		0
#define ICMP_TYPE_ECHOREPLY	0
#define ICMP_TYPE_ECHOREQ	8

#define ICMPMSG(type,code) (((type)<<8)|(code))

/*  *********************************************************************
    *  Structures
    ********************************************************************* */

struct icmp_info_s {
    ip_info_t *icmp_ipinfo;
    queue_t icmp_echoreplies;
    int icmp_maxreplies;
};

/*  *********************************************************************
    *  ICMP_RX_CALLBACK(ref,buf,dst,src)
    *  
    *  This routine is called by the IP layer when we receive
    *  ICMP protocol messages.
    *  
    *  Input parameters: 
    *  	   ref - reference data (an icmp_info_t)
    *  	   buf - the ebuf containing the buffer
    *  	   dst - destination IP address (us, usually)
    *  	   src - source IP address
    *  	   
    *  Return value:
    *  	   ETH_KEEP to keep packet, ETH_DROP to cause packet to be freed
    ********************************************************************* */

static int icmp_rx_callback(void *ref,ebuf_t *buf,uint8_t *dst,uint8_t *src)
{
    icmp_info_t *icmp = (icmp_info_t *)ref;
    ip_info_t *ipi = icmp->icmp_ipinfo;
    uint16_t imsg;
    uint16_t cksum;
    ebuf_t *txbuf;
    uint8_t *icmphdr;
    int res;

    imsg = ICMPMSG(buf->eb_ptr[0],buf->eb_ptr[1]);

    res = ETH_DROP;			/* assume we're dropping the pkt */

    switch (imsg) {
	case ICMPMSG(ICMP_TYPE_ECHOREQ,ICMP_CODE_ECHO):
	    txbuf = _ip_alloc(ipi);
	    if (txbuf) {
		/* Construct reply from the original packet. */		   
		icmphdr = txbuf->eb_ptr;
		ebuf_append_bytes(txbuf,buf->eb_ptr,buf->eb_length);
		icmphdr[0] = ICMP_TYPE_ECHOREPLY;
		icmphdr[1] = ICMP_CODE_ECHO;
		icmphdr[2] = 0; icmphdr[3] = 0;
		cksum = ~ip_chksum(0,icmphdr,ebuf_length(txbuf));
		icmphdr[2] = (cksum >> 8) & 0xFF;
		icmphdr[3] = (cksum & 0xFF);
		if (_ip_send(ipi,txbuf,src,IPPROTO_ICMP) < 0) {
		    _ip_free(ipi,txbuf);
		    }
		}
	    break;

	case ICMPMSG(ICMP_TYPE_ECHOREPLY,ICMP_CODE_ECHO):
	    if (q_count(&(icmp->icmp_echoreplies)) < icmp->icmp_maxreplies) {
		/* We're keeping this packet, put it on the queue and don't
		   free it in the driver. */
		q_enqueue(&(icmp->icmp_echoreplies),(queue_t *) buf);
		res = ETH_KEEP;
		}
	    break;

	default:
	    res = ETH_DROP;
	}
    
    return res;
}


/*  *********************************************************************
    *  _ICMP_INIT(ipi)
    *  
    *  Initialize the ICMP layer.
    *  
    *  Input parameters: 
    *  	   ipi - ipinfo structure of IP layer to attach to
    *  	   
    *  Return value:
    *  	   icmp_info_t structure or NULL if error occurs
    ********************************************************************* */

icmp_info_t *_icmp_init(ip_info_t *ipi)
{
    icmp_info_t *icmp;

    icmp = (icmp_info_t *) KMALLOC(sizeof(icmp_info_t),0);
    if (!icmp) return NULL;

    icmp->icmp_ipinfo = ipi;
    q_init(&(icmp->icmp_echoreplies));
    icmp->icmp_maxreplies = 0;

    _ip_register(ipi,IPPROTO_ICMP,icmp_rx_callback,icmp);

    return icmp;
}

/*  *********************************************************************
    *  _ICMP_UNINIT(icmp)
    *  
    *  Un-initialize the ICMP layer.
    *  
    *  Input parameters: 
    *  	   icmp - icmp_info_t structure
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

void _icmp_uninit(icmp_info_t *icmp)
{
    _ip_deregister(icmp->icmp_ipinfo,IPPROTO_ICMP);

    KFREE(icmp);
}

int _icmp_ping(icmp_info_t *icmp,uint8_t *dest,int seq,int len)
{
    return 0;
}